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

106 lines
3.1 KiB
PHP
Raw Normal View History

2022-12-26 12:06:35 +00:00
<?php
namespace Appwrite\Migration\Version;
2022-12-27 07:35:55 +00:00
use Appwrite\Auth\Auth;
2022-12-26 12:06:35 +00:00
use Appwrite\Migration\Migration;
use Utopia\CLI\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
2022-12-26 12:16:01 +00:00
class V17 extends Migration
2022-12-26 12:06:35 +00:00
{
public function execute(): void
{
/**
* Disable SubQueries for Performance.
*/
foreach (['subQueryIndexes', 'subQueryPlatforms', 'subQueryDomains', 'subQueryKeys', 'subQueryWebhooks', 'subQuerySessions', 'subQueryTokens', 'subQueryMemberships', 'subqueryVariables'] as $name) {
Database::addFilter(
$name,
fn () => null,
fn () => []
);
}
Console::log('Migrating Project: ' . $this->project->getAttribute('name') . ' (' . $this->project->getId() . ')');
Console::info('Migrating Collections');
$this->migrateCollections();
2022-12-27 07:35:55 +00:00
Console::info('Migrating Documents');
$this->forEachDocument([$this, 'fixDocument']);
2022-12-26 12:06:35 +00:00
}
/**
* Migrate all Collections.
*
* @return void
*/
protected function migrateCollections(): void
{
foreach ($this->collections as $collection) {
$id = $collection['$id'];
Console::log("Migrating Collection \"{$id}\"");
$this->projectDB->setNamespace("_{$this->project->getInternalId()}");
switch ($id) {
2022-12-27 07:24:34 +00:00
case 'files':
try {
/**
* Update 'mimeType' attribute size (127->255)
*/
$this->projectDB->updateAttribute($id, 'mimeType', Database::VAR_STRING, 255, true, false);
$this->projectDB->deleteCachedCollection($id);
} catch (\Throwable $th) {
Console::warning("'mimeType' from {$id}: {$th->getMessage()}");
}
break;
2022-12-26 12:06:35 +00:00
default:
break;
}
usleep(50000);
}
}
/**
* Fix run on each document
*
* @param \Utopia\Database\Document $document
* @return \Utopia\Database\Document
*/
protected function fixDocument(Document $document)
{
2022-12-27 07:35:55 +00:00
switch ($document->getCollection()) {
case 'projects':
/**
* Bump version number.
*/
$document->setAttribute('version', '1.1.0');
/**
* Set default maxSessions
*/
$document->setAttribute('auths', array_merge($document->getAttribute('auths', []), [
'maxSessions' => APP_LIMIT_USER_SESSIONS_DEFAULT
]));
break;
case 'users':
/**
* Set hashOptions type
*/
$document->setAttribute('hashOptions', array_merge($document->getAttribute('hashOptions', []), [
'type' => $document->getAttribute('hash', Auth::DEFAULT_ALGO)
]));
break;
}
2022-12-26 12:06:35 +00:00
return $document;
}
}