]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/UpgradeDatabaseEncoding.php
a17fc952351df5103709c0c2406f8287ac140215
[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 {--database= : The database connection to use.}';
16
17     /**
18      * The console command description.
19      *
20      * @var string
21      */
22     protected $description = 'Generate SQL commands to upgrade the database to UTF8mb4';
23
24     /**
25      * Create a new command instance.
26      *
27      */
28     public function __construct()
29     {
30         parent::__construct();
31     }
32
33     /**
34      * Execute the console command.
35      *
36      * @return mixed
37      */
38     public function handle()
39     {
40         $connection = DB::getDefaultConnection();
41         if ($this->option('database') !== null) {
42             DB::setDefaultConnection($this->option('database'));
43         }
44
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;');
53         }
54
55         DB::setDefaultConnection($connection);
56     }
57 }