3 namespace BookStack\Console\Commands;
5 use Illuminate\Console\Command;
6 use Illuminate\Support\Facades\DB;
8 class UpgradeDatabaseEncoding extends Command
11 * The name and signature of the console command.
15 protected $signature = 'bookstack:db-utf8mb4 {--database= : The database connection to use.}';
18 * The console command description.
22 protected $description = 'Generate SQL commands to upgrade the database to UTF8mb4';
25 * Create a new command instance.
28 public function __construct()
30 parent::__construct();
34 * Execute the console command.
38 public function handle()
40 $connection = DB::getDefaultConnection();
41 if ($this->option('database') !== null) {
42 DB::setDefaultConnection($this->option('database'));
45 $database = DB::getDatabaseName();
46 $tables = DB::select('SHOW TABLES');
47 $this->line('ALTER DATABASE `'.$database.'` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
48 $this->line('USE `'.$database.'`;');
49 $key = 'Tables_in_' . $database;
50 foreach ($tables as $table) {
51 $tableName = $table->$key;
52 $this->line('ALTER TABLE `'.$tableName.'` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
55 DB::setDefaultConnection($connection);