@@ -119,6 +119,7 @@ public function hasCollection(string $collectionName): bool
119119SELECT TABLE_NAME
120120FROM information_schema.tables
121121WHERE TABLE_NAME = ' {$ this ->tableName ($ collectionName )}'
122+ AND TABLE_SCHEMA = ' {$ this ->schemaName ($ collectionName )}'
122123EOT ;
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 )};
180184EOT ;
181185
182186 $ this ->transactional (function () use ($ cmd ) {
@@ -190,6 +194,7 @@ public function hasCollectionIndex(string $collectionName, string $indexName): b
190194SELECT INDEXNAME
191195FROM pg_indexes
192196WHERE TABLENAME = ' {$ this ->tableName ($ collectionName )}'
197+ AND SCHEMANAME = ' {$ this ->schemaName ($ collectionName )}'
193198AND INDEXNAME = ' $ indexName'
194199EOT ;
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;
227232EOT ;
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;
267272EOT ;
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+ );
316323EOT ;
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 )}
349356SET doc = (to_jsonb(doc) || :doc) {$ metadataStr }
350357WHERE 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 )}
388395SET doc = (to_jsonb(doc) || :doc) {$ metadataStr }
389396$ where;
390397EOT ;
@@ -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 )}
428435WHERE id = :id
429436EOT ;
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;
452459EOT ;
453460
@@ -465,7 +472,7 @@ public function getDoc(string $collectionName, string $docId): ?array
465472 {
466473 $ query = <<<EOT
467474SELECT doc
468- FROM {$ this ->tableName ($ collectionName )}
475+ FROM {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
469476WHERE id = :id
470477EOT ;
471478 $ stmt = $ this ->connection ->prepare ($ query );
@@ -502,7 +509,7 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
502509
503510 $ query = <<<EOT
504511SELECT 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;
706713EOT ;
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}
0 commit comments