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 <noreply@anthropic.com>
This commit is contained in:
Jake BarnbyandClaude Fable 5 committed 2026-07-21 01:29:48 +12:00
1 parent 1d94059ce3
commit 1544ad5bcc
8 files changed
+232 -172

No files matched your search

+2 -2
View File
@@ -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', '<')) {
+1 -1
View File
@@ -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';
+1
View File
@@ -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',
];
-162
View File
@@ -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;
}
}
+221
View File
@@ -0,0 +1,221 @@
<?php
namespace Appwrite\Migration\Version;
use Appwrite\Migration\Migration;
use Exception;
use Throwable;
use Utopia\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Migration\Resource;
class V26 extends Migration
{
/**
* @throws Throwable
*/
public function execute(): void
{
Console::info('Migrating collections');
$this->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;
}
}
+1 -1
View File
@@ -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) {
+1 -1
View File
@@ -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
@@ -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()
{
}