From 3d059e470a62e73bf96b8fdad30112ecf43b5d3f Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 5 Mar 2024 14:47:30 +0100 Subject: [PATCH] fix: migration 1.5.x --- src/Appwrite/Migration/Version/V20.php | 43 +++++++++++++++++++++----- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Migration/Version/V20.php b/src/Appwrite/Migration/Version/V20.php index 14d193501b..dc08facdd1 100644 --- a/src/Appwrite/Migration/Version/V20.php +++ b/src/Appwrite/Migration/Version/V20.php @@ -85,19 +85,25 @@ class V20 extends Migration ]) as $attribute ) { $foundIndex = false; + $collectionId = "database_{$attribute['databaseInternalId']}_collection_{$attribute['collectionInternalId']}"; foreach ( $this->documentsIterator('indexes', [ Query::equal('databaseInternalId', [$attribute['databaseInternalId']]), Query::equal('collectionInternalId', [$attribute['collectionInternalId']]), ]) as $index ) { - if (in_array($attribute['key'], $index['attributes'])) { - $this->projectDB->deleteIndex($index['collectionId'], $index['$id']); + if (in_array($attribute->getAttribute('key'), $index->getAttribute('attributes'))) { + try { + $this->projectDB->deleteIndex($collectionId, $index->getId()); + } catch (Throwable $th) { + Console::warning("Failed to delete index: {$th->getMessage()}"); + } finally { $foundIndex = true; } + } } if ($foundIndex === true) { - $this->projectDB->updateAttribute($attribute['collectionInternalId'], $attribute['key'], $attribute['type']); + $this->projectDB->updateAttribute($collectionId, $attribute['key'], $attribute['type']); } } } @@ -221,6 +227,22 @@ class V20 extends Migration Console::warning("'mfa' from {$id}: {$th->getMessage()}"); } + // Create mfaRecoveryCodes attribute + try { + $this->createAttributeFromCollection($this->projectDB, $id, 'mfaRecoveryCodes'); + $this->projectDB->purgeCachedCollection($id); + } catch (Throwable $th) { + Console::warning("'mfa' from {$id}: {$th->getMessage()}"); + } + + // Create mfaRecoveryCodes attribute + try { + $this->createAttributeFromCollection($this->projectDB, $id, 'mfaUpdatedAt'); + $this->projectDB->purgeCachedCollection($id); + } catch (Throwable $th) { + Console::warning("'mfa' from {$id}: {$th->getMessage()}"); + } + // Create challenges attribute try { $this->createAttributeFromCollection($this->projectDB, $id, 'challenges'); @@ -257,8 +279,6 @@ class V20 extends Migration Console::warning("'webhooks' from {$id}: {$th->getMessage()}"); } break; - default: - break; } usleep(50000); @@ -278,7 +298,6 @@ class V20 extends Migration /** * Creating inf metric */ - Console::info('Migrating Sessions metric'); $sessionsCreated = $this->projectDB->sum('stats', 'value', [ @@ -508,7 +527,11 @@ class V20 extends Migration 'providerType' => MESSAGE_TYPE_EMAIL, 'identifier' => $document->getAttribute('email'), ]); - $this->projectDB->createDocument('targets', $target); + try { + $this->projectDB->createDocument('targets', $target); + } catch (Duplicate $th) { + Console::warning("Email target for user {$document->getId()} already exists."); + } } if ($document->getAttribute('phone', '') !== '') { @@ -519,7 +542,11 @@ class V20 extends Migration 'providerType' => MESSAGE_TYPE_SMS, 'identifier' => $document->getAttribute('phone'), ]); - $this->projectDB->createDocument('targets', $target); + try { + $this->projectDB->createDocument('targets', $target); + } catch (Duplicate $th) { + Console::warning("Email target for user {$document->getId()} already exists."); + } } break; case 'sessions':