1
0
Fork 0
mirror of synced 2024-07-06 23:21:05 +12:00

Merge pull request #7737 from appwrite/fix-mgiration-1.5.x

fix: migration 1.5.x
This commit is contained in:
Torsten Dittmann 2024-03-07 17:19:43 +01:00 committed by GitHub
commit fa37105de5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -85,19 +85,25 @@ class V20 extends Migration
]) as $attribute ]) as $attribute
) { ) {
$foundIndex = false; $foundIndex = false;
$collectionId = "database_{$attribute['databaseInternalId']}_collection_{$attribute['collectionInternalId']}";
foreach ( foreach (
$this->documentsIterator('indexes', [ $this->documentsIterator('indexes', [
Query::equal('databaseInternalId', [$attribute['databaseInternalId']]), Query::equal('databaseInternalId', [$attribute['databaseInternalId']]),
Query::equal('collectionInternalId', [$attribute['collectionInternalId']]), Query::equal('collectionInternalId', [$attribute['collectionInternalId']]),
]) as $index ]) as $index
) { ) {
if (in_array($attribute['key'], $index['attributes'])) { if (in_array($attribute->getAttribute('key'), $index->getAttribute('attributes'))) {
$this->projectDB->deleteIndex($index['collectionId'], $index['$id']); try {
$foundIndex = true; $this->projectDB->deleteIndex($collectionId, $index->getId());
} catch (Throwable $th) {
Console::warning("Failed to delete index: {$th->getMessage()}");
} finally {
$foundIndex = true;
}
} }
} }
if ($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('challenges');
$this->createCollection('authenticators'); $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; break;
case 'stats': case 'stats':
try { try {
@ -165,11 +153,16 @@ class V20 extends Migration
* Alter `signed` internal type on `value` attr * Alter `signed` internal type on `value` attr
*/ */
$this->projectDB->updateAttribute(collection: $id, id: 'value', signed: true); $this->projectDB->updateAttribute(collection: $id, id: 'value', signed: true);
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'type' from {$id}: {$th->getMessage()}"); 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 // update stats index
$index = '_key_metric_period_time'; $index = '_key_metric_period_time';
@ -190,7 +183,6 @@ class V20 extends Migration
// Create expire attribute // Create expire attribute
try { try {
$this->createAttributeFromCollection($this->projectDB, $id, 'expire'); $this->createAttributeFromCollection($this->projectDB, $id, 'expire');
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'expire' from {$id}: {$th->getMessage()}"); Console::warning("'expire' from {$id}: {$th->getMessage()}");
} }
@ -198,17 +190,28 @@ class V20 extends Migration
// Create factors attribute // Create factors attribute
try { try {
$this->createAttributeFromCollection($this->projectDB, $id, 'factors'); $this->createAttributeFromCollection($this->projectDB, $id, 'factors');
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'factors' from {$id}: {$th->getMessage()}"); 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; break;
case 'users': case 'users':
// Create targets attribute // Create targets attribute
try { try {
$this->createAttributeFromCollection($this->projectDB, $id, 'targets'); $this->createAttributeFromCollection($this->projectDB, $id, 'targets');
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'targets' from {$id}: {$th->getMessage()}"); Console::warning("'targets' from {$id}: {$th->getMessage()}");
} }
@ -216,15 +219,20 @@ class V20 extends Migration
// Create mfa attribute // Create mfa attribute
try { try {
$this->createAttributeFromCollection($this->projectDB, $id, 'mfa'); $this->createAttributeFromCollection($this->projectDB, $id, 'mfa');
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'mfa' from {$id}: {$th->getMessage()}"); 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 // Create challenges attribute
try { try {
$this->createAttributeFromCollection($this->projectDB, $id, 'challenges'); $this->createAttributeFromCollection($this->projectDB, $id, 'challenges');
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'challenges' from {$id}: {$th->getMessage()}"); Console::warning("'challenges' from {$id}: {$th->getMessage()}");
} }
@ -232,32 +240,60 @@ class V20 extends Migration
// Create authenticators attribute // Create authenticators attribute
try { try {
$this->createAttributeFromCollection($this->projectDB, $id, 'authenticators'); $this->createAttributeFromCollection($this->projectDB, $id, 'authenticators');
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'authenticators' from {$id}: {$th->getMessage()}"); Console::warning("'authenticators' from {$id}: {$th->getMessage()}");
} }
try {
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) {
Console::warning("Purge cache from {$id}: {$th->getMessage()}");
}
break; break;
case 'projects': case 'projects':
// Rename providers authProviders to oAuthProviders // Rename providers authProviders to oAuthProviders
try { try {
$this->projectDB->renameAttribute($id, 'authProviders', 'oAuthProviders'); $this->projectDB->renameAttribute($id, 'authProviders', 'oAuthProviders');
$this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'oAuthProviders' from {$id}: {$th->getMessage()}"); Console::warning("'oAuthProviders' from {$id}: {$th->getMessage()}");
} }
break;
case 'webhooks':
try { try {
$this->createAttributeFromCollection($this->projectDB, $id, 'enabled');
$this->createAttributeFromCollection($this->projectDB, $id, 'logs');
$this->createAttributeFromCollection($this->projectDB, $id, 'attempts');
$this->projectDB->purgeCachedCollection($id); $this->projectDB->purgeCachedCollection($id);
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("'webhooks' from {$id}: {$th->getMessage()}"); Console::warning("Purge cache from {$id}: {$th->getMessage()}");
} }
break; 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; break;
} }
@ -265,8 +301,6 @@ class V20 extends Migration
} }
} }
/** /**
* @return void * @return void
* @throws Authorization * @throws Authorization
@ -278,7 +312,6 @@ class V20 extends Migration
/** /**
* Creating inf metric * Creating inf metric
*/ */
Console::info('Migrating Sessions metric'); Console::info('Migrating Sessions metric');
$sessionsCreated = $this->projectDB->sum('stats', 'value', [ $sessionsCreated = $this->projectDB->sum('stats', 'value', [
@ -317,7 +350,7 @@ class V20 extends Migration
/** /**
* Creating inf metric * Creating inf metric
*/ */
console::log("Creating inf metric to {$metric}"); Console::log("Creating inf metric to {$metric}");
$id = \md5("_inf_{$metric}"); $id = \md5("_inf_{$metric}");
$this->projectDB->createDocument('stats', new Document([ $this->projectDB->createDocument('stats', new Document([
'$id' => $id, '$id' => $id,
@ -328,7 +361,7 @@ class V20 extends Migration
'region' => 'default', 'region' => 'default',
])); ]));
} catch (Duplicate $th) { } 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('$id', \md5("{$time}_{$stat['period']}_{$to}"));
$stat->setAttribute('metric', $to); $stat->setAttribute('metric', $to);
$this->projectDB->createDocument('stats', $stat); $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; $latestDocument = !empty(array_key_last($stats)) ? $stats[array_key_last($stats)] : null;
} }
@ -508,7 +541,11 @@ class V20 extends Migration
'providerType' => MESSAGE_TYPE_EMAIL, 'providerType' => MESSAGE_TYPE_EMAIL,
'identifier' => $document->getAttribute('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', '') !== '') { if ($document->getAttribute('phone', '') !== '') {
@ -519,7 +556,11 @@ class V20 extends Migration
'providerType' => MESSAGE_TYPE_SMS, 'providerType' => MESSAGE_TYPE_SMS,
'identifier' => $document->getAttribute('phone'), '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; break;
case 'sessions': case 'sessions':