createIndex: run pre-check + drop before count and IndexValidator

Greptile: $count (META_INDEXES query) and IndexValidator both ran
BEFORE the pre-check / drop block. On an Upsert recreate path:

- The to-be-dropped index was included in the count, so a table at
  getLimitForIndexes() falsely threw 'Index limit reached' even
  though the net change is zero (one drop, one add).
- IndexValidator was initialized with $tableIndexes from the
  in-memory $table — including the existing index — so the new
  index spec could be flagged as a conflict against itself ('Invalid
  index: …') before the drop ran.

Reordered createIndex so:
  1. Pre-check + drop block runs first (Tolerate returns early).
  2. $table is reloaded after a drop so embedded $tableIndexes
     reflects the post-drop state.
  3. count, validateFieldsForIndexes, build $index, IndexValidator
     all run AFTER the drop, against the fresh $table.

Mirrors the attribute path's structure where checkAttribute already
runs after dropAttributeForRecreate.
This commit is contained in:
Prem Palanisamy committed 2026-04-28 08:10:11 +01:00
1 parent d37efed0d9
commit 7d71505ec0
1 file changed
+45 -40
+45 -40
View File
@@ -1048,6 +1048,50 @@ class Appwrite extends Destination
}
$dbForDatabases = ($this->getDatabasesDB)($database);
$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
$updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt);
$this->trackOrphanCandidate($database, $table, 'indexKeys', $resource->getKey(), $dbForDatabases);
$indexMetaId = $this->attributeIndexMetaId($database, $table, $resource->getKey());
// Pre-check + drop runs BEFORE count/validator so the to-be-dropped index isn't included in
// the limit count or in IndexValidator's $tableIndexes (otherwise an Upsert recreate at the
// ceiling throws "Index limit reached" or "Invalid index" even though the net change is zero).
if ($this->onDuplicate !== OnDuplicate::Fail) {
$existingIdx = $this->dbForProject->getDocument(self::META_INDEXES, $indexMetaId);
$action = $this->onDuplicate->resolveSchemaAction(
!$existingIdx->isEmpty(),
$updatedAt,
$existingIdx->getUpdatedAt(),
);
// Spec match → skip work. Create excluded; nothing on dest to match against.
if ($action !== SchemaAction::Create && $this->indexSpecMatches($existingIdx, $resource)) {
$action = SchemaAction::Tolerate;
}
// Indexes have no in-place primitive — any action other than Tolerate falls through to drop+recreate.
$earlyReturn = match ($action) {
SchemaAction::Tolerate => (function () use ($resource, $database, $table): bool {
$this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId());
$resource->setStatus(Resource::STATUS_SKIPPED, 'Already exists on destination');
return false;
})(),
SchemaAction::UpdateInPlace, SchemaAction::Create => null,
};
if ($earlyReturn !== null) {
return $earlyReturn;
}
if ($action === SchemaAction::UpdateInPlace) {
$dbForDatabases->deleteIndex($this->tableCollectionId($database, $table), $resource->getKey());
$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.
$table = $this->dbForProject->getDocument($this->databaseCollectionId($database), $table->getId());
}
}
$count = $this->dbForProject->count(self::META_INDEXES, [
Query::equal('collectionInternalId', [$table->getSequence()]),
Query::equal('databaseInternalId', [$database->getSequence()])
@@ -1069,13 +1113,8 @@ class Appwrite extends Destination
$this->validateFieldsForIndexes($resource, $table, $lengths);
}
$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
$updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt);
$this->trackOrphanCandidate($database, $table, 'indexKeys', $resource->getKey(), $dbForDatabases);
$index = new UtopiaDocument([
'$id' => ID::custom($this->attributeIndexMetaId($database, $table, $resource->getKey())),
'$id' => ID::custom($indexMetaId),
'key' => $resource->getKey(),
'status' => 'available', // processing, available, failed, deleting, stuck
'databaseInternalId' => $database->getSequence(),
@@ -1116,7 +1155,6 @@ class Appwrite extends Destination
$dbForDatabases->getAdapter()->getSupportForFulltextIndex()
);
if (!$validator->isValid($index)) {
throw new Exception(
resourceName: $resource->getName(),
@@ -1126,39 +1164,6 @@ class Appwrite extends Destination
);
}
$indexMetaId = $this->attributeIndexMetaId($database, $table, $resource->getKey());
if ($this->onDuplicate !== OnDuplicate::Fail) {
$existingIdx = $this->dbForProject->getDocument(self::META_INDEXES, $indexMetaId);
$action = $this->onDuplicate->resolveSchemaAction(
!$existingIdx->isEmpty(),
$updatedAt,
$existingIdx->getUpdatedAt(),
);
// Spec match → skip work. Create excluded; nothing on dest to match against.
if ($action !== SchemaAction::Create && $this->indexSpecMatches($existingIdx, $resource)) {
$action = SchemaAction::Tolerate;
}
// Indexes have no in-place primitive — any action other than Tolerate falls through to drop+recreate.
$earlyReturn = match ($action) {
SchemaAction::Tolerate => (function () use ($resource, $database, $table): bool {
$this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId());
$resource->setStatus(Resource::STATUS_SKIPPED, 'Already exists on destination');
return false;
})(),
SchemaAction::UpdateInPlace, SchemaAction::Create => null,
};
if ($earlyReturn !== null) {
return $earlyReturn;
}
if ($action === SchemaAction::UpdateInPlace) {
$dbForDatabases->deleteIndex($this->tableCollectionId($database, $table), $resource->getKey());
$this->dbForProject->deleteDocument(self::META_INDEXES, $indexMetaId);
$this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId());
}
}
$index = $this->dbForProject->createDocument(self::META_INDEXES, $index);
try {