From 3d059e470a62e73bf96b8fdad30112ecf43b5d3f Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 5 Mar 2024 14:47:30 +0100 Subject: [PATCH 1/2] 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': From 740a6a1e2d902c642a685267bdb2e17685863285 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 5 Mar 2024 16:22:02 +0100 Subject: [PATCH 2/2] fix: migration for 1.5.x --- src/Appwrite/Migration/Version/V20.php | 122 ++++++++++++++----------- 1 file changed, 68 insertions(+), 54 deletions(-) diff --git a/src/Appwrite/Migration/Version/V20.php b/src/Appwrite/Migration/Version/V20.php index dc08facdd1..7a848734dd 100644 --- a/src/Appwrite/Migration/Version/V20.php +++ b/src/Appwrite/Migration/Version/V20.php @@ -93,13 +93,13 @@ class V20 extends Migration ]) as $index ) { 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; - } + try { + $this->projectDB->deleteIndex($collectionId, $index->getId()); + } catch (Throwable $th) { + Console::warning("Failed to delete index: {$th->getMessage()}"); + } finally { + $foundIndex = true; + } } } if ($foundIndex === true) { @@ -142,24 +142,6 @@ class V20 extends Migration $this->createCollection('challenges'); $this->createCollection('authenticators'); - break; - case 'cache': - // Create resourceType attribute - try { - $this->createAttributeFromCollection($this->projectDB, $id, 'resourceType'); - $this->projectDB->purgeCachedCollection($id); - } catch (Throwable $th) { - Console::warning("'resourceType' from {$id}: {$th->getMessage()}"); - } - - // Create mimeType attribute - try { - $this->createAttributeFromCollection($this->projectDB, $id, 'mimeType'); - $this->projectDB->purgeCachedCollection($id); - } catch (Throwable $th) { - Console::warning("'mimeType' from {$id}: {$th->getMessage()}"); - } - break; case 'stats': try { @@ -171,11 +153,16 @@ class V20 extends Migration * Alter `signed` internal type on `value` attr */ $this->projectDB->updateAttribute(collection: $id, id: 'value', signed: true); - $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { Console::warning("'type' from {$id}: {$th->getMessage()}"); } + try { + $this->projectDB->purgeCachedCollection($id); + } catch (Throwable $th) { + Console::warning("Purge cache from {$id}: {$th->getMessage()}"); + } + // update stats index $index = '_key_metric_period_time'; @@ -196,7 +183,6 @@ class V20 extends Migration // Create expire attribute try { $this->createAttributeFromCollection($this->projectDB, $id, 'expire'); - $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { Console::warning("'expire' from {$id}: {$th->getMessage()}"); } @@ -204,17 +190,28 @@ class V20 extends Migration // Create factors attribute try { $this->createAttributeFromCollection($this->projectDB, $id, 'factors'); - $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { Console::warning("'factors' from {$id}: {$th->getMessage()}"); } + // Create mfaRecoveryCodes attribute + try { + $this->createAttributeFromCollection($this->projectDB, $id, 'mfaUpdatedAt'); + } catch (Throwable $th) { + Console::warning("'mfaUpdatedAt' from {$id}: {$th->getMessage()}"); + } + + try { + $this->projectDB->purgeCachedCollection($id); + } catch (Throwable $th) { + Console::warning("Purge cache from {$id}: {$th->getMessage()}"); + } + break; case 'users': // Create targets attribute try { $this->createAttributeFromCollection($this->projectDB, $id, 'targets'); - $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { Console::warning("'targets' from {$id}: {$th->getMessage()}"); } @@ -222,7 +219,6 @@ class V20 extends Migration // Create mfa attribute try { $this->createAttributeFromCollection($this->projectDB, $id, 'mfa'); - $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { Console::warning("'mfa' from {$id}: {$th->getMessage()}"); } @@ -230,23 +226,13 @@ class V20 extends Migration // 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()}"); + Console::warning("'mfaRecoveryCodes' from {$id}: {$th->getMessage()}"); } // Create challenges attribute try { $this->createAttributeFromCollection($this->projectDB, $id, 'challenges'); - $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { Console::warning("'challenges' from {$id}: {$th->getMessage()}"); } @@ -254,30 +240,60 @@ class V20 extends Migration // Create authenticators attribute try { $this->createAttributeFromCollection($this->projectDB, $id, 'authenticators'); - $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { Console::warning("'authenticators' from {$id}: {$th->getMessage()}"); } + try { + $this->projectDB->purgeCachedCollection($id); + } catch (Throwable $th) { + Console::warning("Purge cache from {$id}: {$th->getMessage()}"); + } + break; case 'projects': // Rename providers authProviders to oAuthProviders try { $this->projectDB->renameAttribute($id, 'authProviders', 'oAuthProviders'); - $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { Console::warning("'oAuthProviders' from {$id}: {$th->getMessage()}"); } - break; - case 'webhooks': + try { - $this->createAttributeFromCollection($this->projectDB, $id, 'enabled'); - $this->createAttributeFromCollection($this->projectDB, $id, 'logs'); - $this->createAttributeFromCollection($this->projectDB, $id, 'attempts'); $this->projectDB->purgeCachedCollection($id); } catch (Throwable $th) { - Console::warning("'webhooks' from {$id}: {$th->getMessage()}"); + Console::warning("Purge cache from {$id}: {$th->getMessage()}"); } + + break; + case 'webhooks': + // Create enabled attribute + try { + $this->createAttributeFromCollection($this->projectDB, $id, 'enabled'); + } catch (Throwable $th) { + Console::warning("'enabled' from {$id}: {$th->getMessage()}"); + } + + // Create logs attribute + try { + $this->createAttributeFromCollection($this->projectDB, $id, 'logs'); + } catch (Throwable $th) { + Console::warning("'logs' from {$id}: {$th->getMessage()}"); + } + + // Create attempts attribute + try { + $this->createAttributeFromCollection($this->projectDB, $id, 'attempts'); + } catch (Throwable $th) { + Console::warning("'attempts' from {$id}: {$th->getMessage()}"); + } + + try { + $this->projectDB->purgeCachedCollection($id); + } catch (Throwable $th) { + Console::warning("Purge cache from {$id}: {$th->getMessage()}"); + } + break; } @@ -285,8 +301,6 @@ class V20 extends Migration } } - - /** * @return void * @throws Authorization @@ -336,7 +350,7 @@ class V20 extends Migration /** * Creating inf metric */ - console::log("Creating inf metric to {$metric}"); + Console::log("Creating inf metric to {$metric}"); $id = \md5("_inf_{$metric}"); $this->projectDB->createDocument('stats', new Document([ '$id' => $id, @@ -347,7 +361,7 @@ class V20 extends Migration 'region' => 'default', ])); } catch (Duplicate $th) { - console::log("Error while creating inf metric: duplicate id {$metric} {$id}"); + Console::warning("Error while creating inf metric: duplicate id {$metric} {$id}"); } } @@ -401,7 +415,7 @@ class V20 extends Migration $stat->setAttribute('$id', \md5("{$time}_{$stat['period']}_{$to}")); $stat->setAttribute('metric', $to); $this->projectDB->createDocument('stats', $stat); - console::log("deleting metric {$from} and creating {$to}"); + Console::log("deleting metric {$from} and creating {$to}"); } $latestDocument = !empty(array_key_last($stats)) ? $stats[array_key_last($stats)] : null; }