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

161 lines
4.9 KiB
PHP
Raw Normal View History

2023-03-17 01:21:46 +00:00
<?php
namespace Appwrite\Migration\Version;
use Appwrite\Auth\Auth;
use Utopia\Config\Config;
2023-03-17 01:21:46 +00:00
use Appwrite\Migration\Migration;
use Utopia\CLI\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
class V19 extends Migration
{
public function execute(): void
{
2023-06-06 15:36:05 +00:00
2023-03-17 01:21:46 +00:00
/**
* Disable SubQueries for Performance.
*/
2023-06-06 15:36:05 +00:00
foreach (['subQueryIndexes', 'subQueryPlatforms', 'subQueryDomains', 'subQueryKeys', 'subQueryWebhooks', 'subQuerySessions', 'subQueryTokens', 'subQueryMemberships', 'subQueryVariables'] as $name) {
2023-03-17 01:21:46 +00:00
Database::addFilter(
$name,
fn () => null,
fn () => []
);
}
Console::log('Migrating Project: ' . $this->project->getAttribute('name') . ' (' . $this->project->getId() . ')');
2023-06-06 15:36:05 +00:00
$this->projectDB->setNamespace("_{$this->project->getInternalId()}");
2023-06-06 15:45:06 +00:00
$this->alterPermissionIndex('_metadata');
2023-06-06 15:36:05 +00:00
Console::info('Migrating Databases');
$this->migrateDatabases();
2023-03-17 01:21:46 +00:00
Console::info('Migrating Collections');
$this->migrateCollections();
2023-06-06 15:47:11 +00:00
Console::info('Migrating Buckets');
$this->migrateBuckets();
2023-03-17 01:21:46 +00:00
Console::info('Migrating Documents');
$this->forEachDocument([$this, 'fixDocument']);
}
2023-06-06 15:36:05 +00:00
/**
* Migrate all Databases.
*
* @return void
* @throws \Exception
*/
private function migrateDatabases(): void
{
foreach ($this->documentsIterator('databases') as $database) {
2023-06-08 07:53:40 +00:00
Console::log("Migrating Collections of {$database->getId()} ({$database->getAttribute('name')})");
2023-06-06 15:36:05 +00:00
2023-06-08 07:53:40 +00:00
$databaseTable = "database_{$database->getInternalId()}";
2023-06-06 15:36:05 +00:00
$this->alterPermissionIndex($databaseTable);
foreach ($this->documentsIterator($databaseTable) as $collection) {
$collectionTable = "{$databaseTable}_collection_{$collection->getInternalId()}";
2023-06-08 07:53:40 +00:00
Console::log("Migrating Collections of {$collectionTable} {$collection->getId()} ({$collection->getAttribute('name')})");
2023-06-06 15:36:05 +00:00
$this->alterPermissionIndex($collectionTable);
}
}
}
2023-03-17 01:21:46 +00:00
/**
* Migrate all Collections.
*
* @return void
*/
2023-06-06 15:36:05 +00:00
private function migrateCollections(): void
2023-03-17 01:21:46 +00:00
{
foreach ($this->collections as $collection) {
$id = $collection['$id'];
Console::log("Migrating Collection \"{$id}\"");
$this->projectDB->setNamespace("_{$this->project->getInternalId()}");
switch ($id) {
case 'projects':
try {
/**
* Create 'passwordHistory' attribute
*/
$this->createAttributeFromCollection($this->projectDB, $id, 'smtp');
$this->createAttributeFromCollection($this->projectDB, $id, 'templates');
$this->projectDB->deleteCachedCollection($id);
} catch (\Throwable $th) {
Console::warning("'SMTP and Templates' from {$id}: {$th->getMessage()}");
}
break;
default:
break;
}
2023-06-08 07:53:40 +00:00
if (!in_array($id, ['files', 'collections'])) {
$this->alterPermissionIndex($id);
}
2023-03-17 01:21:46 +00:00
usleep(50000);
}
}
/**
* Fix run on each document
*
2023-06-06 15:36:05 +00:00
* @param Document $document
* @return Document
2023-03-17 01:21:46 +00:00
*/
2023-06-06 15:36:05 +00:00
protected function fixDocument(Document $document): Document
2023-03-17 01:21:46 +00:00
{
switch ($document->getCollection()) {
case 'projects':
/**
* Bump version number.
*/
$document->setAttribute('version', '1.4.0');
$document->setAttribute('smtp', []);
$document->setAttribute('templates', []);
break;
}
return $document;
}
2023-06-06 15:36:05 +00:00
protected function alterPermissionIndex($collectionName): void
{
2023-06-07 06:46:24 +00:00
try {
$table = "`{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getInternalId()}_{$collectionName}_perms";
$this->pdo->prepare("
ALTER TABLE {$table}
DROP INDEX `_permission`,
ADD INDEX `_permission` (`_permission`, `_type`, `_document`);
2023-06-11 07:59:44 +00:00
")->execute();
2023-06-06 15:36:05 +00:00
} catch (\Throwable $th) {
Console::warning($th->getMessage());
}
}
2023-06-06 15:47:11 +00:00
/**
* Migrating all Bucket tables.
*
* @return void
* @throws \Exception
* @throws \PDOException
*/
protected function migrateBuckets(): void
{
foreach ($this->documentsIterator('buckets') as $bucket) {
$id = "bucket_{$bucket->getInternalId()}";
2023-06-08 07:53:40 +00:00
Console::log("Migrating Bucket {$id} {$bucket->getId()} ({$bucket->getAttribute('name')})");
2023-06-06 15:47:11 +00:00
$this->alterPermissionIndex($id);
}
}
2023-03-17 01:21:46 +00:00
}