Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7074,12 +7074,24 @@ parameters:
count: 1
path: tests/Destinations/AppwriteLegacyIndexMetadataTest.php

-
message: '#^Parameter \#1 \$callback of function array_map expects \(callable\(mixed\)\: mixed\)\|null, Closure\(Throwable\)\: string given\.$#'
identifier: argument.type
count: 1
path: tests/Destinations/AppwriteOrphanSchemaDropTest.php

-
message: '#^Parameter \#2 \$values of static method Utopia\\Query\\Query\:\:equal\(\) expects array\<array\<mixed\>\|bool\|float\|int\|string\|null\>, array\<int, mixed\> given\.$#'
identifier: argument.type
count: 2
path: tests/Destinations/AppwriteLegacyIndexMetadataTest.php

-
message: '#^Parameter \#2 \$values of static method Utopia\\Query\\Query\:\:equal\(\) expects array\<array\<mixed\>\|bool\|float\|int\|string\|null\>, array\<int, mixed\> given\.$#'
identifier: argument.type
count: 2
path: tests/Destinations/AppwriteOrphanSchemaDropTest.php

-
message: '#^Possibly invalid array key type int\<0, max\>\|null\.$#'
identifier: offsetAccess.invalidOffset
Expand Down
28 changes: 25 additions & 3 deletions src/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\NotFound as NotFoundException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Format;
use Utopia\Database\Id;
Expand Down Expand Up @@ -1489,7 +1490,7 @@ protected function createIndex(Index $resource): bool
}

if ($action === SchemaAction::Overwrite) {
$dbForDatabases->deleteIndex($this->tableCollectionId($database, $table), $resource->getKey());
$this->dropFromSchema(fn () => $dbForDatabases->deleteIndex($this->tableCollectionId($database, $table), $resource->getKey()));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching orphan metadata still skips schema recreation

If a run stops after writing the metadata document but before creating the schema object, a retry with the same source spec takes the indexSpecMatches/attributeSpecMatches skip path before reaching this drop. The metadata then appears healthy, so the retry reports a skip while the schema object remains absent; the orphan cleanup sweep also leaves it alone because the source still emits that key. Consider checking schema presence before treating a matching metadata spec as a skip.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Destinations/Appwrite.php
Line: 1493

Comment:
**Matching orphan metadata still skips schema recreation**

If a run stops after writing the metadata document but before creating the schema object, a retry with the same source spec takes the `indexSpecMatches`/`attributeSpecMatches` skip path before reaching this drop. The metadata then appears healthy, so the retry reports a skip while the schema object remains absent; the orphan cleanup sweep also leaves it alone because the source still emits that key. Consider checking schema presence before treating a matching metadata spec as a skip.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · bug · Reply if this doesn't apply.

$this->dbForProject->deleteDocument(self::META_INDEXES, $indexMetaId);
$this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId());
// Reload $table — in-memory copy still holds the dropped index, so IndexValidator below would over-count.
Expand Down Expand Up @@ -1753,9 +1754,9 @@ private function dropAttributeForRecreate(
$isRelationship = $resource->getType() === Column::TYPE_RELATIONSHIP;

if ($isRelationship) {
$dbForDatabases->deleteRelationship($collectionId, $resource->getKey());
$this->dropFromSchema(fn () => $dbForDatabases->deleteRelationship($collectionId, $resource->getKey()));
} else {
$dbForDatabases->deleteAttribute($collectionId, $resource->getKey());
$this->dropFromSchema(fn () => $dbForDatabases->deleteAttribute($collectionId, $resource->getKey()));
}

$this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $attributeMetaId);
Expand Down Expand Up @@ -2369,6 +2370,27 @@ private function dropOrphanIndex(
$this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId());
}

