new: [migration] Added migrations scripts

pull/54/head
mokaddem 2021-06-01 15:20:24 +02:00
parent dc38a11c65
commit 29c9b57ed2
4 changed files with 373 additions and 0 deletions

View File

@ -0,0 +1,208 @@
<?php
declare(strict_types=1);
use Migrations\AbstractMigration;
class UniqUUIDAndConstraints extends AbstractMigration
{
private $tablesRequiringUUIDMigration = [
'auth_keys',
'broods',
'encryption_keys',
'individuals',
'meta_fields',
'meta_templates',
'organisations',
'roles',
'sharing_groups',
'users',
];
public function up()
{
foreach ($this->tablesRequiringUUIDMigration as $table) {
$table = $this->table($table);
$this->migrateUUID($table)->update();
}
// We don't need these table as we'll move to a more generic approach
$this->table('alignment_tags')->drop()->save();
$this->table('individual_encryption_keys')->drop()->save();
$this->table('organisation_encryption_keys')->drop()->save();
// If a user is deleted, so you its auth_keys
$this->table('auth_keys')
->addForeignKey('user_id', 'users', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
->update();
// Should an encryption_keys be tied to a user? In the UI it says to individuals/organisations but you can only set it from the user profile.
$this->table('encryption_keys')
->renameColumn('owner_type', 'owner_model') // less confusing name & make it its length future-proof
->changeColumn('owner_model', 'string', [
'length' => 40,
'default' => null,
'null' => false,
])
->update();
// A meta_field belongs to both a template & a template field. If one of them is removed, so should the meta field
// (We don't want floating meta_fields as there is no way to link it back to its corresponding template)
$this->table('meta_fields')
->addForeignKey('meta_template_id', 'meta_templates', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
->addForeignKey('meta_template_field_id', 'meta_template_fields', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
->update();
// sgo being an mapping table, we don't need an ID. We can use the two other FK as a composed PK
$sgo = $this->table('sgo');
if ($sgo->hasColumn('id')) {
$sgo
->changePrimaryKey(['sharing_group_id', 'organisation_id'])
->removeColumn('id')
->update();
}
// A sharing group belongs to both a user & an organisation. If one of them is removed, so should the sharing group
// (A sharing group without owner should not exists)
$this->table('sharing_groups')
->addForeignKey('user_id', 'users', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
->addForeignKey('organisation_id', 'organisations', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
->update();
}
public function down()
{
/**
* Operations not recoverable during a rollback:
* - Dropped tables (alignment_tags, individual_encryption_keys, organisation_encryption_keys)
* - sgo's `ID` primary key
*/
foreach ($this->tablesRequiringUUIDMigration as $table) {
$table = $this->table($table);
$this->roolbackUUID($table)->update();
}
$this->table('auth_keys')
->dropForeignKey('user_id')
->update();
$this->table('encryption_keys')
->renameColumn('owner_model', 'owner_type')
->changeColumn('owner_type', 'string', [
'length' => 20,
'default' => null,
'null' => false,
])
->update();
$this->table('meta_fields')
->dropForeignKey('meta_template_id')
->dropForeignKey('meta_template_field_id')
->update();
$this->table('sharing_groups')
->dropForeignKey('user_id')
->dropForeignKey('organisation_id')
->update();
}
// public function change()
// {
// if ($this->isMigratingUp()) {
// foreach ($this->tablesRequiringUUIDMigration as $table) {
// $table = $this->table($table);
// $this->migrateUUID($table)->update();
// }
// } else {
// foreach ($this->tablesRequiringUUIDMigration as $table) {
// $table = $this->table($table);
// $this->roolbackUUID($table)->update();
// }
// }
// if ($this->isMigratingUp()) {
// // We don't need these table as we'll move to a more generic approach
// $this->table('alignment_tags')->drop()->save();
// $this->table('individual_encryption_keys')->drop()->save();
// $this->table('organisation_encryption_keys')->drop()->save();
// }
// // If a user is deleted, so you its auth_keys
// $this->table('auth_keys')
// ->addForeignKey('user_id', 'users', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
// ->update();
// // Should an encryption_keys be tied to a user? In the UI it says to individuals/organisations but you can only set it from the user profile.
// $encryption_keys = $this->table('encryption_keys');
// $encryption_keys->renameColumn('owner_type', 'owner_model');
// if ($this->isMigratingUp()) {
// $encryption_keys->changeColumn('owner_model', 'string', [
// 'length' => 40,
// 'default' => null,
// 'null' => false,
// ])->update();
// } else {
// $encryption_keys->changeColumn('owner_type', 'string', [
// 'length' => 20,
// 'default' => null,
// 'null' => false,
// ])->update();
// }
// // A meta_field belongs to both a template & a template field. If one of them is removed, so should the meta field
// // (We don't want floating meta_fields as there is no way to link it back to its corresponding template)
// $this->table('meta_fields')
// ->addForeignKey('meta_template_id', 'meta_templates', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
// ->addForeignKey('meta_template_field_id', 'meta_template_fields', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
// ->update();
// // sgo being an mapping table, we don't need an ID. We can use the two other FK as a composed PK
// $sgo = $this->table('sgo');
// if ($this->isMigratingUp()) {
// $sgo->changePrimaryKey(['sharing_group_id', 'organisation_id'])
// ->removeColumn('id')
// ->update();
// } else {
// $sgo->addColumn('id', 'integer', [
// 'autoIncrement' => true,
// 'limit' => 10,
// 'signed' => false,
// ])
// ->changePrimaryKey('id')
// ->update();
// }
// // A sharing group belongs to both a user & an organisation. If one of them is removed, so should the sharing group
// // (A sharing group without owner should not exists)
// $this->table('sharing_groups')
// ->addForeignKey('user_id', 'users', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
// ->addForeignKey('organisation_id', 'organisations', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
// ->update();
// }
// Use cake's built-in uuid type and ensure unicity
private function migrateUUID($table)
{
$table->changeColumn('uuid', 'uuid', [
'default' => null,
'null' => false,
]);
if ($table->hasIndex('uuid')) {
$table->removeIndex(['uuid']); // remove existing non-unique index
}
$table->addIndex(['uuid'], ['unique' => true]);
return $table;
}
private function roolbackUUID($table)
{
$table->changeColumn('uuid', 'string', [
'limit' => 40,
'null' => true,
])->removeIndex(['uuid']);
return $table;
}
}

View File

@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
use Migrations\AbstractMigration;
use Phinx\Db\Adapter\MysqlAdapter;
class InboxSystem extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
* @return void
*/
public $autoId = false; // turn off automatic `id` column create. We want it to be `int(10) unsigned`
public function change()
{
$table = $this->table('inbox', [
'signed' => false,
'collation' => 'utf8mb4_unicode_ci',
]);
$table
->addColumn('id', 'integer', [
'autoIncrement' => true,
'limit' => 10,
'signed' => false,
])
->addPrimaryKey('id')
->addColumn('uuid', 'uuid', [
'default' => null,
'null' => false,
])
->addColumn('scope', 'string', [
'default' => null,
'null' => false,
'limit' => 191,
'comment' => 'The to model on which the request should be performed onto',
])
->addColumn('action', 'string', [
'default' => null,
'null' => false,
'limit' => 191,
'comment' => 'A specific action belonging to the model',
])
->addColumn('title', 'string', [
'default' => null,
'null' => false,
'limit' => 191,
])
->addColumn('origin', 'string', [
'default' => null,
'null' => false,
'limit' => 191,
])
// ->addColumn('ip', 'string', [
// 'limit' => 191,
// 'default' => null,
// 'null' => true,
// ])
->addColumn('user_id', 'integer', [
'default' => null,
'null' => true,
'signed' => false,
'length' => 10,
])
->addColumn('comment', 'text', [
'default' => null,
'null' => true,
])
->addColumn('description', 'text', [
'default' => null,
'null' => true,
])
->addColumn('data', 'text', [
'default' => null,
'null' => true,
'limit' => MysqlAdapter::TEXT_LONG
])
->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
]);
$table->addForeignKey('user_id', 'users', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE']);
$table->addIndex(['uuid'], ['unique' => true])
->addIndex('scope')
->addIndex('action')
->addIndex('title')
->addIndex('origin')
// ->addIndex('ip')
->addIndex('created')
->addIndex('user_id');
$table->create();
}
}

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
use Migrations\AbstractMigration;
class LocalTools extends AbstractMigration
{
public $autoId = false; // turn off automatic `id` column create. We want it to be `int(10) unsigned`
/**
* Change Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('local_tools', [
'signed' => false,
'collation' => 'utf8mb4_unicode_ci',
]);
$table
->addColumn('id', 'integer', [
'autoIncrement' => true,
'limit' => 10,
'signed' => false,
])
->addPrimaryKey('id')
->addColumn('name', 'string', [
'default' => null,
'null' => false,
'limit' => 191,
'comment' => 'The name of the individual connection',
])
->addColumn('connector', 'string', [
'default' => null,
'null' => false,
'limit' => 191,
'comment' => 'The library name used for the connection',
])
->addColumn('settings', 'text', [
'default' => null,
'null' => true,
])
->addColumn('exposed', 'boolean', [
'default' => 0,
'null' => false,
])
->addColumn('description', 'text', [
'default' => null,
'null' => true,
]);
$table->addIndex('name')
->addIndex('connector');
$table->create();
}
}

File diff suppressed because one or more lines are too long