From 1544ad5bcc3a6030d6b26822a08fb3f4fd75c617 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 21 Jul 2026 01:29:14 +1200 Subject: [PATCH] fix(migrations): ship resource links under 1.9.6, not 2.0.0 The release carrying this feature is 1.9.6, so the backfill moves out of 2.0.0's V25 into its own V26 migration keyed to 1.9.6, the V27 request/response filters gate at 1.9.6, and APP_VERSION_STABLE bumps to match. V25 returns to main's contents. Co-Authored-By: Claude Fable 5 --- app/controllers/general.php | 4 +- app/init/constants.php | 2 +- src/Appwrite/Migration/Migration.php | 1 + src/Appwrite/Migration/Version/V25.php | 162 ------------- src/Appwrite/Migration/Version/V26.php | 221 ++++++++++++++++++ src/Appwrite/Utopia/Request/Filters/V27.php | 2 +- src/Appwrite/Utopia/Response/Filters/V27.php | 2 +- .../Version/{V25Test.php => V26Test.php} | 10 +- 8 files changed, 232 insertions(+), 172 deletions(-) create mode 100644 src/Appwrite/Migration/Version/V26.php rename tests/unit/Migration/Version/{V25Test.php => V26Test.php} (96%) diff --git a/app/controllers/general.php b/app/controllers/general.php index 18f252458e..47b4ecd097 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -919,7 +919,7 @@ Http::init() if (version_compare($requestFormat, '1.9.5', '<')) { $request->addFilter(new RequestV26()); } - if (version_compare($requestFormat, '2.0.0', '<')) { + if (version_compare($requestFormat, '1.9.6', '<')) { $request->addFilter(new RequestV27()); } } @@ -946,7 +946,7 @@ Http::init() */ $responseFormat = $request->getHeaderLine('x-appwrite-response-format', System::getEnv('_APP_SYSTEM_RESPONSE_FORMAT', '')); if ($responseFormat) { - if (version_compare($responseFormat, '2.0.0', '<')) { + if (version_compare($responseFormat, '1.9.6', '<')) { $response->addFilter(new ResponseV27()); } if (version_compare($responseFormat, '1.9.5', '<')) { diff --git a/app/init/constants.php b/app/init/constants.php index f5eaa3a4ab..88da25bcbc 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -52,7 +52,7 @@ const APP_RESOURCE_TOKEN_ACCESS = 24 * 60 * 60; // 24 hours const APP_FILE_ACCESS = 24 * 60 * 60; // 24 hours const APP_CACHE_UPDATE = 24 * 60 * 60; // 24 hours const APP_CACHE_BUSTER = 4326; -const APP_VERSION_STABLE = '1.9.5'; +const APP_VERSION_STABLE = '1.9.6'; const APP_DATABASE_ATTRIBUTE_EMAIL = 'email'; const APP_DATABASE_ATTRIBUTE_ENUM = 'enum'; const APP_DATABASE_ATTRIBUTE_IP = 'ip'; diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index b37846b642..73a5a2522d 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -98,6 +98,7 @@ abstract class Migration '1.9.3' => 'V24', '1.9.4' => 'V24', '1.9.5' => 'V24', + '1.9.6' => 'V26', '2.0.0' => 'V25', ]; diff --git a/src/Appwrite/Migration/Version/V25.php b/src/Appwrite/Migration/Version/V25.php index 05b1142be2..4aa05673c1 100644 --- a/src/Appwrite/Migration/Version/V25.php +++ b/src/Appwrite/Migration/Version/V25.php @@ -9,7 +9,6 @@ use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; -use Utopia\Migration\Resource; class V25 extends Migration { @@ -20,9 +19,6 @@ class V25 extends Migration { Console::info('Migrating collections'); $this->migrateCollections(); - - Console::info('Migrating documents'); - $this->forEachDocument($this->migrateDocument(...)); } /** @@ -108,165 +104,7 @@ class V25 extends Migration } } break; - - case 'migrations': - if ($collectionType === 'projects') { - $attributes = [ - 'resourceInternalId', - 'parentResourceId', - 'parentResourceInternalId', - 'parentResourceType', - 'destinationResourceId', - 'destinationResourceInternalId', - 'destinationResourceType', - ]; - try { - $this->createAttributesFromCollection($this->dbForProject, $id, $attributes); - } catch (Throwable $th) { - Console::warning('Failed to create attributes "' . \implode(', ', $attributes) . "\" in collection {$id}: {$th->getMessage()}"); - } - - $indexes = [ - '_key_resourceType', - '_key_resourceInternalId', - '_key_parentResourceId', - '_key_parentResourceType', - '_key_parentResourceInternalId', - '_key_destinationResourceId', - '_key_destinationResourceInternalId', - '_key_destinationResourceType', - ]; - foreach ($indexes as $index) { - try { - $this->createIndexFromCollection($this->dbForProject, $id, $index); - } catch (Throwable $th) { - Console::warning("Failed to create index \"{$index}\" from {$id}: {$th->getMessage()}"); - } - } - - $this->dbForProject->purgeCachedCollection($id); - } - break; } } } - - protected function migrateDocument(Document $document): Document - { - if ($document->getCollection() !== 'migrations') { - return $document; - } - - $resourceId = (string) $document->getAttribute('resourceId', ''); - $parentResourceId = (string) $document->getAttribute('parentResourceId', ''); - $parentResourceType = ''; - $split = false; - - if ($parentResourceId === '') { - if (!\str_contains($resourceId, ':')) { - return $document; - } - - [$parentResourceId, $resourceId] = \explode(':', $resourceId, 2); - if ($parentResourceId === '' || $resourceId === '') { - return $document; - } - - $parentResourceType = (string) $document->getAttribute('resourceType', ''); - $split = true; - } - - if ($resourceId === '') { - return $document; - } - - $internalIds = $this->resolveInternalIds($parentResourceId, $resourceId, $document); - if ( - $split - && (!isset($internalIds['parentResourceInternalId']) || !isset($internalIds['resourceInternalId'])) - ) { - return $document; - } - - if ($split) { - $document - ->setAttribute('resourceId', $resourceId) - ->setAttribute('resourceType', Resource::TYPE_COLLECTION) - ->setAttribute('parentResourceId', $parentResourceId); - - if ($parentResourceType !== '') { - $document->setAttribute('parentResourceType', $parentResourceType); - } - } - - if ( - (string) $document->getAttribute('parentResourceInternalId', '') === '' - && isset($internalIds['parentResourceInternalId']) - ) { - $document->setAttribute('parentResourceInternalId', $internalIds['parentResourceInternalId']); - } - if ( - (string) $document->getAttribute('resourceInternalId', '') === '' - && isset($internalIds['resourceInternalId']) - ) { - $document->setAttribute('resourceInternalId', $internalIds['resourceInternalId']); - } - - return $document; - } - - /** - * @return array{parentResourceInternalId?: string, resourceInternalId?: string} - */ - protected function resolveInternalIds(string $parentResourceId, string $resourceId, Document $migration): array - { - try { - $database = $this->dbForProject->getDocument('databases', $parentResourceId); - if ( - $database->isEmpty() - || $database->getSequence() === '' - || !$this->predatesMigration($database, $migration) - ) { - return []; - } - } catch (Throwable $th) { - Console::warning("Failed to resolve parent internal ID for migration {$migration->getId()}: {$th->getMessage()}"); - return []; - } - - $internalIds = [ - 'parentResourceInternalId' => (string) $database->getSequence(), - ]; - - try { - $resource = $this->dbForProject->getDocument('database_' . $database->getSequence(), $resourceId); - if ( - !$resource->isEmpty() - && $resource->getSequence() !== '' - && $this->predatesMigration($resource, $migration) - ) { - $internalIds['resourceInternalId'] = (string) $resource->getSequence(); - } - } catch (Throwable $th) { - Console::warning("Failed to resolve resource internal ID for migration {$migration->getId()}: {$th->getMessage()}"); - } - - return $internalIds; - } - - /** - * A resource created after the migration is a reused public ID, not the - * generation the historical migration operated on. - */ - protected function predatesMigration(Document $resource, Document $migration): bool - { - $resourceCreatedAt = \strtotime($resource->getCreatedAt()); - $migrationCreatedAt = \strtotime($migration->getCreatedAt()); - - if ($resourceCreatedAt === false || $migrationCreatedAt === false) { - return false; - } - - return $resourceCreatedAt <= $migrationCreatedAt; - } } diff --git a/src/Appwrite/Migration/Version/V26.php b/src/Appwrite/Migration/Version/V26.php new file mode 100644 index 0000000000..3f5b155453 --- /dev/null +++ b/src/Appwrite/Migration/Version/V26.php @@ -0,0 +1,221 @@ +migrateCollections(); + + Console::info('Migrating documents'); + $this->forEachDocument($this->migrateDocument(...)); + } + + /** + * Migrate Collections. + * + * @return void + * @throws Exception|Throwable + */ + private function migrateCollections(): void + { + $projectInternalId = $this->project->getSequence(); + + if (empty($projectInternalId)) { + throw new Exception('Project ID is null'); + } + + $collectionType = match ($projectInternalId) { + 'console' => 'console', + default => 'projects', + }; + + $collections = $this->collections[$collectionType]; + + foreach ($collections as $collection) { + $id = $collection['$id']; + + if (empty($id)) { + continue; + } + + Console::log("Migrating collection \"{$id}\""); + + $this->dbForProject->purgeCachedCollection($id); + $this->dbForProject->purgeCachedDocument(Database::METADATA, $id); + + switch ($id) { + case 'migrations': + if ($collectionType === 'projects') { + $attributes = [ + 'resourceInternalId', + 'parentResourceId', + 'parentResourceInternalId', + 'parentResourceType', + 'destinationResourceId', + 'destinationResourceInternalId', + 'destinationResourceType', + ]; + try { + $this->createAttributesFromCollection($this->dbForProject, $id, $attributes); + } catch (Throwable $th) { + Console::warning('Failed to create attributes "' . \implode(', ', $attributes) . "\" in collection {$id}: {$th->getMessage()}"); + } + + $indexes = [ + '_key_resourceType', + '_key_resourceInternalId', + '_key_parentResourceId', + '_key_parentResourceType', + '_key_parentResourceInternalId', + '_key_destinationResourceId', + '_key_destinationResourceInternalId', + '_key_destinationResourceType', + ]; + foreach ($indexes as $index) { + try { + $this->createIndexFromCollection($this->dbForProject, $id, $index); + } catch (Throwable $th) { + Console::warning("Failed to create index \"{$index}\" from {$id}: {$th->getMessage()}"); + } + } + + $this->dbForProject->purgeCachedCollection($id); + } + break; + } + } + } + + protected function migrateDocument(Document $document): Document + { + if ($document->getCollection() !== 'migrations') { + return $document; + } + + $resourceId = (string) $document->getAttribute('resourceId', ''); + $parentResourceId = (string) $document->getAttribute('parentResourceId', ''); + $parentResourceType = ''; + $split = false; + + if ($parentResourceId === '') { + if (!\str_contains($resourceId, ':')) { + return $document; + } + + [$parentResourceId, $resourceId] = \explode(':', $resourceId, 2); + if ($parentResourceId === '' || $resourceId === '') { + return $document; + } + + $parentResourceType = (string) $document->getAttribute('resourceType', ''); + $split = true; + } + + if ($resourceId === '') { + return $document; + } + + $internalIds = $this->resolveInternalIds($parentResourceId, $resourceId, $document); + if ( + $split + && (!isset($internalIds['parentResourceInternalId']) || !isset($internalIds['resourceInternalId'])) + ) { + return $document; + } + + if ($split) { + $document + ->setAttribute('resourceId', $resourceId) + ->setAttribute('resourceType', Resource::TYPE_COLLECTION) + ->setAttribute('parentResourceId', $parentResourceId); + + if ($parentResourceType !== '') { + $document->setAttribute('parentResourceType', $parentResourceType); + } + } + + if ( + (string) $document->getAttribute('parentResourceInternalId', '') === '' + && isset($internalIds['parentResourceInternalId']) + ) { + $document->setAttribute('parentResourceInternalId', $internalIds['parentResourceInternalId']); + } + if ( + (string) $document->getAttribute('resourceInternalId', '') === '' + && isset($internalIds['resourceInternalId']) + ) { + $document->setAttribute('resourceInternalId', $internalIds['resourceInternalId']); + } + + return $document; + } + + /** + * @return array{parentResourceInternalId?: string, resourceInternalId?: string} + */ + protected function resolveInternalIds(string $parentResourceId, string $resourceId, Document $migration): array + { + try { + $database = $this->dbForProject->getDocument('databases', $parentResourceId); + if ( + $database->isEmpty() + || $database->getSequence() === '' + || !$this->predatesMigration($database, $migration) + ) { + return []; + } + } catch (Throwable $th) { + Console::warning("Failed to resolve parent internal ID for migration {$migration->getId()}: {$th->getMessage()}"); + return []; + } + + $internalIds = [ + 'parentResourceInternalId' => (string) $database->getSequence(), + ]; + + try { + $resource = $this->dbForProject->getDocument('database_' . $database->getSequence(), $resourceId); + if ( + !$resource->isEmpty() + && $resource->getSequence() !== '' + && $this->predatesMigration($resource, $migration) + ) { + $internalIds['resourceInternalId'] = (string) $resource->getSequence(); + } + } catch (Throwable $th) { + Console::warning("Failed to resolve resource internal ID for migration {$migration->getId()}: {$th->getMessage()}"); + } + + return $internalIds; + } + + /** + * A resource created after the migration is a reused public ID, not the + * generation the historical migration operated on. + */ + protected function predatesMigration(Document $resource, Document $migration): bool + { + $resourceCreatedAt = \strtotime($resource->getCreatedAt()); + $migrationCreatedAt = \strtotime($migration->getCreatedAt()); + + if ($resourceCreatedAt === false || $migrationCreatedAt === false) { + return false; + } + + return $resourceCreatedAt <= $migrationCreatedAt; + } +} diff --git a/src/Appwrite/Utopia/Request/Filters/V27.php b/src/Appwrite/Utopia/Request/Filters/V27.php index 3b4c427732..58baf8a1a5 100644 --- a/src/Appwrite/Utopia/Request/Filters/V27.php +++ b/src/Appwrite/Utopia/Request/Filters/V27.php @@ -6,7 +6,7 @@ use Appwrite\Utopia\Request\Filter; class V27 extends Filter { - // Convert 1.9.5 params to 2.0.0 + // Convert 1.9.5 params to 1.9.6 public function parse(array $content, string $model): array { return match ($model) { diff --git a/src/Appwrite/Utopia/Response/Filters/V27.php b/src/Appwrite/Utopia/Response/Filters/V27.php index e0cf53ea3f..d377536488 100644 --- a/src/Appwrite/Utopia/Response/Filters/V27.php +++ b/src/Appwrite/Utopia/Response/Filters/V27.php @@ -5,7 +5,7 @@ namespace Appwrite\Utopia\Response\Filters; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Filter; -// Convert 2.0.0 Data format to 1.9.5 format +// Convert 1.9.6 Data format to 1.9.5 format class V27 extends Filter { public function parse(array $content, string $model): array diff --git a/tests/unit/Migration/Version/V25Test.php b/tests/unit/Migration/Version/V26Test.php similarity index 96% rename from tests/unit/Migration/Version/V25Test.php rename to tests/unit/Migration/Version/V26Test.php index fadb5609e0..4b8059e41a 100644 --- a/tests/unit/Migration/Version/V25Test.php +++ b/tests/unit/Migration/Version/V26Test.php @@ -4,16 +4,16 @@ declare(strict_types=1); namespace Tests\Unit\Migration\Version; -use Appwrite\Migration\Version\V25; +use Appwrite\Migration\Version\V26; use PHPUnit\Framework\TestCase; use Utopia\Database\Document; use Utopia\Migration\Resource; -final class V25Test extends TestCase +final class V26Test extends TestCase { public function testPreservesLegacyResourceWhenInternalIdsCannotBeResolved(): void { - $migration = new class () extends V25 { + $migration = new class () extends V26 { public function __construct() { } @@ -50,7 +50,7 @@ final class V25Test extends TestCase public function testRerunResolvesMissingInternalIdsIdempotently(): void { - $migration = new class () extends V25 { + $migration = new class () extends V26 { /** * @var array{parentResourceInternalId?: string, resourceInternalId?: string} */ @@ -123,7 +123,7 @@ final class V25Test extends TestCase public function testRejectsResourcesCreatedAfterMigration(): void { - $migration = new class () extends V25 { + $migration = new class () extends V26 { public function __construct() { }