Skip to content

Commit d5d68ab

Browse files
committed
Add creation of schema to docstore creation
This commit adds the creation of the schema to the process of creating the document store. So now the schema does not need to be already available when the document store is created
1 parent 2cedf45 commit d5d68ab

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/PostgresDocumentStore.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ public function addCollection(string $collectionName, Index ...$indices): void
148148
}
149149
}
150150

151+
$createSchemaCmd = "CREATE SCHEMA IF NOT EXISTS {$this->schemaName($collectionName)}";
152+
151153
$cmd = <<<EOT
152154
CREATE TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)} (
153155
id {$this->docIdSchema},
@@ -161,7 +163,8 @@ public function addCollection(string $collectionName, Index ...$indices): void
161163
return $this->indexToSqlCmd($index, $collectionName);
162164
}, $indices);
163165

164-
$this->transactional(function() use ($cmd, $indicesCmds) {
166+
$this->transactional(function() use ($createSchemaCmd, $cmd, $indicesCmds) {
167+
$this->connection->prepare($createSchemaCmd)->execute();
165168
$this->connection->prepare($cmd)->execute();
166169

167170
array_walk($indicesCmds, function ($cmd) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* This file is part of the event-engine/php-postgres-document-store.
4+
* (c) 2019 prooph software GmbH <contact@prooph.de>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace EventEngine\DocumentStoreTest\Postgres;
13+
14+
use EventEngine\DocumentStore\Filter\AnyOfDocIdFilter;
15+
use EventEngine\DocumentStore\Filter\AnyOfFilter;
16+
use EventEngine\DocumentStore\Filter\DocIdFilter;
17+
use EventEngine\DocumentStore\Filter\NotFilter;
18+
use PHPUnit\Framework\TestCase;
19+
use EventEngine\DocumentStore\FieldIndex;
20+
use EventEngine\DocumentStore\Index;
21+
use EventEngine\DocumentStore\MultiFieldIndex;
22+
use EventEngine\DocumentStore\Postgres\PostgresDocumentStore;
23+
use Ramsey\Uuid\Uuid;
24+
25+
class SchemedPostgresDocumentStoreTest extends TestCase
26+
{
27+
private CONST TABLE_PREFIX = 'test_';
28+
private CONST SCHEMA = 'test.';
29+
30+
/**
31+
* @var PostgresDocumentStore
32+
*/
33+
protected $documentStore;
34+
35+
/**
36+
* @var \PDO
37+
*/
38+
protected $connection;
39+
40+
protected function setUp(): void
41+
{
42+
$this->connection = TestUtil::getConnection();
43+
$this->documentStore = new PostgresDocumentStore($this->connection, self::TABLE_PREFIX);
44+
}
45+
46+
public function tearDown(): void
47+
{
48+
TestUtil::tearDownDatabase();
49+
}
50+
51+
/**
52+
* @test
53+
*/
54+
public function it_adds_collection_with_schema(): void
55+
{
56+
$this->documentStore->addCollection(self::SCHEMA . 'test');
57+
$this->assertFalse($this->documentStore->hasCollection('test'));
58+
$this->assertTrue($this->documentStore->hasCollection(self::SCHEMA . 'test'));
59+
}
60+
}

0 commit comments

Comments
 (0)