Skip to content

Commit 3f91272

Browse files
committed
Fix double metdata when migrating
1 parent 8e6a033 commit 3f91272

3 files changed

Lines changed: 65 additions & 8 deletions

File tree

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/Database.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,7 +2047,11 @@ public function createAttribute(string $collection, string $id, string $type, in
20472047
}
20482048
}
20492049

2050-
$collection->setAttribute('attributes', $attribute, Document::SET_TYPE_APPEND);
2050+
// Append attribute, removing any duplicates
2051+
$attributes = $collection->getAttribute('attributes', []);
2052+
$attributes = \array_filter($attributes, fn ($attr) => $attr['$id'] !== $id);
2053+
$attributes[] = $attribute;
2054+
$collection->setAttribute('attributes', \array_values($attributes));
20512055

20522056
$this->updateMetadata(
20532057
collection: $collection,
@@ -2169,10 +2173,17 @@ public function createAttributes(string $collection, array $attributes): bool
21692173
}
21702174
}
21712175

2176+
// Append attribute, removing any duplicates
2177+
$attributes = $collection->getAttribute('attributes', []);
2178+
$newAttributeIds = \array_map(fn ($attr) => $attr['$id'], $attributeDocuments);
2179+
$attributes = \array_filter($attributes, fn ($attr) => !\in_array($attr['$id'], $newAttributeIds));
2180+
21722181
foreach ($attributeDocuments as $attributeDocument) {
2173-
$collection->setAttribute('attributes', $attributeDocument, Document::SET_TYPE_APPEND);
2182+
$attributes[] = $attributeDocument;
21742183
}
21752184

2185+
$collection->setAttribute('attributes', \array_values($attributes));
2186+
21762187
$this->updateMetadata(
21772188
collection: $collection,
21782189
rollbackOperation: fn () => $this->cleanupAttributes($collection->getId(), $attributeDocuments),

tests/e2e/Adapter/Scopes/AttributeTests.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,4 +2195,50 @@ public function testCreateAttributesDelete(): void
21952195
$this->assertCount(1, $attrs);
21962196
$this->assertEquals('b', $attrs[0]['$id']);
21972197
}
2198+
2199+
public function testCreateAttributeWhileMigrating(): void
2200+
{
2201+
/** @var Database $database */
2202+
$database = $this->getDatabase();
2203+
2204+
// Skip test if adapter doesn't support shared tables
2205+
if (!$database->getAdapter()->getSharedTables()) {
2206+
$this->expectNotToPerformAssertions();
2207+
return;
2208+
}
2209+
2210+
// Prepare collection
2211+
$database->createCollection('migration_test');
2212+
$database->setMigrating(true);
2213+
2214+
// First creation, as usual
2215+
$this->assertTrue($database->createAttribute('migration_test', 'status', Database::VAR_STRING, 128, true));
2216+
2217+
$collection = $database->getCollection('migration_test');
2218+
$attributes = $collection->getAttribute('attributes');
2219+
$this->assertCount(1, $attributes);
2220+
$this->assertEquals('status', $attributes[0]['$id']);
2221+
2222+
// Second creation, no exceptions, no duplicates
2223+
$result = $database->createAttribute('migration_test', 'status', Database::VAR_STRING, 128, true);
2224+
$this->assertTrue($result);
2225+
2226+
$collection = $database->getCollection('migration_test');
2227+
$attributes = $collection->getAttribute('attributes');
2228+
$this->assertCount(1, $attributes);
2229+
$this->assertEquals('status', $attributes[0]['$id']);
2230+
2231+
// Third creation, same as second, once more, just in case
2232+
$result = $database->createAttribute('migration_test', 'status', Database::VAR_STRING, 128, true);
2233+
$this->assertTrue($result);
2234+
2235+
$collection = $database->getCollection('migration_test');
2236+
$attributes = $collection->getAttribute('attributes');
2237+
$this->assertCount(1, $attributes);
2238+
$this->assertEquals('status', $attributes[0]['$id']);
2239+
2240+
// Cleanup
2241+
$database->setMigrating(false);
2242+
$database->deleteCollection('migration_test');
2243+
}
21982244
}

0 commit comments

Comments
 (0)