diff --git a/app/tasks/migrate.php b/app/tasks/migrate.php index ff0705eb32..a7aa55aa78 100644 --- a/app/tasks/migrate.php +++ b/app/tasks/migrate.php @@ -29,7 +29,7 @@ $cli $db = $register->get('db', true); $cache = $register->get('cache', true); - + $cache->flushAll(); $cache = new Cache(new RedisCache($cache)); $projectDB = new Database(new MariaDB($db), $cache); diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index f702bb7ac7..0c292d7b4c 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -292,7 +292,7 @@ abstract class Migration $indexKey = array_search($indexId, array_column($indexes, '$id')); - if (!$indexKey) { + if ($indexKey === false) { throw new Exception("Attribute {$indexId} not found"); } diff --git a/src/Appwrite/Migration/Version/V14.php b/src/Appwrite/Migration/Version/V14.php index 8f9e06b684..3f0dd944f2 100644 --- a/src/Appwrite/Migration/Version/V14.php +++ b/src/Appwrite/Migration/Version/V14.php @@ -25,6 +25,42 @@ class V14 extends Migration $this->forEachDocument([$this, 'fixDocument']); } + protected function createNewMetaData(string $id): void + { + if (in_array($id, ['files'])) return; + + try { + $this->pdo->prepare("ALTER TABLE IF EXISTS `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getId()}_{$id}` RENAME TO `_{$this->project->getInternalId()}_{$id}`")->execute(); + } catch (\Throwable $th) { + Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); + } + try { + $this->pdo->prepare("ALTER TABLE IF EXISTS `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getId()}_{$id}_perms` RENAME TO `_{$this->project->getInternalId()}_{$id}_perms`")->execute(); + } catch (\Throwable $th) { + Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); + } + try { + $this->pdo->prepare("ALTER TABLE `_{$this->project->getInternalId()}_{$id}` ADD COLUMN IF NOT EXISTS `_createdAt` int unsigned DEFAULT NULL")->execute(); + } catch (\Throwable $th) { + Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); + } + try { + $this->pdo->prepare("ALTER TABLE `_{$this->project->getInternalId()}_{$id}` ADD COLUMN IF NOT EXISTS `_updatedAt` int unsigned DEFAULT NULL")->execute(); + } catch (\Throwable $th) { + Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); + } + try { + $this->pdo->prepare("CREATE INDEX IF NOT EXISTS `_created_at` ON `_{$this->project->getInternalId()}_{$id}` (`_createdAt`)")->execute(); + } catch (\Throwable $th) { + Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); + } + try { + $this->pdo->prepare("CREATE INDEX IF NOT EXISTS `_updated_at` ON `_{$this->project->getInternalId()}_{$id}` (`_updatedAt`)")->execute(); + } catch (\Throwable $th) { + Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); + } + } + /** * Migrate all Collections. * @@ -37,36 +73,7 @@ class V14 extends Migration Console::log("- {$id}"); - try { - $this->pdo->prepare("ALTER TABLE IF EXISTS `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getId()}_{$id}` RENAME TO `_{$this->project->getInternalId()}_{$id}`")->execute(); - } catch (\Throwable $th) { - Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); - } - try { - $this->pdo->prepare("ALTER TABLE IF EXISTS `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getId()}_{$id}_perms` RENAME TO `_{$this->project->getInternalId()}_{$id}_perms`")->execute(); - } catch (\Throwable $th) { - Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); - } - try { - $this->pdo->prepare("ALTER TABLE `_{$this->project->getInternalId()}_{$id}` ADD COLUMN IF NOT EXISTS `_createdAt` int unsigned DEFAULT NULL")->execute(); - } catch (\Throwable $th) { - Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); - } - try { - $this->pdo->prepare("ALTER TABLE `_{$this->project->getInternalId()}_{$id}` ADD COLUMN IF NOT EXISTS `_updatedAt` int unsigned DEFAULT NULL")->execute(); - } catch (\Throwable $th) { - Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); - } - try { - $this->pdo->prepare("CREATE INDEX IF NOT EXISTS `_created_at` ON `_{$this->project->getInternalId()}_{$id}` (`_createdAt`)")->execute(); - } catch (\Throwable $th) { - Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); - } - try { - $this->pdo->prepare("CREATE INDEX IF NOT EXISTS `_updatedAt` ON `_{$this->project->getInternalId()}_{$id}` (`_updatedAt`)")->execute(); - } catch (\Throwable $th) { - Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); - } + $this->createNewMetaData($id); usleep(100000); @@ -102,7 +109,7 @@ class V14 extends Migration */ $this->createAttributeFromCollection($this->projectDB, $id, 'teamInternalId'); } catch (\Throwable $th) { - Console::warning("'collectionInternalId' from {$id}: {$th->getMessage()}"); + Console::warning("'teamInternalId' from {$id}: {$th->getMessage()}"); } break; @@ -113,7 +120,7 @@ class V14 extends Migration */ $this->createAttributeFromCollection($this->projectDB, $id, 'projectInternalId'); } catch (\Throwable $th) { - Console::warning("'collectionInternalId' from {$id}: {$th->getMessage()}"); + Console::warning("'projectInternalId' from {$id}: {$th->getMessage()}"); } try { /** @@ -225,7 +232,7 @@ class V14 extends Migration */ $this->createIndexFromCollection($this->projectDB, $id, '_key_phone'); } catch (\Throwable $th) { - Console::warning("'_key_project' from {$id}: {$th->getMessage()}"); + Console::warning("'_key_phone' from {$id}: {$th->getMessage()}"); } break; @@ -276,7 +283,7 @@ class V14 extends Migration */ $this->createAttributeFromCollection($this->projectDB, $id, 'teamInternalId'); } catch (\Throwable $th) { - Console::warning("'userInternalId' from {$id}: {$th->getMessage()}"); + Console::warning("'teamInternalId' from {$id}: {$th->getMessage()}"); } try { /** @@ -388,7 +395,7 @@ class V14 extends Migration break; case 'attributes': case 'indexes': - if (!empty($document->getAttribute('collectionId')) && is_null($document->getAttribute('collectionInternalId'))) { + if (!empty($document->getAttribute('collectionId')) && is_null($document->getAttribute('collectionInternalId'))) { $internalId = $this->projectDB->getDocument('collections', $document->getAttribute('collectionId'))->getInternalId(); $document->setAttribute('collectionInternalId', $internalId); } @@ -402,6 +409,9 @@ class V14 extends Migration $document->setAttribute('$updatedAt', $document->getAttribute('dateUpdated')); } + $internalId = $this->projectDB->getDocument('collections', $document->getId())->getInternalId(); + $this->createNewMetaData("collection_{$internalId}"); + break; case 'platforms': if (is_null($document->getCreatedAt())) { @@ -424,11 +434,8 @@ class V14 extends Migration $document->setAttribute('$updatedAt', $document->getAttribute('dateUpdated')); } - break; - case 'files': - if (is_null($document->getCreatedAt())) { - $document->setAttribute('$createdAt', $document->getAttribute('dateCreated')); - } + $internalId = $this->projectDB->getDocument('buckets', $document->getId())->getInternalId(); + $this->createNewMetaData("bucket_{$internalId}"); break; case 'users':