From a9b18811ea8113b03975c75a8ff90ef71b385445 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Tue, 23 Sep 2025 14:35:01 -0700 Subject: [PATCH] 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. --- src/Appwrite/Migration/Version/V23.php | 58 ++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Migration/Version/V23.php b/src/Appwrite/Migration/Version/V23.php index fb9a646898..b3d29fad64 100644 --- a/src/Appwrite/Migration/Version/V23.php +++ b/src/Appwrite/Migration/Version/V23.php @@ -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 *