From 6bcd89acf76163ca09c95903f27ceed925718061 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 22 Jul 2017 15:54:17 +0100 Subject: [PATCH] Moved utf8mb4 migration to command instead of migration To prevent errors upon migration. Command generates out the SQL syntax to make the change instead so the upgrade can be done manually. In reference to #425 --- .../Commands/UpgradeDatabaseEncoding.php | 57 +++++++++++++++++++ app/Console/Kernel.php | 3 +- ...02_152834_update_db_encoding_to_ut8mb4.php | 26 ++------- 3 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 app/Console/Commands/UpgradeDatabaseEncoding.php diff --git a/app/Console/Commands/UpgradeDatabaseEncoding.php b/app/Console/Commands/UpgradeDatabaseEncoding.php new file mode 100644 index 000000000..dbdf778e8 --- /dev/null +++ b/app/Console/Commands/UpgradeDatabaseEncoding.php @@ -0,0 +1,57 @@ +option('database') !== null) { + DB::setDefaultConnection($this->option('database')); + } + + $database = DB::getDatabaseName(); + $tables = DB::select('SHOW TABLES'); + $this->line('ALTER DATABASE `'.$database.'` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'); + $this->line('USE `'.$database.'`;'); + $key = 'Tables_in_' . $database; + foreach ($tables as $table) { + $tableName = $table->$key; + $this->line('ALTER TABLE `'.$tableName.'` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'); + } + + DB::setDefaultConnection($connection); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 4fa0b3c80..af9f5fd46 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,7 +15,8 @@ class Kernel extends ConsoleKernel Commands\ClearActivity::class, Commands\ClearRevisions::class, Commands\RegeneratePermissions::class, - Commands\RegenerateSearch::class + Commands\RegenerateSearch::class, + Commands\UpgradeDatabaseEncoding::class ]; /** diff --git a/database/migrations/2017_07_02_152834_update_db_encoding_to_ut8mb4.php b/database/migrations/2017_07_02_152834_update_db_encoding_to_ut8mb4.php index 550c95826..259da0720 100644 --- a/database/migrations/2017_07_02_152834_update_db_encoding_to_ut8mb4.php +++ b/database/migrations/2017_07_02_152834_update_db_encoding_to_ut8mb4.php @@ -1,7 +1,5 @@ setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); - $pdo->exec('ALTER DATABASE `'.$database.'` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'); - $key = 'Tables_in_' . $database; - foreach ($tables as $table) { - $tableName = $table->$key; - $pdo->exec('ALTER TABLE `'.$tableName.'` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'); - } + // Migration removed due to issues during live migration. + // Instead you can run the command `artisan bookstack:db-utf8mb4-syntax` + // which will generate out the SQL request to upgrade your DB to utf8mb4. } /** @@ -32,15 +23,6 @@ class UpdateDbEncodingToUt8mb4 extends Migration */ public function down() { - $database = DB::getDatabaseName(); - $tables = DB::select('SHOW TABLES'); - $pdo = DB::getPdo(); - $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); - $pdo->exec('ALTER DATABASE `'.$database.'` CHARACTER SET utf8 COLLATE utf8_unicode_ci'); - $key = 'Tables_in_' . $database; - foreach ($tables as $table) { - $tableName = $table->$key; - $pdo->exec('ALTER TABLE `'.$tableName.'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci'); - } + // } }