diff --git a/src/Appwrite/Migration/Version/V20.php b/src/Appwrite/Migration/Version/V20.php index 14d193501b..7a848734dd 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']); - $foundIndex = true; + 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']); } } } @@ -136,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 { @@ -165,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'; @@ -190,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()}"); } @@ -198,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()}"); } @@ -216,15 +219,20 @@ 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()}"); } + // Create mfaRecoveryCodes attribute + try { + $this->createAttributeFromCollection($this->projectDB, $id, 'mfaRecoveryCodes'); + } catch (Throwable $th) { + 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()}"); } @@ -232,32 +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; - default: + 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; } @@ -265,8 +301,6 @@ class V20 extends Migration } } - - /** * @return void * @throws Authorization @@ -278,7 +312,6 @@ class V20 extends Migration /** * Creating inf metric */ - Console::info('Migrating Sessions metric'); $sessionsCreated = $this->projectDB->sum('stats', 'value', [ @@ -317,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, @@ -328,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}"); } } @@ -382,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; } @@ -508,7 +541,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 +556,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':