Skip to content

Commit b4c7b04

Browse files
author
Alexander Miertsch
authored
Merge pull request #3 from heiglandreas/allowUsageOfSchema
Allow usage of different schemas for documents
2 parents 9701437 + b2ee535 commit b4c7b04

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

src/PostgresDocumentStore.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function hasCollection(string $collectionName): bool
119119
SELECT TABLE_NAME
120120
FROM information_schema.tables
121121
WHERE TABLE_NAME = '{$this->tableName($collectionName)}'
122+
AND TABLE_SCHEMA = '{$this->schemaName($collectionName)}'
122123
EOT;
123124

124125
$stmt = $this->connection->prepare($query);
@@ -147,8 +148,10 @@ public function addCollection(string $collectionName, Index ...$indices): void
147148
}
148149
}
149150

151+
$createSchemaCmd = "CREATE SCHEMA IF NOT EXISTS {$this->schemaName($collectionName)}";
152+
150153
$cmd = <<<EOT
151-
CREATE TABLE {$this->tableName($collectionName)} (
154+
CREATE TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)} (
152155
id {$this->docIdSchema},
153156
doc JSONB NOT NULL,
154157
$metadataColumns
@@ -160,7 +163,8 @@ public function addCollection(string $collectionName, Index ...$indices): void
160163
return $this->indexToSqlCmd($index, $collectionName);
161164
}, $indices);
162165

163-
$this->transactional(function() use ($cmd, $indicesCmds) {
166+
$this->transactional(function() use ($createSchemaCmd, $cmd, $indicesCmds) {
167+
$this->connection->prepare($createSchemaCmd)->execute();
164168
$this->connection->prepare($cmd)->execute();
165169

166170
array_walk($indicesCmds, function ($cmd) {
@@ -176,7 +180,7 @@ public function addCollection(string $collectionName, Index ...$indices): void
176180
public function dropCollection(string $collectionName): void
177181
{
178182
$cmd = <<<EOT
179-
DROP TABLE {$this->tableName($collectionName)};
183+
DROP TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)};
180184
EOT;
181185

182186
$this->transactional(function () use ($cmd) {
@@ -190,6 +194,7 @@ public function hasCollectionIndex(string $collectionName, string $indexName): b
190194
SELECT INDEXNAME
191195
FROM pg_indexes
192196
WHERE TABLENAME = '{$this->tableName($collectionName)}'
197+
AND SCHEMANAME = '{$this->schemaName($collectionName)}'
193198
AND INDEXNAME = '$indexName'
194199
EOT;
195200

@@ -222,7 +227,7 @@ public function addCollectionIndex(string $collectionName, Index $index): void
222227
$columnsSql = substr($columnsSql, 2);
223228

224229
$metadataColumnCmd = <<<EOT
225-
ALTER TABLE {$this->tableName($collectionName)}
230+
ALTER TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
226231
$columnsSql;
227232
EOT;
228233

@@ -262,7 +267,7 @@ public function dropCollectionIndex(string $collectionName, $index): void
262267
$columnsSql = substr($columnsSql, 2);
263268

264269
$metadataColumnCmd = <<<EOT
265-
ALTER TABLE {$this->tableName($collectionName)}
270+
ALTER TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
266271
$columnsSql;
267272
EOT;
268273
$index = $index->indexCmd();
@@ -312,7 +317,9 @@ public function addDoc(string $collectionName, string $docId, array $doc): void
312317
}
313318

314319
$cmd = <<<EOT
315-
INSERT INTO {$this->tableName($collectionName)} (id, doc{$metadataKeysStr}) VALUES (:id, :doc{$metadataValsStr});
320+
INSERT INTO {$this->schemaName($collectionName)}.{$this->tableName($collectionName)} (
321+
id, doc{$metadataKeysStr}) VALUES (:id, :doc{$metadataValsStr}
322+
);
316323
EOT;
317324

318325
$this->transactional(function () use ($cmd, $docId, $doc, $metadata) {
@@ -345,7 +352,7 @@ public function updateDoc(string $collectionName, string $docId, array $docOrSub
345352
}
346353

347354
$cmd = <<<EOT
348-
UPDATE {$this->tableName($collectionName)}
355+
UPDATE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
349356
SET doc = (to_jsonb(doc) || :doc){$metadataStr}
350357
WHERE id = :id
351358
;
@@ -384,7 +391,7 @@ public function updateMany(string $collectionName, Filter $filter, array $set):
384391
}
385392

386393
$cmd = <<<EOT
387-
UPDATE {$this->tableName($collectionName)}
394+
UPDATE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
388395
SET doc = (to_jsonb(doc) || :doc){$metadataStr}
389396
$where;
390397
EOT;
@@ -424,7 +431,7 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
424431
public function deleteDoc(string $collectionName, string $docId): void
425432
{
426433
$cmd = <<<EOT
427-
DELETE FROM {$this->tableName($collectionName)}
434+
DELETE FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
428435
WHERE id = :id
429436
EOT;
430437

@@ -447,7 +454,7 @@ public function deleteMany(string $collectionName, Filter $filter): void
447454
$where = $filterStr? "WHERE $filterStr" : '';
448455

449456
$cmd = <<<EOT
450-
DELETE FROM {$this->tableName($collectionName)}
457+
DELETE FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
451458
$where;
452459
EOT;
453460

@@ -465,7 +472,7 @@ public function getDoc(string $collectionName, string $docId): ?array
465472
{
466473
$query = <<<EOT
467474
SELECT doc
468-
FROM {$this->tableName($collectionName)}
475+
FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
469476
WHERE id = :id
470477
EOT;
471478
$stmt = $this->connection->prepare($query);
@@ -502,7 +509,7 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
502509

503510
$query = <<<EOT
504511
SELECT doc
505-
FROM {$this->tableName($collectionName)}
512+
FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
506513
$where
507514
$orderBy
508515
$limit
@@ -701,7 +708,7 @@ private function indexToSqlCmd(Index $index, string $collectionName): string
701708
$name = $index->name() ?? '';
702709

703710
$cmd = <<<EOT
704-
CREATE $type $name ON {$this->tableName($collectionName)}
711+
CREATE $type $name ON {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
705712
$fields;
706713
EOT;
707714

@@ -751,6 +758,19 @@ private function extractFieldPartFromFieldIndex(DocumentStore\FieldIndex $fieldI
751758

752759
private function tableName(string $collectionName): string
753760
{
761+
if (false !== $dotPosition = strpos($collectionName, '.')) {
762+
$collectionName = substr($collectionName, $dotPosition+1);
763+
}
764+
754765
return mb_strtolower($this->tablePrefix . $collectionName);
755766
}
767+
768+
private function schemaName(string $collectionName): string
769+
{
770+
$schemaName = 'public';
771+
if (false !== $dotPosition = strpos($collectionName, '.')) {
772+
$schemaName = substr($collectionName, 0, $dotPosition);
773+
}
774+
return mb_strtolower($schemaName);
775+
}
756776
}
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)