mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
(fix): skip existing attributes in batch attribute creation
Re-running a migration whose attribute batch partially exists (e.g. 1.9.x installs where V24 already created deploymentRetention, startCommand, buildSpecification and runtimeSpecification) caused the whole createAttributes call to fail with Duplicate, so new attributes like providerBranches and providerPaths were never created. Filter out attributes that already exist on the live collection before batching, return early when nothing is left, and fall back to per-attribute creation if the batch still hits a duplicate.
This commit is contained in:
2 files changed
+93
-4
No files matched your search
@@ -317,7 +317,19 @@ abstract class Migration
|
||||
$attributes = $collection['attributes'];
|
||||
$attributeKeys = \array_column($collection['attributes'], '$id');
|
||||
|
||||
$database->purgeCachedCollection($collectionId);
|
||||
|
||||
$existingIds = \array_map(
|
||||
fn ($attribute) => $attribute->getId(),
|
||||
$database->getCollection($collectionId)->getAttribute('attributes', [])
|
||||
);
|
||||
|
||||
foreach ($attributeIds as $attributeId) {
|
||||
if (\in_array($attributeId, $existingIds)) {
|
||||
Console::warning("Skipping attribute \"{$attributeId}\" in collection {$collectionId}: Attribute already exists");
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributeKey = \array_search($attributeId, $attributeKeys);
|
||||
|
||||
if ($attributeKey === false) {
|
||||
@@ -334,10 +346,36 @@ abstract class Migration
|
||||
$attributesToCreate[] = $attribute;
|
||||
}
|
||||
|
||||
$database->createAttributes(
|
||||
collection: $collectionId,
|
||||
attributes: $attributesToCreate,
|
||||
);
|
||||
if (empty($attributesToCreate)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$database->createAttributes(
|
||||
collection: $collectionId,
|
||||
attributes: $attributesToCreate,
|
||||
);
|
||||
} catch (Duplicate) {
|
||||
foreach ($attributesToCreate as $attribute) {
|
||||
try {
|
||||
$database->createAttribute(
|
||||
collection: $collectionId,
|
||||
id: $attribute['$id'],
|
||||
type: $attribute['type'],
|
||||
size: $attribute['size'],
|
||||
required: $attribute['required'],
|
||||
default: $attribute['default'],
|
||||
signed: $attribute['signed'] ?? true,
|
||||
array: $attribute['array'] ?? false,
|
||||
format: $attribute['format'] ?? '',
|
||||
formatOptions: $attribute['formatOptions'] ?? [],
|
||||
filters: $attribute['filters'],
|
||||
);
|
||||
} catch (Duplicate) {
|
||||
Console::warning("Skipping attribute \"{$attribute['$id']}\" in collection {$collectionId}: Attribute already exists");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -132,4 +132,55 @@ final class MigrationVersionsTest extends TestCase
|
||||
$this->assertArrayHasKey('firstSeen', $attributes);
|
||||
$this->assertArrayHasKey('lastSeen', $attributes);
|
||||
}
|
||||
|
||||
public function testCreateAttributesFromCollectionSkipsExistingAttributes(): void
|
||||
{
|
||||
require_once __DIR__ . '/../../../app/init.php';
|
||||
|
||||
$authorization = new Authorization();
|
||||
$database = new Database(new Memory(), new Cache(new NoCache()));
|
||||
$database
|
||||
->setAuthorization($authorization)
|
||||
->setDatabase('migrationV24Functions')
|
||||
->setNamespace('migration_functions_' . \uniqid());
|
||||
$database->create();
|
||||
$database->createCollection('functions');
|
||||
|
||||
$migration = new V24();
|
||||
$migration->setProject(
|
||||
new Document(['$id' => 'project', '$sequence' => '1']),
|
||||
$database,
|
||||
$database,
|
||||
$authorization,
|
||||
);
|
||||
|
||||
$existing = [
|
||||
'deploymentRetention',
|
||||
'startCommand',
|
||||
'buildSpecification',
|
||||
'runtimeSpecification',
|
||||
];
|
||||
$new = [
|
||||
'providerBranches',
|
||||
'providerPaths',
|
||||
];
|
||||
|
||||
\ob_start();
|
||||
try {
|
||||
$migration->createAttributesFromCollection($database, 'functions', $existing);
|
||||
$migration->createAttributesFromCollection($database, 'functions', [...$existing, ...$new]);
|
||||
$migration->createAttributesFromCollection($database, 'functions', [...$existing, ...$new]);
|
||||
} finally {
|
||||
\ob_end_clean();
|
||||
}
|
||||
|
||||
$attributes = [];
|
||||
foreach ($database->getCollection('functions')->getAttribute('attributes', []) as $attribute) {
|
||||
$attributes[] = $attribute instanceof Document ? $attribute->getAttribute('$id') : ($attribute['$id'] ?? '');
|
||||
}
|
||||
|
||||
foreach ([...$existing, ...$new] as $id) {
|
||||
$this->assertContains($id, $attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user