Skip to content

Commit 6c0ee2a

Browse files
abnegateclaude
andcommitted
fix(sqlite): include _tenant in shared perms UNIQUE constraint
Match MariaDB's shared shape: under shared tables UNIQUE perms is (_document, _type, _permission, _tenant). Without _tenant the upsert path's perm INSERT collides when two tenants legitimately hold the same role on the same document — exactly what the SharedTables/SQLite upsert tests trip over once the cursor cascade is gone. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 2e0ed26 commit 6c0ee2a

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/Database/Adapter/SQLite.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,16 @@ public function createCollection(string $name, array $attributes = [], array $in
219219
$this->createIndex($id, '_created_at', Database::INDEX_KEY, [ '_createdAt'], [], []);
220220
$this->createIndex($id, '_updated_at', Database::INDEX_KEY, [ '_updatedAt'], [], []);
221221

222-
// Permissions UNIQUE stays on (_document, _type, _permission)
223-
// for now even under shared tables: composite unique with
224-
// _tenant trips upsert flows that re-insert permissions
225-
// without first re-loading the existing rows. Revisit once
226-
// the upsert permission diff is hardened.
227-
$this->createIndex("{$id}_perms", '_index_1', Database::INDEX_UNIQUE, ['_document', '_type', '_permission'], [], []);
222+
// Match MariaDB's shared shape: include _tenant in the perms
223+
// UNIQUE so two tenants can both grant the same role on the
224+
// same document without colliding. Without _tenant the
225+
// upsert path for shared tables hits a UNIQUE violation
226+
// when a new tenant re-inserts permissions for a row that
227+
// already exists in another tenant.
228+
$permsColumns = $this->sharedTables
229+
? ['_document', '_type', '_permission', '_tenant']
230+
: ['_document', '_type', '_permission'];
231+
$this->createIndex("{$id}_perms", '_index_1', Database::INDEX_UNIQUE, $permsColumns, [], []);
228232
$this->createIndex("{$id}_perms", '_index_2', Database::INDEX_KEY, ['_permission', '_type'], [], []);
229233

230234
if ($this->sharedTables) {

0 commit comments

Comments
 (0)