mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
(feat): expose migration destination generations
This commit is contained in:
1 parent
aa9be97ca4
commit
22800b14d9
11 files changed
+129
-9
No files matched your search
@@ -2333,6 +2333,17 @@ return [
|
||||
'array' => false,
|
||||
'filters' => [],
|
||||
],
|
||||
[
|
||||
'$id' => ID::custom('destinationResourceInternalId'),
|
||||
'type' => Database::VAR_STRING,
|
||||
'format' => '',
|
||||
'size' => Database::LENGTH_KEY,
|
||||
'signed' => true,
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'array' => false,
|
||||
'filters' => [],
|
||||
],
|
||||
[
|
||||
'$id' => ID::custom('destinationResourceType'),
|
||||
'type' => Database::VAR_STRING,
|
||||
@@ -2416,6 +2427,13 @@ return [
|
||||
'lengths' => [Database::LENGTH_KEY],
|
||||
'orders' => [Database::ORDER_ASC],
|
||||
],
|
||||
[
|
||||
'$id' => '_key_destinationResourceInternalId',
|
||||
'type' => Database::INDEX_KEY,
|
||||
'attributes' => ['destinationResourceInternalId'],
|
||||
'lengths' => [Database::LENGTH_KEY],
|
||||
'orders' => [Database::ORDER_ASC],
|
||||
],
|
||||
[
|
||||
'$id' => '_key_destinationResourceType',
|
||||
'type' => Database::INDEX_KEY,
|
||||
|
||||
@@ -117,6 +117,7 @@ class V25 extends Migration
|
||||
'parentResourceInternalId',
|
||||
'parentResourceType',
|
||||
'destinationResourceId',
|
||||
'destinationResourceInternalId',
|
||||
'destinationResourceType',
|
||||
];
|
||||
try {
|
||||
@@ -132,6 +133,7 @@ class V25 extends Migration
|
||||
'_key_parentResourceType',
|
||||
'_key_parentResourceInternalId',
|
||||
'_key_destinationResourceId',
|
||||
'_key_destinationResourceInternalId',
|
||||
'_key_destinationResourceType',
|
||||
];
|
||||
foreach ($indexes as $index) {
|
||||
@@ -178,7 +180,7 @@ class V25 extends Migration
|
||||
return $document;
|
||||
}
|
||||
|
||||
$internalIds = $this->resolveInternalIds($parentResourceId, $resourceId, $document->getId());
|
||||
$internalIds = $this->resolveInternalIds($parentResourceId, $resourceId, $document);
|
||||
if (
|
||||
$split
|
||||
&& (!isset($internalIds['parentResourceInternalId']) || !isset($internalIds['resourceInternalId']))
|
||||
@@ -216,15 +218,19 @@ class V25 extends Migration
|
||||
/**
|
||||
* @return array{parentResourceInternalId?: string, resourceInternalId?: string}
|
||||
*/
|
||||
protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array
|
||||
protected function resolveInternalIds(string $parentResourceId, string $resourceId, Document $migration): array
|
||||
{
|
||||
try {
|
||||
$database = $this->dbForProject->getDocument('databases', $parentResourceId);
|
||||
if ($database->isEmpty() || $database->getSequence() === '') {
|
||||
if (
|
||||
$database->isEmpty()
|
||||
|| $database->getSequence() === ''
|
||||
|| !$this->predatesMigration($database, $migration)
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
} catch (Throwable $th) {
|
||||
Console::warning("Failed to resolve parent internal ID for migration {$migrationId}: {$th->getMessage()}");
|
||||
Console::warning("Failed to resolve parent internal ID for migration {$migration->getId()}: {$th->getMessage()}");
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -234,13 +240,33 @@ class V25 extends Migration
|
||||
|
||||
try {
|
||||
$resource = $this->dbForProject->getDocument('database_' . $database->getSequence(), $resourceId);
|
||||
if (!$resource->isEmpty() && $resource->getSequence() !== '') {
|
||||
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 {$migrationId}: {$th->getMessage()}");
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -197,6 +197,7 @@ class Create extends Action
|
||||
'parentResourceInternalId' => $database->getSequence(),
|
||||
'parentResourceType' => $parentResourceType,
|
||||
'destinationResourceId' => $database->getId(),
|
||||
'destinationResourceInternalId' => $database->getSequence(),
|
||||
'destinationResourceType' => $parentResourceType,
|
||||
'statusCounters' => '{}',
|
||||
'resourceData' => '{}',
|
||||
|
||||
@@ -193,6 +193,7 @@ class Create extends Action
|
||||
'parentResourceInternalId' => $database->getSequence(),
|
||||
'parentResourceType' => $parentResourceType,
|
||||
'destinationResourceId' => $database->getId(),
|
||||
'destinationResourceInternalId' => $database->getSequence(),
|
||||
'destinationResourceType' => $parentResourceType,
|
||||
'statusCounters' => '{}',
|
||||
'resourceData' => '{}',
|
||||
|
||||
@@ -11,10 +11,13 @@ class Migrations extends Base
|
||||
'destination',
|
||||
'resources',
|
||||
'resourceId',
|
||||
'resourceInternalId',
|
||||
'resourceType',
|
||||
'parentResourceId',
|
||||
'parentResourceInternalId',
|
||||
'parentResourceType',
|
||||
'destinationResourceId',
|
||||
'destinationResourceInternalId',
|
||||
'destinationResourceType',
|
||||
'statusCounters',
|
||||
'resourceData',
|
||||
|
||||
@@ -36,6 +36,7 @@ class V27 extends Filter
|
||||
unset($content['parentResourceInternalId']);
|
||||
unset($content['parentResourceType']);
|
||||
unset($content['destinationResourceId']);
|
||||
unset($content['destinationResourceInternalId']);
|
||||
unset($content['destinationResourceType']);
|
||||
|
||||
return $content;
|
||||
|
||||
@@ -109,6 +109,13 @@ class Migration extends Model
|
||||
'example' => 'databaseId',
|
||||
'array' => false
|
||||
])
|
||||
->addRule('destinationResourceInternalId', [
|
||||
'type' => self::TYPE_STRING,
|
||||
'description' => 'Internal ID of the destination resource created or overwritten by the migration.',
|
||||
'default' => '',
|
||||
'example' => '1',
|
||||
'array' => false
|
||||
])
|
||||
->addRule('destinationResourceType', [
|
||||
'type' => self::TYPE_STRING,
|
||||
'description' => 'Type of the destination resource created or overwritten by the migration.',
|
||||
|
||||
@@ -3855,8 +3855,10 @@ trait MigrationsBase
|
||||
$this->assertNotNull($migration);
|
||||
$this->assertSame($data['tableId'], $migration['resourceId']);
|
||||
$this->assertSame($databaseId, $migration['parentResourceId']);
|
||||
$this->assertNotSame('', $migration['parentResourceInternalId']);
|
||||
$this->assertSame(Resource::TYPE_DATABASE, $migration['parentResourceType']);
|
||||
$this->assertSame($databaseId, $migration['destinationResourceId']);
|
||||
$this->assertSame($migration['parentResourceInternalId'], $migration['destinationResourceInternalId']);
|
||||
$this->assertSame(Resource::TYPE_DATABASE, $migration['destinationResourceType']);
|
||||
|
||||
$response = $this->client->call(Client::METHOD_GET, '/migrations', array_merge([
|
||||
@@ -3864,7 +3866,7 @@ trait MigrationsBase
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'queries' => [
|
||||
Query::equal('destinationResourceId', [$databaseId])->toString(),
|
||||
Query::equal('destinationResourceInternalId', [$migration['destinationResourceInternalId']])->toString(),
|
||||
Query::equal('destinationResourceType', [Resource::TYPE_DATABASE])->toString(),
|
||||
],
|
||||
]);
|
||||
|
||||
@@ -24,7 +24,7 @@ final class V25Test extends TestCase
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array
|
||||
protected function resolveInternalIds(string $parentResourceId, string $resourceId, Document $migration): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
@@ -75,7 +75,7 @@ final class V25Test extends TestCase
|
||||
* @return array{parentResourceInternalId?: string, resourceInternalId?: string}
|
||||
*/
|
||||
#[\Override]
|
||||
protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array
|
||||
protected function resolveInternalIds(string $parentResourceId, string $resourceId, Document $migration): array
|
||||
{
|
||||
return $this->internalIds;
|
||||
}
|
||||
@@ -116,4 +116,29 @@ final class V25Test extends TestCase
|
||||
$this->assertSame(Resource::TYPE_DATABASE, $document->getAttribute('parentResourceType'));
|
||||
$this->assertSame($afterResolution, $document->getArrayCopy());
|
||||
}
|
||||
|
||||
public function testRejectsResourcesCreatedAfterMigration(): void
|
||||
{
|
||||
$migration = new class () extends V25 {
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function isCandidate(Document $resource, Document $migration): bool
|
||||
{
|
||||
return $this->predatesMigration($resource, $migration);
|
||||
}
|
||||
};
|
||||
$document = new Document([
|
||||
'$createdAt' => '2026-01-02T00:00:00.000+00:00',
|
||||
]);
|
||||
|
||||
$this->assertTrue($migration->isCandidate(new Document([
|
||||
'$createdAt' => '2026-01-01T00:00:00.000+00:00',
|
||||
]), $document));
|
||||
$this->assertFalse($migration->isCandidate(new Document([
|
||||
'$createdAt' => '2026-01-03T00:00:00.000+00:00',
|
||||
]), $document));
|
||||
$this->assertFalse($migration->isCandidate(new Document(), $document));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries\Migrations;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Database\Query;
|
||||
|
||||
final class MigrationsTest extends TestCase
|
||||
{
|
||||
public function testCanonicalResourceFieldsCanBeQueried(): void
|
||||
{
|
||||
$validator = new Migrations();
|
||||
|
||||
foreach ([
|
||||
'resourceId',
|
||||
'resourceInternalId',
|
||||
'resourceType',
|
||||
'parentResourceId',
|
||||
'parentResourceInternalId',
|
||||
'parentResourceType',
|
||||
'destinationResourceId',
|
||||
'destinationResourceInternalId',
|
||||
'destinationResourceType',
|
||||
] as $attribute) {
|
||||
$query = Query::equal($attribute, ['value']);
|
||||
|
||||
$this->assertTrue($validator->isValid([$query]), $validator->getDescription());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ final class V27Test extends TestCase
|
||||
'parentResourceInternalId' => '10',
|
||||
'parentResourceType' => 'database',
|
||||
'destinationResourceId' => 'destination',
|
||||
'destinationResourceInternalId' => '30',
|
||||
'destinationResourceType' => 'database',
|
||||
], Response::MODEL_MIGRATION);
|
||||
|
||||
@@ -46,6 +47,7 @@ final class V27Test extends TestCase
|
||||
'parentResourceInternalId' => '',
|
||||
'parentResourceType' => '',
|
||||
'destinationResourceId' => '',
|
||||
'destinationResourceInternalId' => '',
|
||||
'destinationResourceType' => '',
|
||||
], Response::MODEL_MIGRATION);
|
||||
|
||||
@@ -69,6 +71,7 @@ final class V27Test extends TestCase
|
||||
'parentResourceId' => 'database',
|
||||
'parentResourceType' => 'database',
|
||||
'destinationResourceId' => 'destination',
|
||||
'destinationResourceInternalId' => '30',
|
||||
'destinationResourceType' => 'database',
|
||||
]],
|
||||
], Response::MODEL_MIGRATION_LIST);
|
||||
|
||||
Reference in new issue
Block a user