3 namespace BookStack\Console\Commands;
5 use Illuminate\Console\Command;
6 use Illuminate\Support\Facades\DB;
8 class UpgradeDatabaseEncodingCommand extends Command
11 * The name and signature of the console command.
15 protected $signature = 'bookstack:db-utf8mb4
16 {--database= : The database connection to use}';
19 * The console command description.
23 protected $description = 'Generate SQL commands to upgrade the database to UTF8mb4';
27 * Execute the console command.
29 public function handle(): int
31 $connection = DB::getDefaultConnection();
32 if ($this->option('database') !== null) {
33 DB::setDefaultConnection($this->option('database'));
36 $database = DB::getDatabaseName();
37 $tables = DB::select('SHOW TABLES');
38 $this->line('ALTER DATABASE `' . $database . '` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
39 $this->line('USE `' . $database . '`;');
40 $key = 'Tables_in_' . $database;
41 foreach ($tables as $table) {
42 $tableName = $table->$key;
43 $this->line("ALTER TABLE `{$tableName}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
46 DB::setDefaultConnection($connection);