Skip to content

Commit 5fd882e

Browse files
removed unique constraint and added conflict keys on upsert
1 parent efa1bcc commit 5fd882e

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/Database/Adapter/Postgres.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,26 @@ public function createCollection(string $name, array $attributes = [], array $in
232232
CREATE TABLE {$this->getSQLTable($id)} (
233233
_id SERIAL NOT NULL,
234234
_uid VARCHAR(255) NOT NULL,
235-
". $sqlTenant ."
235+
" . $sqlTenant . "
236236
\"_createdAt\" TIMESTAMP(3) DEFAULT NULL,
237237
\"_updatedAt\" TIMESTAMP(3) DEFAULT NULL,
238238
_permissions TEXT DEFAULT NULL,
239239
" . \implode(' ', $attributeStrings) . "
240240
PRIMARY KEY (_id),
241-
UNIQUE (\"_uid\")
241+
UNIQUE(_uid)
242242
);
243243
";
244244

245245
if ($this->sharedTables) {
246246
$collection .= "
247-
CREATE UNIQUE INDEX \"{$namespace}_{$this->tenant}_{$id}_uid\" ON {$this->getSQLTable($id)} (LOWER(_uid), _tenant);
247+
CREATE UNIQUE INDEX \"{$namespace}_{$this->tenant}_{$id}_uid\" ON {$this->getSQLTable($id)} (\"_uid\", \"_tenant\");
248248
CREATE INDEX \"{$namespace}_{$this->tenant}_{$id}_created\" ON {$this->getSQLTable($id)} (_tenant, \"_createdAt\");
249249
CREATE INDEX \"{$namespace}_{$this->tenant}_{$id}_updated\" ON {$this->getSQLTable($id)} (_tenant, \"_updatedAt\");
250250
CREATE INDEX \"{$namespace}_{$this->tenant}_{$id}_tenant_id\" ON {$this->getSQLTable($id)} (_tenant, _id);
251251
";
252252
} else {
253253
$collection .= "
254-
CREATE UNIQUE INDEX \"{$namespace}_{$id}_uid\" ON {$this->getSQLTable($id)} (LOWER(_uid));
254+
CREATE UNIQUE INDEX \"{$namespace}_{$id}_uid\" ON {$this->getSQLTable($id)} (\"_uid\");
255255
CREATE INDEX \"{$namespace}_{$id}_created\" ON {$this->getSQLTable($id)} (\"_createdAt\");
256256
CREATE INDEX \"{$namespace}_{$id}_updated\" ON {$this->getSQLTable($id)} (\"_updatedAt\");
257257
";
@@ -1303,13 +1303,14 @@ protected function getUpsertStatement(
13031303
}
13041304
}
13051305

1306+
$conflictKeys = $this->sharedTables ? '("_uid", _tenant)' : '("_uid")';
1307+
13061308
$stmt = $this->getPDO()->prepare(
13071309
"
13081310
INSERT INTO {$this->getSQLTable($tableName)} AS target {$columns}
13091311
VALUES " . implode(', ', $batchKeys) . "
1310-
ON CONFLICT (\"_uid\") DO UPDATE
1311-
SET
1312-
" . implode(', ', $updateColumns)
1312+
ON CONFLICT {$conflictKeys} DO UPDATE
1313+
SET " . implode(', ', $updateColumns)
13131314
);
13141315

13151316
foreach ($bindValues as $key => $binding) {

0 commit comments

Comments
 (0)