Skip to content

Commit 2cedf45

Browse files
committed
Add schema to all queries containing the tablename
This adds the schema-enabling to all queries that target the tablename.
1 parent d699231 commit 2cedf45

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ services:
77
- .:/app
88
environment:
99
- PROOPH_ENV=dev
10-
- PDO_DSN=pgsql:host=postgres port=5432 dbname=event_engine
10+
- PDO_DSN=pgsql:host=postgres port=5433 dbname=event_engine
1111
- PDO_USER=postgres
1212
- PDO_PWD=
1313

1414
postgres:
1515
image: postgres:alpine
1616
ports:
17-
- 5432:5432
17+
- 5433:5432
1818
environment:
1919
- POSTGRES_DB=event_engine

src/PostgresDocumentStore.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function addCollection(string $collectionName, Index ...$indices): void
177177
public function dropCollection(string $collectionName): void
178178
{
179179
$cmd = <<<EOT
180-
DROP TABLE {$this->tableName($collectionName)};
180+
DROP TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)};
181181
EOT;
182182

183183
$this->transactional(function () use ($cmd) {
@@ -191,6 +191,7 @@ public function hasCollectionIndex(string $collectionName, string $indexName): b
191191
SELECT INDEXNAME
192192
FROM pg_indexes
193193
WHERE TABLENAME = '{$this->tableName($collectionName)}'
194+
AND SCHEMANAME = '{$this->schemaName($collectionName)}'
194195
AND INDEXNAME = '$indexName'
195196
EOT;
196197

@@ -223,7 +224,7 @@ public function addCollectionIndex(string $collectionName, Index $index): void
223224
$columnsSql = substr($columnsSql, 2);
224225

225226
$metadataColumnCmd = <<<EOT
226-
ALTER TABLE {$this->tableName($collectionName)}
227+
ALTER TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
227228
$columnsSql;
228229
EOT;
229230

@@ -263,7 +264,7 @@ public function dropCollectionIndex(string $collectionName, $index): void
263264
$columnsSql = substr($columnsSql, 2);
264265

265266
$metadataColumnCmd = <<<EOT
266-
ALTER TABLE {$this->tableName($collectionName)}
267+
ALTER TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
267268
$columnsSql;
268269
EOT;
269270
$index = $index->indexCmd();
@@ -313,7 +314,9 @@ public function addDoc(string $collectionName, string $docId, array $doc): void
313314
}
314315

315316
$cmd = <<<EOT
316-
INSERT INTO {$this->tableName($collectionName)} (id, doc{$metadataKeysStr}) VALUES (:id, :doc{$metadataValsStr});
317+
INSERT INTO {$this->schemaName($collectionName)}.{$this->tableName($collectionName)} (
318+
id, doc{$metadataKeysStr}) VALUES (:id, :doc{$metadataValsStr}
319+
);
317320
EOT;
318321

319322
$this->transactional(function () use ($cmd, $docId, $doc, $metadata) {
@@ -346,7 +349,7 @@ public function updateDoc(string $collectionName, string $docId, array $docOrSub
346349
}
347350

348351
$cmd = <<<EOT
349-
UPDATE {$this->tableName($collectionName)}
352+
UPDATE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
350353
SET doc = (to_jsonb(doc) || :doc){$metadataStr}
351354
WHERE id = :id
352355
;
@@ -385,7 +388,7 @@ public function updateMany(string $collectionName, Filter $filter, array $set):
385388
}
386389

387390
$cmd = <<<EOT
388-
UPDATE {$this->tableName($collectionName)}
391+
UPDATE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
389392
SET doc = (to_jsonb(doc) || :doc){$metadataStr}
390393
$where;
391394
EOT;
@@ -425,7 +428,7 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
425428
public function deleteDoc(string $collectionName, string $docId): void
426429
{
427430
$cmd = <<<EOT
428-
DELETE FROM {$this->tableName($collectionName)}
431+
DELETE FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
429432
WHERE id = :id
430433
EOT;
431434

@@ -448,7 +451,7 @@ public function deleteMany(string $collectionName, Filter $filter): void
448451
$where = $filterStr? "WHERE $filterStr" : '';
449452

450453
$cmd = <<<EOT
451-
DELETE FROM {$this->tableName($collectionName)}
454+
DELETE FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
452455
$where;
453456
EOT;
454457

@@ -466,7 +469,7 @@ public function getDoc(string $collectionName, string $docId): ?array
466469
{
467470
$query = <<<EOT
468471
SELECT doc
469-
FROM {$this->tableName($collectionName)}
472+
FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
470473
WHERE id = :id
471474
EOT;
472475
$stmt = $this->connection->prepare($query);
@@ -503,7 +506,7 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
503506

504507
$query = <<<EOT
505508
SELECT doc
506-
FROM {$this->tableName($collectionName)}
509+
FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
507510
$where
508511
$orderBy
509512
$limit
@@ -702,7 +705,7 @@ private function indexToSqlCmd(Index $index, string $collectionName): string
702705
$name = $index->name() ?? '';
703706

704707
$cmd = <<<EOT
705-
CREATE $type $name ON {$this->tableName($collectionName)}
708+
CREATE $type $name ON {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
706709
$fields;
707710
EOT;
708711

0 commit comments

Comments
 (0)