Skip to content

Commit d699231

Browse files
committed
Allow usage of different schemas for documents
This commit adds support for using dedicated schemas for storing documents. This way one can separate storage of documents of different scopes do dedicated schemas per scope. THis allows for a better structuring of the documents within the database. This commit does **not** handle creating the schemas. The schema needs to be available to the DocumentStore for this to work. Basically this allows one to set the schema right within the table-name by separating schema and tablename by a dot like this: `schemaname.tablename`. The name will then be split on the dot and the former part be used as schema-name, the later part as table-name. Any table-prefixes will only be added to the table name, not to the scheme.
1 parent 9701437 commit d699231

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/PostgresDocumentStore.php

Lines changed: 15 additions & 1 deletion
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);
@@ -148,7 +149,7 @@ public function addCollection(string $collectionName, Index ...$indices): void
148149
}
149150

150151
$cmd = <<<EOT
151-
CREATE TABLE {$this->tableName($collectionName)} (
152+
CREATE TABLE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)} (
152153
id {$this->docIdSchema},
153154
doc JSONB NOT NULL,
154155
$metadataColumns
@@ -751,6 +752,19 @@ private function extractFieldPartFromFieldIndex(DocumentStore\FieldIndex $fieldI
751752

752753
private function tableName(string $collectionName): string
753754
{
755+
if (false !== $dotPosition = strpos($collectionName, '.')) {
756+
$collectionName = substr($collectionName, $dotPosition+1);
757+
}
758+
754759
return mb_strtolower($this->tablePrefix . $collectionName);
755760
}
761+
762+
private function schemaName(string $collectionName): string
763+
{
764+
$schemaName = 'public';
765+
if (false !== $dotPosition = strpos($collectionName, '.')) {
766+
$schemaName = substr($collectionName, 0, $dotPosition);
767+
}
768+
return mb_strtolower($schemaName);
769+
}
756770
}

0 commit comments

Comments
 (0)