1
0
Fork 0
mirror of synced 2024-06-01 10:29:48 +12:00

fix: migration for custom collections and buckets

This commit is contained in:
Torsten Dittmann 2022-06-21 19:39:05 +02:00
parent 467246cc73
commit fc36e6c2d0
3 changed files with 49 additions and 42 deletions

View file

@ -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);

View file

@ -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");
}

View file

@ -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':