From 88c129dc2c65815b524ddb1da82b482b734cf9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Thu, 1 Jul 2021 16:27:20 +0200 Subject: [PATCH] Fix #15 Create a migration script to merge the SQLite and MySQL existing databases to the FlexiAPI one Fix a rollback issue in a migration --- .../app/Console/Commands/ImportDatabase.php | 217 ++++++++++++++++++ .../2014_10_12_000000_create_users_table.php | 2 +- flexisip-account-manager.spec | 2 +- 3 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 flexiapi/app/Console/Commands/ImportDatabase.php diff --git a/flexiapi/app/Console/Commands/ImportDatabase.php b/flexiapi/app/Console/Commands/ImportDatabase.php new file mode 100644 index 0000000..0052c25 --- /dev/null +++ b/flexiapi/app/Console/Commands/ImportDatabase.php @@ -0,0 +1,217 @@ +addConnection([ + 'driver' => $this->option('type'), + 'host' => $this->option('host'), + 'database' => $this->argument('dbname'), + 'username' => $this->option('username'), + 'password' => $this->option('password'), + 'port' => $this->option('port'), + 'charset' => 'utf8', + 'collation' => 'utf8_unicode_ci', + 'prefix' => '', + ], 'default'); + + $capsule->addConnection([ + 'driver' => 'sqlite', + 'database' => $this->argument('sqlitefilepath'), + ], 'sqlite'); + + $capsule->setAsGlobal(); + + // Ensure that the target database is empty + if (Account::count() > 0) { + $this->error('An empty database is required to run the migration'); + return 1; + } + + $accountsCount = Capsule::table('accounts')->count(); + + if ($this->confirm($accountsCount . ' accounts will be migrated : Do you wish to continue?')) { + // Accounts + + $this->info('Migrating the accounts'); + + $pages = $accountsCount / $this->_pagination; + $bar = $this->output->createProgressBar($pages); + + for ($page = 0; $page <= $pages; $page++) { + $originAccounts = Capsule::table('accounts') + ->take($this->_pagination) + ->skip($page*$this->_pagination) + ->get() + ->map(function ($element) { + // Fix bad creation_time + $creationTime = strtotime($element->creation_time); + if ($creationTime == false || $creationTime < 0) { + $element->creation_time = gmdate('Y-m-d H:i:s', 1); + } + return (array)$element; + }) + ->toArray(); + + Account::insert($originAccounts); + + $bar->advance(); + } + + $bar->finish(); + + $this->newLine(); + + $this->disableForeignKeyCheck(); + + // Passwords + $this->info('Migrating the passwords'); + + $pages = Capsule::table('accounts')->count() / $this->_pagination; + $bar = $this->output->createProgressBar($pages); + + for ($page = 0; $page <= $pages; $page++) { + $originPasswords = Capsule::table('passwords') + ->take($this->_pagination) + ->skip($page*$this->_pagination) + ->get() + ->map(function ($element) { + return (array)$element; + }) + ->toArray(); + + Password::insert($originPasswords); + + $bar->advance(); + } + + $bar->finish(); + + $this->newLine(); + + // Aliases + $this->info('Migrating the aliases'); + + $pages = Capsule::table('aliases')->count() / $this->_pagination; + $bar = $this->output->createProgressBar($pages); + + for ($page = 0; $page <= $pages; $page++) { + $originAliases = Capsule::table('aliases') + ->take($this->_pagination) + ->skip($page*$this->_pagination) + ->get() + ->map(function ($element) { + return (array)$element; + }) + ->toArray(); + + Alias::insert($originAliases); + + $bar->advance(); + } + + $bar->finish(); + + $this->newLine(); + + $this->info('Migrating the admins'); + + $originAdmins = Capsule::connection('sqlite') + ->table('admins') + ->get() + ->map(function ($element) { + return (array)$element; + }) + ->toArray(); + Admin::insert($originAdmins); + + $this->info('Migrating the api keys'); + + $originApiKeys = Capsule::connection('sqlite') + ->table('api_keys') + ->get() + ->map(function ($element) { + return (array)$element; + }) + ->toArray(); + ApiKey::insert($originApiKeys); + + $this->info('Migrating the nonces'); + + $originNonces = Capsule::connection('sqlite') + ->table('nonces') + ->get() + ->map(function ($element) { + return (array)$element; + }) + ->toArray(); + DigestNonce::insert($originNonces); + + $this->info('Migrating the email changed'); + + $originEmailChanged = Capsule::connection('sqlite') + ->table('email_changed') + ->get() + ->map(function ($element) { + return (array)$element; + }) + ->toArray(); + EmailChanged::insert($originEmailChanged); + + $this->info('Migrating the phone change code'); + + $originPhoneChangeCodes = Capsule::connection('sqlite') + ->table('phone_change_codes') + ->get() + ->map(function ($element) { + return (array)$element; + }) + ->toArray(); + PhoneChangeCode::insert($originPhoneChangeCodes); + + $this->enableForeignKeyCheck(); + + $this->newLine(); + $this->info('Databases migrated'); + } + + return 0; + } +} diff --git a/flexiapi/database/migrations/2014_10_12_000000_create_users_table.php b/flexiapi/database/migrations/2014_10_12_000000_create_users_table.php index 1dc8440..daff40d 100644 --- a/flexiapi/database/migrations/2014_10_12_000000_create_users_table.php +++ b/flexiapi/database/migrations/2014_10_12_000000_create_users_table.php @@ -21,6 +21,6 @@ class CreateUsersTable extends Migration public function down() { - Schema::connection('local')->dropIfExists('users'); + Schema::dropIfExists('users'); } } diff --git a/flexisip-account-manager.spec b/flexisip-account-manager.spec index 7b61ea3..79d157b 100644 --- a/flexisip-account-manager.spec +++ b/flexisip-account-manager.spec @@ -8,7 +8,7 @@ #%define _datadir %{_datarootdir} #%define _docdir %{_datadir}/doc -%define build_number 82 +%define build_number 83 %define var_dir /var/opt/belledonne-communications %define opt_dir /opt/belledonne-communications/share/flexisip-account-manager