mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Merge pull request #5334 from appwrite/feat-db-update-migrations
Feat db update migrations
This commit is contained in:
@@ -37,6 +37,8 @@ abstract class Migration
|
||||
*/
|
||||
protected Database $consoleDB;
|
||||
|
||||
protected \PDO $pdo;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@@ -99,6 +101,13 @@ abstract class Migration
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPDO(\PDO $pdo): self
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates through every document.
|
||||
*
|
||||
@@ -332,6 +341,25 @@ abstract class Migration
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change a collection attribute's internal type
|
||||
*
|
||||
* @param string $collection
|
||||
* @param string $attribute
|
||||
* @param string $type
|
||||
* @return void
|
||||
*/
|
||||
protected function changeAttributeInternalType(string $collection, string $attribute, string $type): void
|
||||
{
|
||||
$stmt = $this->pdo->prepare("ALTER TABLE `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getInternalId()}_{$collection}` MODIFY `$attribute` $type;");
|
||||
|
||||
try {
|
||||
$stmt->execute();
|
||||
} catch (\Exception $e) {
|
||||
Console::warning($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes migration for set project.
|
||||
*/
|
||||
|
||||
@@ -17,11 +17,6 @@ use Utopia\Database\Helpers\Role;
|
||||
|
||||
class V15 extends Migration
|
||||
{
|
||||
/**
|
||||
* @var \PDO $pdo
|
||||
*/
|
||||
private $pdo;
|
||||
|
||||
/**
|
||||
* @var array<string>
|
||||
*/
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
|
||||
namespace Appwrite\Migration\Version;
|
||||
|
||||
use Appwrite\Auth\Auth;
|
||||
use Appwrite\Migration\Migration;
|
||||
use Utopia\CLI\Console;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
|
||||
@@ -13,10 +11,11 @@ class V18 extends Migration
|
||||
{
|
||||
public function execute(): void
|
||||
{
|
||||
|
||||
/**
|
||||
* Disable SubQueries for Performance.
|
||||
*/
|
||||
foreach (['subQueryIndexes', 'subQueryPlatforms', 'subQueryDomains', 'subQueryKeys', 'subQueryWebhooks', 'subQuerySessions', 'subQueryTokens', 'subQueryMemberships', 'subqueryVariables'] as $name) {
|
||||
foreach (['subQueryIndexes', 'subQueryPlatforms', 'subQueryDomains', 'subQueryKeys', 'subQueryWebhooks', 'subQuerySessions', 'subQueryTokens', 'subQueryMemberships', 'subQueryVariables'] as $name) {
|
||||
Database::addFilter(
|
||||
$name,
|
||||
fn () => null,
|
||||
@@ -25,12 +24,44 @@ class V18 extends Migration
|
||||
}
|
||||
|
||||
Console::log('Migrating Project: ' . $this->project->getAttribute('name') . ' (' . $this->project->getId() . ')');
|
||||
$this->projectDB->setNamespace("_{$this->project->getInternalId()}");
|
||||
|
||||
Console::info('Migrating Databases');
|
||||
$this->migrateDatabases();
|
||||
|
||||
Console::info('Migrating Collections');
|
||||
$this->migrateCollections();
|
||||
|
||||
Console::info('Migrating Documents');
|
||||
$this->forEachDocument([$this, 'fixDocument']);
|
||||
$this->forEachDocument(function (Document $document) {
|
||||
$this->migrateDocument($document);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate all Databases.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function migrateDatabases(): void
|
||||
{
|
||||
foreach ($this->documentsIterator('databases') as $database) {
|
||||
$databaseTable = "database_{$database->getInternalId()}";
|
||||
|
||||
Console::info("Migrating Collections of {$database->getId()} ({$database->getAttribute('name')})");
|
||||
|
||||
foreach ($this->documentsIterator($databaseTable) as $collection) {
|
||||
$collectionTable = "{$databaseTable}_collection_{$collection->getInternalId()}";
|
||||
|
||||
foreach ($collection['attributes'] ?? [] as $attribute) {
|
||||
if ($attribute['type'] !== Database::VAR_FLOAT) {
|
||||
continue;
|
||||
}
|
||||
$this->changeAttributeInternalType($collectionTable, $attribute['key'], 'DOUBLE');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,14 +69,19 @@ class V18 extends Migration
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function migrateCollections(): void
|
||||
private function migrateCollections(): void
|
||||
{
|
||||
foreach ($this->collections as $collection) {
|
||||
$id = $collection['$id'];
|
||||
|
||||
Console::log("Migrating Collection \"{$id}\"");
|
||||
|
||||
$this->projectDB->setNamespace("_{$this->project->getInternalId()}");
|
||||
foreach ($collection['attributes'] ?? [] as $attribute) {
|
||||
if ($attribute['type'] !== Database::VAR_FLOAT) {
|
||||
continue;
|
||||
}
|
||||
$this->changeAttributeInternalType($id, $attribute['$id'], 'DOUBLE');
|
||||
}
|
||||
|
||||
switch ($id) {
|
||||
case 'users':
|
||||
@@ -59,6 +95,17 @@ class V18 extends Migration
|
||||
Console::warning("'passwordHistory' from {$id}: {$th->getMessage()}");
|
||||
}
|
||||
break;
|
||||
case 'teams':
|
||||
try {
|
||||
/**
|
||||
* Create 'prefs' attribute
|
||||
*/
|
||||
$this->createAttributeFromCollection($this->projectDB, $id, 'prefs');
|
||||
$this->projectDB->deleteCachedCollection($id);
|
||||
} catch (\Throwable $th) {
|
||||
Console::warning("'prefs' from {$id}: {$th->getMessage()}");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -70,10 +117,10 @@ class V18 extends Migration
|
||||
/**
|
||||
* Fix run on each document
|
||||
*
|
||||
* @param \Utopia\Database\Document $document
|
||||
* @return \Utopia\Database\Document
|
||||
* @param Document $document
|
||||
* @return Document
|
||||
*/
|
||||
protected function fixDocument(Document $document)
|
||||
private function migrateDocument(Document $document): Document
|
||||
{
|
||||
switch ($document->getCollection()) {
|
||||
case 'projects':
|
||||
@@ -96,6 +143,12 @@ class V18 extends Migration
|
||||
*/
|
||||
$document->setAttribute('passwordHistory', []);
|
||||
break;
|
||||
case 'teams':
|
||||
/**
|
||||
* Default prefs
|
||||
*/
|
||||
$document->setAttribute('prefs', new \stdClass());
|
||||
break;
|
||||
}
|
||||
|
||||
return $document;
|
||||
|
||||
Reference in New Issue
Block a user