]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/UpgradeDatabaseEncoding.php
Aligned command class code
[bookstack] / app / Console / Commands / UpgradeDatabaseEncoding.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use Illuminate\Console\Command;
6 use Illuminate\Support\Facades\DB;
7
8 class UpgradeDatabaseEncoding extends Command
9 {
10     /**
11      * The name and signature of the console command.
12      *
13      * @var string
14      */
15     protected $signature = 'bookstack:db-utf8mb4 
16                             {--database= : The database connection to use}';
17
18     /**
19      * The console command description.
20      *
21      * @var string
22      */
23     protected $description = 'Generate SQL commands to upgrade the database to UTF8mb4';
24
25
26     /**
27      * Execute the console command.
28      */
29     public function handle(): int
30     {
31         $connection = DB::getDefaultConnection();
32         if ($this->option('database') !== null) {
33             DB::setDefaultConnection($this->option('database'));
34         }
35
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;");
44         }
45
46         DB::setDefaultConnection($connection);
47
48         return 0;
49     }
50 }