fix: migration to modify tables

This commit is contained in:
Torsten Dittmann 2022-06-21 15:46:05 +02:00
parent ef6768433c
commit 718f4f1b5d
2 changed files with 29 additions and 9 deletions

View file

@ -242,7 +242,6 @@ abstract class Migration
public function createAttributeFromCollection(Database $database, string $collectionId, string $attributeId): void public function createAttributeFromCollection(Database $database, string $collectionId, string $attributeId): void
{ {
$collection = Config::getParam('collections', [])[$collectionId] ?? null; $collection = Config::getParam('collections', [])[$collectionId] ?? null;
if (is_null($collection)) { if (is_null($collection)) {
throw new Exception("Collection {$collectionId} not found"); throw new Exception("Collection {$collectionId} not found");
} }
@ -250,7 +249,7 @@ abstract class Migration
$attributeKey = array_search($attributeId, array_column($attributes, '$id')); $attributeKey = array_search($attributeId, array_column($attributes, '$id'));
if (!$attributeKey) { if ($attributeKey === false) {
throw new Exception("Attribute {$attributeId} not found"); throw new Exception("Attribute {$attributeId} not found");
} }

View file

@ -40,15 +40,39 @@ class V14 extends Migration
try { try {
$this->pdo->prepare("ALTER TABLE IF EXISTS `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getId()}_{$id}` RENAME TO `_{$this->project->getInternalId()}_{$id}`")->execute(); $this->pdo->prepare("ALTER TABLE IF EXISTS `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getId()}_{$id}` RENAME TO `_{$this->project->getInternalId()}_{$id}`")->execute();
$this->pdo->prepare("ALTER TABLE `_{$this->project->getInternalId()}_{$id}` ADD `_createdAt` int unsigned DEFAULT NULL")->execute();
$this->pdo->prepare("ALTER TABLE `_{$this->project->getInternalId()}_{$id}` ADD `_updatedAt` int unsigned DEFAULT NULL")->execute();
$this->pdo->prepare("CREATE INDEX `_created_at` ON `_{$this->project->getInternalId()}_{$id}` (`_createdAt`)")->execute();
$this->pdo->prepare("CREATE INDEX `_updatedAt` ON `_{$this->project->getInternalId()}_{$id}` (`_updatedAt`)")->execute();
} catch (\Throwable $th) { } catch (\Throwable $th) {
Console::warning("Migrating {$id} Collection: {$th->getMessage()}"); 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()}");
}
usleep(100000); usleep(100000);
$this->projectDB->setNamespace("_{$this->project->getInternalId()}");
switch ($id) { switch ($id) {
case 'attributes': case 'attributes':
case 'indexes': case 'indexes':
@ -303,9 +327,6 @@ class V14 extends Migration
*/ */
$document->setAttribute('version', '0.15.0'); $document->setAttribute('version', '0.15.0');
break;
case '':
break; break;
} }