fix(self-hosted): clear cache for collections and documents

In older versions of Appwrite, the internal ID was the $internalId attribute. However, it has now changed to $sequence so that it can align with the publicly exposed attribute for an auto-incrementing ID.

The problem with this change is that data in the cache still references the old $internalId attribute, which can lead to inconsistencies and errors when accessing cached documents. To resolve this issue, we need to clear the cache for all collections and documents at the beginning of the migration so that the new $sequence attribute is used.
This commit is contained in:
Steven Nguyen 2025-09-23 14:35:01 -07:00
parent 59f82a0dd7
commit a9b18811ea
No known key found for this signature in database

View file

@ -42,14 +42,22 @@ class V23 extends Migration
foreach ($subQueries as $name) {
Database::addFilter(
$name,
fn() => null,
fn() => []
fn () => null,
fn () => []
);
}
Console::info('Migrating collections');
$this->migrateCollections();
if ($this->project->getSequence() != 'console') {
Console::info('Migrating Databases');
$this->migrateDatabases();
}
Console::info('Migrating Buckets');
$this->migrateBuckets();
Console::info('Migrating documents');
$this->forEachDocument($this->migrateDocument(...));
}
@ -84,6 +92,10 @@ class V23 extends Migration
Console::log("Migrating collection \"{$id}\"");
// Clear cache to ensure new $sequence is used
$this->dbForProject->purgeCachedCollection($id);
$this->dbForProject->purgeCachedDocument(Database::METADATA, $id);
switch ($id) {
case 'databases':
$attributes = [
@ -102,8 +114,6 @@ class V23 extends Migration
} catch (Throwable $th) {
Console::warning("'resourceInternalId' from {$id}: {$th->getMessage()}");
}
break;
$this->dbForProject->purgeCachedCollection($id);
break;
default:
@ -112,6 +122,46 @@ class V23 extends Migration
}
}
/**
* Migrate all Database Table tables
*
* @return void
* @throws Exception
*/
private function migrateDatabases(): void
{
$this->dbForProject->foreach('databases', function (Document $database) {
Console::log("Migrating Collections of {$database->getId()} ({$database->getAttribute('name')})");
$databaseTable = "database_{$database->getSequence()}";
$this->dbForProject->purgeCachedCollection($databaseTable);
$this->dbForProject->foreach($databaseTable, function (Document $collection) use ($databaseTable) {
Console::log("Migrating Collection of {$collection->getId()} ({$collection->getAttribute('name')})");
$collectionTable = "{$databaseTable}_collection_{$collection->getSequence()}";
$this->dbForProject->purgeCachedCollection($collectionTable);
});
});
}
/**
* Migrate all Bucket tables
*
* @return void
* @throws \Exception
* @throws \PDOException
*/
protected function migrateBuckets(): void
{
$this->dbForProject->foreach('buckets', function (Document $bucket) {
Console::log("Migrating Bucket {$bucket->getId()} ({$bucket->getAttribute('name')})");
$bucketTable = "bucket_{$bucket->getSequence()}";
$this->dbForProject->purgeCachedCollection($bucketTable);
});
}
/**
* Fix run on each document
*