mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-27 12:41:58 +00:00
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
This commit is contained in:
parent
433cb9b3b2
commit
6bcd89acf7
57
app/Console/Commands/UpgradeDatabaseEncoding.php
Normal file
57
app/Console/Commands/UpgradeDatabaseEncoding.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BookStack\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class UpgradeDatabaseEncoding extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'bookstack:db-utf8mb4-syntax {--database= : The database connection to use.}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Generate SQL commands to upgrade the database to UTF8mb4';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$connection = DB::getDefaultConnection();
|
||||||
|
if ($this->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);
|
||||||
|
}
|
||||||
|
}
|
@ -15,7 +15,8 @@ class Kernel extends ConsoleKernel
|
|||||||
Commands\ClearActivity::class,
|
Commands\ClearActivity::class,
|
||||||
Commands\ClearRevisions::class,
|
Commands\ClearRevisions::class,
|
||||||
Commands\RegeneratePermissions::class,
|
Commands\RegeneratePermissions::class,
|
||||||
Commands\RegenerateSearch::class
|
Commands\RegenerateSearch::class,
|
||||||
|
Commands\UpgradeDatabaseEncoding::class
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
class UpdateDbEncodingToUt8mb4 extends Migration
|
class UpdateDbEncodingToUt8mb4 extends Migration
|
||||||
@ -13,16 +11,9 @@ class UpdateDbEncodingToUt8mb4 extends Migration
|
|||||||
*/
|
*/
|
||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
$database = DB::getDatabaseName();
|
// Migration removed due to issues during live migration.
|
||||||
$tables = DB::select('SHOW TABLES');
|
// Instead you can run the command `artisan bookstack:db-utf8mb4-syntax`
|
||||||
$pdo = DB::getPdo();
|
// which will generate out the SQL request to upgrade your DB to utf8mb4.
|
||||||
$pdo->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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,15 +23,6 @@ class UpdateDbEncodingToUt8mb4 extends Migration
|
|||||||
*/
|
*/
|
||||||
public function down()
|
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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user