]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/UpgradeDatabaseEncoding.php
Merge branch 'openid' of https://github.com/jasperweyne/BookStack into jasperweyne...
[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     public function __construct()
28     {
29         parent::__construct();
30     }
31
32     /**
33      * Execute the console command.
34      *
35      * @return mixed
36      */
37     public function handle()
38     {
39         $connection = DB::getDefaultConnection();
40         if ($this->option('database') !== null) {
41             DB::setDefaultConnection($this->option('database'));
42         }
43
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;');
52         }
53
54         DB::setDefaultConnection($connection);
55     }
56 }