1
0
Fork 0
mirror of synced 2024-06-01 10:29:48 +12:00

fix: migration

This commit is contained in:
Torsten Dittmann 2022-05-13 14:49:31 +02:00
parent 50415d5b37
commit 39b994c958
3 changed files with 65 additions and 50 deletions

View file

@ -8,6 +8,8 @@ use Utopia\Database\Database;
use Utopia\CLI\Console;
use Utopia\Config\Config;
use Exception;
use Utopia\App;
use Utopia\Database\Validator\Authorization;
abstract class Migration
{
@ -50,15 +52,20 @@ abstract class Migration
public function __construct()
{
Authorization::disable();
Authorization::setDefaultStatus(false);
$this->collections = array_merge([
'_metadata' => [
'$id' => '_metadata'
'$id' => '_metadata',
'$collection' => Database::METADATA
],
'audit' => [
'$id' => 'audit'
'$id' => 'audit',
'$collection' => Database::METADATA
],
'abuse' => [
'$id' => 'abuse'
'$id' => 'abuse',
'$collection' => Database::METADATA
]
], Config::getParam('collections', []));
}
@ -93,6 +100,7 @@ abstract class Migration
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
foreach ($this->collections as $collection) {
if ($collection['$collection'] !== Database::METADATA) return;
$sum = 0;
$nextDocument = null;
$collectionCount = $this->projectDB->count($collection['$id']);
@ -189,6 +197,53 @@ abstract class Migration
return $result;
}
/**
* Creates colletion from the config collection.
*
* @param string $id
* @param string|null $name
* @return void
* @throws \Throwable
*/
protected function createCollection(string $id, string $name = null): void
{
$name ??= $id;
if (!$this->projectDB->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), $name)) {
$attributes = [];
$indexes = [];
$collection = $this->collections[$id];
foreach ($collection['attributes'] as $attribute) {
$attributes[] = new Document([
'$id' => $attribute['$id'],
'type' => $attribute['type'],
'size' => $attribute['size'],
'required' => $attribute['required'],
'signed' => $attribute['signed'],
'array' => $attribute['array'],
'filters' => $attribute['filters'],
]);
}
foreach ($collection['indexes'] as $index) {
$indexes[] = new Document([
'$id' => $index['$id'],
'type' => $index['type'],
'attributes' => $index['attributes'],
'lengths' => $index['lengths'],
'orders' => $index['orders'],
]);
}
try {
$this->projectDB->createCollection($name, $attributes, $indexes);
} catch (\Throwable $th) {
throw $th;
}
}
}
/**
* Executes migration for set project.
*/

View file

@ -297,53 +297,6 @@ class V12 extends Migration
}
}
/**
* Creates colletion from the config collection.
*
* @param string $id
* @param string|null $name
* @return void
* @throws \Throwable
*/
protected function createCollection(string $id, string $name = null): void
{
$name ??= $id;
if (!$this->projectDB->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), $name)) {
$attributes = [];
$indexes = [];
$collection = $this->collections[$id];
foreach ($collection['attributes'] as $attribute) {
$attributes[] = new Document([
'$id' => $attribute['$id'],
'type' => $attribute['type'],
'size' => $attribute['size'],
'required' => $attribute['required'],
'signed' => $attribute['signed'],
'array' => $attribute['array'],
'filters' => $attribute['filters'],
]);
}
foreach ($collection['indexes'] as $index) {
$indexes[] = new Document([
'$id' => $index['$id'],
'type' => $index['type'],
'attributes' => $index['attributes'],
'lengths' => $index['lengths'],
'orders' => $index['orders'],
]);
}
try {
$this->projectDB->createCollection($name, $attributes, $indexes);
} catch (\Throwable $th) {
throw $th;
}
}
}
/**
* Migrates permissions to dedicated table.
*

View file

@ -177,6 +177,13 @@ class V13 extends Migration
case 'stats':
//TODO: migrate value (size => 8)
break;
case 'tokens':
try {
$this->createCollection('tokens');
} catch (\Throwable $th) {
Console::warning("'tokens': {$th->getMessage()}");
}
break;
}
usleep(100000);
}