Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1793,16 +1793,27 @@ public function createCollection(string $id, array $attributes = [], array $inde
try {
$this->adapter->createCollection($id, $attributes, $indexes);
} catch (DuplicateException $e) {
// Metadata check (above) already verified collection is absent
// from metadata. A DuplicateException from the adapter means the
// collection exists only in physical schema — an orphan from a prior
// partial failure. Drop and recreate to ensure schema matches.
try {
$this->adapter->deleteCollection($id);
} catch (NotFoundException) {
// Already removed by a concurrent reconciler.
if ($this->adapter->getSharedTables()) {
// In shared-tables mode the physical table is reused across
// tenants. A DuplicateException simply means the table already
// exists for another tenant — not an orphan. Verify the table
// is actually present before writing tenant metadata.
if ($id !== self::METADATA && !$this->adapter->exists($this->adapter->getDatabase(), $id)) {
throw $e;
}
} else {
// Metadata check (above) already verified collection is absent
// from metadata. A DuplicateException from the adapter means
// the collection exists only in physical schema — an orphan
// from a prior partial failure. Drop and recreate to ensure
// schema matches.
try {
$this->adapter->deleteCollection($id);
} catch (NotFoundException) {
// Already removed by a concurrent reconciler.
}
$this->adapter->createCollection($id, $attributes, $indexes);
}
Comment thread
abnegate marked this conversation as resolved.
$this->adapter->createCollection($id, $attributes, $indexes);
}

if ($id === self::METADATA) {
Expand All @@ -1812,10 +1823,14 @@ public function createCollection(string $id, array $attributes = [], array $inde
try {
$createdCollection = $this->silent(fn () => $this->createDocument(self::METADATA, $collection));
} catch (\Throwable $e) {
try {
$this->cleanupCollection($id);
} catch (\Throwable $e) {
Console::error("Failed to rollback collection '{$id}': " . $e->getMessage());
if (!$this->adapter->getSharedTables()) {
try {
$this->cleanupCollection($id);
} catch (\Throwable $e) {
Console::error("Failed to rollback collection '{$id}': " . $e->getMessage());
}
} else {
Console::warning("createCollection '{$id}' metadata failed in shared-tables mode; physical table retained. Metadata document must be re-created.");
}
throw new DatabaseException("Failed to create collection metadata for '{$id}': " . $e->getMessage(), previous: $e);
}
Expand Down
104 changes: 104 additions & 0 deletions tests/e2e/Adapter/Scopes/CollectionTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,110 @@ public function testSharedTablesDuplicates(): void
->setDatabase($schema);
}

public function testSharedTablesMultiTenantCreateCollection(): void
{
/** @var Database $database */
$database = $this->getDatabase();
$sharedTables = $database->getSharedTables();
$namespace = $database->getNamespace();
$schema = $database->getDatabase();

if (!$database->getAdapter()->getSupportForSchemas()) {
$this->expectNotToPerformAssertions();
return;
}

$dbName = 'stMultiTenant';
if ($database->exists($dbName)) {
$database->setDatabase($dbName)->delete();
}

$tenant1 = 10;
$tenant2 = 20;

$database
->setDatabase($dbName)
->setNamespace('')
->setSharedTables(true)
->setTenant($tenant1)
->create();

$database->createCollection('multiTenantCol', [
new Document([
'$id' => 'name',
'type' => Database::VAR_STRING,
'size' => 128,
'required' => true,
]),
]);

$col1 = $database->getCollection('multiTenantCol');
$this->assertFalse($col1->isEmpty());
$this->assertEquals(1, \count($col1->getAttribute('attributes')));

$database->setTenant($tenant2);

$database->createCollection('multiTenantCol', [
new Document([
'$id' => 'name',
'type' => Database::VAR_STRING,
'size' => 128,
'required' => true,
]),
]);

$col2 = $database->getCollection('multiTenantCol');
$this->assertFalse($col2->isEmpty());
$this->assertEquals(1, \count($col2->getAttribute('attributes')));

$database->setTenant($tenant1);
$col1Again = $database->getCollection('multiTenantCol');
$this->assertFalse($col1Again->isEmpty());
Comment thread
abnegate marked this conversation as resolved.
Outdated

$database
->setSharedTables($sharedTables)
->setNamespace($namespace)
->setDatabase($schema);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

public function testSharedTablesMultiTenantCreate(): void
{
/** @var Database $database */
$database = $this->getDatabase();
$sharedTables = $database->getSharedTables();
$namespace = $database->getNamespace();
$schema = $database->getDatabase();

if (!$database->getAdapter()->getSupportForSchemas()) {
$this->expectNotToPerformAssertions();
return;
}

$dbName = 'stMultiCreate';
if ($database->exists($dbName)) {
$database->setDatabase($dbName)->delete();
}

$database
->setDatabase($dbName)
->setNamespace('')
->setSharedTables(true)
->setTenant(100)
->create();

$this->assertTrue($database->exists($dbName));

$database->setTenant(200);
$database->create();

$this->assertTrue($database->exists($dbName));

$database
->setSharedTables($sharedTables)
->setNamespace($namespace)
->setDatabase($schema);
}

public function testEvents(): void
{
$this->getDatabase()->getAuthorization()->skip(function () {
Expand Down
Loading