Skip to content

fix(destination): drop a schema object the destination no longer holds (HS-1472155) - #241

Closed
claudear wants to merge 1 commit into
mainfrom
fix/orphan-schema-drop-hs-1472155
Closed

claudear wants to merge 1 commit into
mainfrom
fix/orphan-schema-drop-hs-1472155

Conversation

@claudear

Copy link
Copy Markdown
Contributor

Dependency cascade from appwrite/appwrite#15471

No manifest bump is needed

The appwrite/appwrite Composer constraint here resolves to appwrite/sdk-for-php, not the server monorepo. Upstream #15471 changed only src/Appwrite/Platform/Modules/Databases/Workers/Databases.php and the server e2e suite — no SDK surface, so no Packagist release followed it. The latest SDK is 32.4.0 (2026-11-16, before that PR merged on 2026-11-19) and the existing ^32.3.0 constraint already resolves to it. composer.json is therefore unchanged.

What does need adapting

src/Destinations/Appwrite.php does not go through the SDK for schema writes — it reimplements the server worker's logic directly against utopia-php/database. So the upstream root cause has a real counterpart here.

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:

  • a run killed between createDocument(META_INDEXES) and $dbForDatabases->createIndex() (the existing catch covers thrown errors, not process death), or
  • a destination carrying an orphan from the very bug #15471 fixed, where DELETE .../indexes/{key} answered 204, the worker set status=stuck, and the indexes document was never removed.

An Overwrite migration drops the schema copy before recreating it. Utopia\Database's deleteIndex() / deleteAttribute() throw NotFound when the key is absent from the collection metadata (verified in the resolved utopia-php/database 8.3.15), and that aborted the import before the meta delete that would have let the recreate through. Every retry took the same path, so the orphan could not be reconciled through migration at all — the same wedge as the ticket, on the destination side.

Note the asymmetry this closes: the orphan cleanup sweep already wrapped deleteIndex in bestEffort, but that sweep only visits destination keys absent from the source. An orphan whose key the source still emits falls to the Overwrite path, which was unguarded.

Changes

  • New dropFromSchema() helper: treats "the schema no longer holds it" as the end state the drop asked for. Unlike the neighbouring bestEffort() it narrows to NotFound, so a dependent index (Dependency) or a relationship dropped as a column still surfaces as an error.
  • Applied to both drop-before-recreate paths — createIndex()'s SchemaAction::Overwrite branch (the direct mirror of the upstream fix) and dropAttributeForRecreate(). Upstream only had to fix the index path because the server's attribute path already caught NotFound; here both were unguarded, and the PR description calls that attribute behaviour the correct reference.

The second upstream bug (pruning lengths/orders by value with array_diff instead of positionally) has no counterpart here — migration never prunes an index's column list; it writes the source spec wholesale.

Tests

tests/Destinations/AppwriteOrphanSchemaDropTest.php, two failing-first tests on the existing in-memory harness. Each builds the orphan the honest way — transfer the schema, then drop only the schema copy via the real deleteIndex()/deleteAttribute(), leaving the meta document — then re-runs as Overwrite:

  • testAnIndexTheSchemaNoLongerHoldsIsRecreated — the re-run changes the orders so the spec cannot match into a Skip. Asserts the import reports no error, the index is back in the schema carrying the new spec (['ASC','DESC']), and the orphan was replaced rather than duplicated.
  • testAColumnTheSchemaNoLongerHoldsIsRecreated — a type change, which has no in-place primitive and so is forced through the drop.

Both assert their own orphan precondition so they cannot silently stop testing the branch.

Without the fix they fail with exactly the ticket's symptoms:

1) testAnIndexTheSchemaNoLongerHoldsIsRecreated
   -Array &0 []
   +Array &0 [ 0 => 'Index not found' ]

2) testAColumnTheSchemaNoLongerHoldsIsRecreated
   -Array &0 []
   +Array &0 [ 0 => 'Attribute not found' ]

Verification

Local PHP is 8.4 and this package's suite runs from the monorepo toolchain, so everything below ran in a php:8.5-cli container with deps installed from this tree:

  • phpunit --testsuite unit → OK, 227 tests, 1144 assertions, 2 skipped (225 before these two).
  • phpstan analyse (level max) → output identical to clean HEAD; the 5 remaining errors are pre-existing drift between the committed baseline and the phpstan build used here, untouched by this change.
  • phpstan-baseline.neon gains the two TransfersColumns trait-context entries that every sibling test class using that trait already has (the baseline keys them per using-class path). The three errors in the new file itself were fixed rather than baselined.
  • rector is a no-op here — rector.php skips both src and tests.

Heads up on routing

This package's canonical source is now packages/migration in the monorepo, and this repo's only workflow redirects PRs to appwrite/appwrite, so the mirror bot will likely redirect this one. Raised here because this is the repository the task targeted; happy to re-raise it against the monorepo instead.

🤖 Generated with Claude Code

…s (HS-1472155)

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 writes, or from a project
wedged by the stuck-index bug fixed upstream in appwrite/appwrite#15471,
where the `indexes` document outlived its schema index.

An Overwrite migration drops the schema copy before recreating it.
`Utopia\Database`'s `deleteIndex()`/`deleteAttribute()` throw `NotFound`
when the key is absent from the collection metadata, and that aborted
the import *before* the meta delete that would have let the recreate
through — so every retry took the same path and the orphan could never
be reconciled through migration at all.

Both drop-before-recreate paths now treat "the schema no longer holds
it" as the end state the drop asked for, mirroring the upstream fix.
The guard narrows to `NotFound`, so a dependent index or a relationship
dropped as a column still surfaces as an error.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@appwrite

appwrite Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Code review

Commit Status Tier Verdict Details
0f290af Reviewed Reviewed 🔵 A · Mergeable after minor fixes 💬 Commented View review

The new drop behavior is useful, but a retry with an unchanged spec can still leave a missing schema object unrepaired.

Adds NotFound-tolerant schema drops to overwrite recreation for indexes and attributes, with in-memory regression tests for orphaned schema entries. It also adds the PHPStan baseline entries associated with the new test class.

Note

Not approving while a bug finding is open: Matching orphan metadata still skips schema recreation

Findings (1)
Finding Where
🟡 Matching orphan metadata still skips schema recreation src/Destinations/Appwrite.php:1493
Fix with agent prompt
### Issue 1
src/Destinations/Appwrite.php:1493
**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.
Walkthrough (3)
File Change
phpstan-baseline.neon Adds PHPStan baseline entries for the new regression test.
src/Destinations/Appwrite.php Adds a NotFound-specific schema-drop helper and uses it in index and attribute recreation.
tests/Destinations/AppwriteOrphanSchemaDropTest.php Adds regression coverage for recreating orphaned indexes and columns during overwrite.

Reviewed 0f290af

Tip

Comment @appwrite review to re-run, or mention @appwrite with a question.

@github-actions

Copy link
Copy Markdown

Thanks for contributing! This repository is a read-only mirror; development for this library happens in packages/migration in appwrite/appwrite. Please open this pull request there instead.

@github-actions github-actions Bot closed this Nov 23, 2026

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · See the inline comments. Summary


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.

@claudear

Copy link
Copy Markdown
Contributor Author

Re-raised against the monorepo as appwrite/appwrite#15905, per the mirror redirect. Same three-file change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant