appwrite/src/Appwrite/Migration/Version/V21.php

229 lines
8.3 KiB
PHP
Raw Normal View History

2024-07-17 05:52:17 +00:00
<?php
namespace Appwrite\Migration\Version;
use Appwrite\Migration\Migration;
use Exception;
use Throwable;
use Utopia\CLI\Console;
use Utopia\Database\Database;
2024-07-17 06:20:34 +00:00
use Utopia\Database\DateTime;
2024-07-17 05:52:17 +00:00
use Utopia\Database\Document;
2024-07-17 05:54:37 +00:00
class V21 extends Migration
2024-07-17 05:52:17 +00:00
{
/**
* @throws Throwable
*/
public function execute(): void
{
/**
* Disable SubQueries for Performance.
*/
foreach (['subQueryIndexes', 'subQueryPlatforms', 'subQueryDomains', 'subQueryKeys', 'subQueryWebhooks', 'subQuerySessions', 'subQueryTokens', 'subQueryMemberships', 'subQueryVariables', 'subQueryChallenges', 'subQueryProjectVariables', 'subQueryTargets', 'subQueryTopicTargets'] as $name) {
Database::addFilter(
$name,
fn () => null,
fn () => []
);
}
Console::log('Migrating Project: ' . $this->project->getAttribute('name') . ' (' . $this->project->getId() . ')');
$this->projectDB->setNamespace("_{$this->project->getInternalId()}");
Console::info('Migrating Collections');
$this->migrateCollections();
2024-07-30 20:53:18 +00:00
if ($this->project->getInternalId() !== 'console') {
Console::info('Migrating Buckets');
$this->migrateBuckets();
}
2024-07-30 21:39:32 +00:00
Console::info('Migrating Documents');
$this->forEachDocument([$this, 'fixDocument']);
2024-07-17 05:52:17 +00:00
}
/**
* Migrate Collections.
*
* @return void
* @throws Exception|Throwable
*/
private function migrateCollections(): void
{
$internalProjectId = $this->project->getInternalId();
$collectionType = match ($internalProjectId) {
'console' => 'console',
default => 'projects',
};
$collections = $this->collections[$collectionType];
foreach ($collections as $collection) {
$id = $collection['$id'];
Console::log("Migrating Collection \"{$id}\"");
$this->projectDB->setNamespace("_$internalProjectId");
switch ($id) {
case 'projects':
// Create accessedAt attribute
try {
$this->createAttributeFromCollection($this->projectDB, $id, 'accessedAt');
} catch (Throwable $th) {
Console::warning("'accessedAt' from {$id}: {$th->getMessage()}");
}
2024-07-24 07:00:20 +00:00
break;
case 'platforms':
// Increase 'type' length to 255
try {
$this->projectDB->updateAttribute($id, 'type', size: 255);
} catch (Throwable $th) {
Console::warning("'type' from {$id}: {$th->getMessage()}");
}
break;
case 'migrations':
// Create destination attribute
try {
$this->createAttributeFromCollection($this->projectDB, $id, 'destination');
} catch (Throwable $th) {
Console::warning("'destination' from {$id}: {$th->getMessage()}");
}
break;
2024-07-17 05:52:17 +00:00
case 'schedules':
// Create data attribute
try {
$this->createAttributeFromCollection($this->projectDB, $id, 'data');
} catch (Throwable $th) {
Console::warning("'data' from {$id}: {$th->getMessage()}");
}
break;
case 'databases':
// Create originalId attribute
try {
$this->createAttributeFromCollection($this->projectDB, $id, 'originalId');
} catch (Throwable $th) {
Console::warning("'originalId' from {$id}: {$th->getMessage()}");
}
break;
2024-07-17 05:52:17 +00:00
case 'functions':
// Create scopes attribute
try {
$this->createAttributeFromCollection($this->projectDB, $id, 'scopes');
} catch (Throwable $th) {
Console::warning("'scopes' from {$id}: {$th->getMessage()}");
}
// Create specification attribute
2024-07-22 10:40:05 +00:00
try {
$this->createAttributeFromCollection($this->projectDB, $id, 'specification');
2024-07-22 10:40:05 +00:00
} catch (Throwable $th) {
Console::warning("'specification' from {$id}: {$th->getMessage()}");
2024-07-22 10:40:05 +00:00
}
2024-07-17 05:52:17 +00:00
break;
case 'executions':
// Create requestMethod index
try {
$this->createIndexFromCollection($this->projectDB, $id, '_key_requestMethod');
} catch (\Throwable $th) {
Console::warning("'_key_requestMethod' from {$id}: {$th->getMessage()}");
}
// Create requestPath index
try {
$this->createIndexFromCollection($this->projectDB, $id, '_key_requestPath');
} catch (\Throwable $th) {
Console::warning("'_key_requestPath' from {$id}: {$th->getMessage()}");
}
// Create deployment index
try {
$this->createIndexFromCollection($this->projectDB, $id, '_key_deployment');
} catch (\Throwable $th) {
Console::warning("'_key_deployment' from {$id}: {$th->getMessage()}");
}
2024-07-24 07:11:48 +00:00
try {
/**
* Create 'scheduledAt' attribute
*/
$this->createAttributeFromCollection($this->projectDB, $id, 'scheduledAt');
} catch (\Throwable $th) {
Console::warning("'scheduledAt' from {$id}: {$th->getMessage()}");
}
try {
/**
* Create 'scheduleInternalId' attribute
*/
$this->createAttributeFromCollection($this->projectDB, $id, 'scheduleInternalId');
} catch (\Throwable $th) {
Console::warning("'scheduleInternalId' from {$id}: {$th->getMessage()}");
}
try {
/**
* Create 'scheduleId' attribute
*/
$this->createAttributeFromCollection($this->projectDB, $id, 'scheduleId');
} catch (\Throwable $th) {
Console::warning("'scheduleId' from {$id}: {$th->getMessage()}");
}
break;
2024-07-17 05:52:17 +00:00
}
usleep(50000);
}
}
/**
* Fix run on each document
*
* @param Document $document
* @return Document
*/
protected function fixDocument(Document $document): Document
{
switch ($document->getCollection()) {
case 'projects':
/**
* Bump version number.
*/
$document->setAttribute('version', '1.6.0');
// Add accessedAt attribute
2024-07-17 06:20:42 +00:00
$document->setAttribute('accessedAt', DateTime::now());
2024-07-17 05:52:17 +00:00
break;
2024-07-22 10:40:05 +00:00
case 'functions':
// Add scopes attribute
$document->setAttribute('scopes', []);
// Add size attribute
2024-08-20 04:23:11 +00:00
$document->setAttribute('specification', APP_FUNCTION_SPECIFICATION_DEFAULT);
2024-07-17 05:52:17 +00:00
}
2024-07-22 10:40:05 +00:00
2024-07-17 05:52:17 +00:00
return $document;
}
2024-07-30 20:53:18 +00:00
/**
* Migrating Buckets.
*
* @return void
*/
private function migrateBuckets(): void
2024-07-30 20:53:18 +00:00
{
foreach ($this->documentsIterator('buckets') as $bucket) {
$bucketId = 'bucket_' . $bucket['$internalId'];
2024-07-30 21:39:32 +00:00
try {
2024-08-19 09:43:50 +00:00
$this->projectDB->updateAttribute($bucketId, 'metadata', size: 65534);
2024-07-31 00:36:29 +00:00
$this->projectDB->purgeCachedCollection($bucketId);
2024-07-30 21:39:32 +00:00
} catch (\Throwable $th) {
Console::warning("'bucketId' from {$bucketId}: {$th->getMessage()}");
}
2024-07-30 20:53:18 +00:00
}
}
2024-07-17 05:52:17 +00:00
}