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.
27 public function __construct()
29 parent::__construct();
33 * Execute the console command.
37 public function handle()
39 $connection = DB::getDefaultConnection();
40 if ($this->option('database') !== null) {
41 DB::setDefaultConnection($this->option('database'));
44 $database = DB::getDatabaseName();
45 $tables = DB::select('SHOW TABLES');
46 $this->line('ALTER DATABASE `' . $database . '` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
47 $this->line('USE `' . $database . '`;');
48 $key = 'Tables_in_' . $database;
49 foreach ($tables as $table) {
50 $tableName = $table->$key;
51 $this->line('ALTER TABLE `' . $tableName . '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
54 DB::setDefaultConnection($connection);