/**
* Drops a column or index from the destination schema, treating "the schema no longer
* holds it" as the end state the drop asked for rather than an error.
*
* The meta document and the schema are two non-atomic writes, so a run killed between
* them — or a destination carrying an orphan left by the stuck-index bug fixed in
* appwrite/appwrite#15471 — leaves a meta document whose schema counterpart is gone.
* `NotFound` there used to abort the import and strand the orphan for good, since the
* caller never reached the meta delete that would have let the recreate through. Unlike
* {@see self::bestEffort()} this narrows to `NotFound`, so a dependent index
* (`Dependency`) or a relationship dropped as a column still surfaces.
*/
private function dropFromSchema(callable $drop): void
{
try {
$drop();
} catch (NotFoundException) {
// Already absent from the schema; the caller still removes the meta document.
}
}

/** Swallows deletion errors — a prior run or relationship cascade may have already removed the target. */
private function bestEffort(callable $fn): void
{
Expand Down
213 changes: 213 additions & 0 deletions tests/Destinations/AppwriteOrphanSchemaDropTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php

declare(strict_types=1);

namespace Utopia\Migration\Tests\Destinations;

use PHPUnit\Framework\TestCase;
use Utopia\Database\Database as UtopiaDatabase;
use Utopia\Database\Document as UtopiaDocument;
use Utopia\Database\Index as UtopiaIndex;
use Utopia\Migration\Destinations\OnDuplicate;
use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Database\Column;
use Utopia\Migration\Resources\Database\Columns\Integer;
use Utopia\Migration\Resources\Database\Columns\Text;
use Utopia\Migration\Resources\Database\Index;
use Utopia\Migration\Resources\Database\Table;
use Utopia\Query\Schema\ColumnType;

/**
* A meta document and its schema counterpart are two separate, non-atomic writes, so a
* destination can hold a meta document the schema no longer backs — from a run killed
* between the two, or from a project wedged by the stuck-index bug fixed in
* appwrite/appwrite#15471, where the index document outlived its schema index.
*
* An Overwrite migration drops the schema copy before recreating it. That drop must read
* "already absent" as the end state it asked for: otherwise the `NotFound` aborts the
* import before the meta delete that would have let the recreate through, and every retry
* takes the same path, so the orphan can never be reconciled through migration at all.
*/
final class AppwriteOrphanSchemaDropTest extends TestCase
{
use TransfersColumns;

private const string OLDER_UPDATED_AT = '2020-01-01T00:00:00.000+00:00';

private const string NEWER_UPDATED_AT = '2030-01-01T00:00:00.000+00:00';

private const string INDEX_KEY = 'idx_title_slug';

#[\Override]
protected function setUp(): void
{
parent::setUp();
self::registerSubqueryFilters();
}

public function testAnIndexTheSchemaNoLongerHoldsIsRecreated(): void
{
$database = $this->projectDatabase();

[$first] = $this->transferSchema(
fn (Table $table): array => [...$this->columns($table), $this->index($table, ['ASC', 'ASC'])],
$database,
OnDuplicate::Fail,
self::OLDER_UPDATED_AT,
);
$this->assertSame([], $this->errorMessages($first));
$this->assertNotNull($this->schemaIndex($database), 'The index must reach the schema before it is orphaned.');

$this->orphanIndex($database);

[$second, $resources] = $this->transferSchema(
fn (Table $table): array => [...$this->columns($table), $this->index($table, ['ASC', 'DESC'])],
$database,
OnDuplicate::Overwrite,
self::NEWER_UPDATED_AT,
);

$this->assertSame(
[],
$this->errorMessages($second),
'Dropping an index the schema no longer holds must not fail the import.',
);

$index = \end($resources);
$this->assertInstanceOf(Index::class, $index);
$this->assertSame(Resource::STATUS_SUCCESS, $index->getStatus());

$recreated = $this->schemaIndex($database);
$this->assertNotNull($recreated, 'The orphaned index must be recreated in the schema.');
$this->assertSame(
['ASC', 'DESC'],
\array_map(static fn (mixed $order): ?string => $order?->value, $recreated->orders),
'The recreated index must carry the source spec, not the orphaned one.',
);
$this->assertCount(1, $this->indexDocuments($database), 'The orphan must be replaced, not duplicated.');
}

public function testAColumnTheSchemaNoLongerHoldsIsRecreated(): void
{
$database = $this->projectDatabase();

[, $first] = $this->transferColumn(
static fn (Table $table): Column => new Text('title', $table, size: 100),
$database,
OnDuplicate::Fail,
self::OLDER_UPDATED_AT,
);
$this->assertSame([], $this->errorMessages($first));

$this->orphanColumn($database, 'title');

// A type change has no in-place primitive, so the overwrite goes through the drop.
[, $destination, $column] = $this->transferColumn(
static fn (Table $table): Column => new Integer('title', $table),
$database,
OnDuplicate::Overwrite,
self::NEWER_UPDATED_AT,
);

$this->assertSame(
[],
$this->errorMessages($destination),
'Dropping a column the schema no longer holds must not fail the import.',
);
$this->assertSame(Resource::STATUS_SUCCESS, $column->getStatus());
$this->assertSame(
ColumnType::Integer,
$this->physicalColumn($database, 'title')->type,
'The orphaned column must be recreated with the source type.',
);
}

/**
* Drops the schema copy of the index and leaves its meta document behind — the state
* the upstream stuck-index bug left on a project.
*/
private function orphanIndex(UtopiaDatabase $database): void
{
$collectionId = $this->tableCollectionId($database);
$database->getAuthorization()->skip(
static fn () => $database->deleteIndex($collectionId, self::INDEX_KEY),
);
$this->purgeTable($database);

$this->assertNull($this->schemaIndex($database), 'The orphan must have no schema counterpart.');
$this->assertCount(1, $this->indexDocuments($database), 'The orphan must keep its meta document.');
}

private function orphanColumn(UtopiaDatabase $database, string $key): void
{
$collectionId = $this->tableCollectionId($database);
$database->getAuthorization()->skip(
static fn () => $database->deleteAttribute($collectionId, $key),
);
$this->purgeTable($database);

$this->assertFalse(
$this->attributeDocument($database)->isEmpty(),
'The orphan must keep its meta document.',
);
}

/**
* @return list<Column>
*/
private function columns(Table $table): array
{
return [new Text('title', $table, size: 100), new Text('slug', $table, size: 100)];
}

/**
* @param list<string> $orders
*/
private function index(Table $table, array $orders): Index
{
return new Index(self::INDEX_KEY, self::INDEX_KEY, $table, 'key', ['title', 'slug'], [], $orders);
}

private function schemaIndex(UtopiaDatabase $database): ?UtopiaIndex
{
$collectionId = $this->tableCollectionId($database);
$collection = $database->getAuthorization()->skip(
static fn () => $database->getCollection($collectionId),
);

foreach ($collection->indexes() as $index) {
if ($index->key === self::INDEX_KEY) {
return $index;
}
}

return null;
}

/**
* @return array<UtopiaDocument>
*/
private function indexDocuments(UtopiaDatabase $database): array
{
return $database->getAuthorization()->skip(
static fn (): array => $database->find('indexes'),
);
}

private function tableCollectionId(UtopiaDatabase $database): string
{
$shop = $this->document($database, 'databases', 'shop');
$table = $this->document($database, 'database_'.$shop->getSequence(), 'products');

return 'database_'.$shop->getSequence().'_collection_'.$table->getSequence();
}

/** The table document caches its subqueried columns and indexes; a direct schema edit bypasses that. */
private function purgeTable(UtopiaDatabase $database): void
{
$shop = $this->document($database, 'databases', 'shop');
$database->getAuthorization()->skip(
static fn () => $database->purgeCachedDocument('database_'.$shop->getSequence(), 'products'),
);
}
}