2017-07-22 10:54:17 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
2023-05-24 08:21:46 -04:00
|
|
|
class UpgradeDatabaseEncodingCommand extends Command
|
2017-07-22 10:54:17 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-05-24 07:59:50 -04:00
|
|
|
protected $signature = 'bookstack:db-utf8mb4
|
|
|
|
{--database= : The database connection to use}';
|
2017-07-22 10:54:17 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Generate SQL commands to upgrade the database to UTF8mb4';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
2023-05-24 07:59:50 -04:00
|
|
|
public function handle(): int
|
2017-07-22 10:54:17 -04:00
|
|
|
{
|
|
|
|
$connection = DB::getDefaultConnection();
|
|
|
|
if ($this->option('database') !== null) {
|
|
|
|
DB::setDefaultConnection($this->option('database'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$database = DB::getDatabaseName();
|
|
|
|
$tables = DB::select('SHOW TABLES');
|
2021-06-26 11:23:15 -04:00
|
|
|
$this->line('ALTER DATABASE `' . $database . '` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
|
|
|
|
$this->line('USE `' . $database . '`;');
|
2017-07-22 10:54:17 -04:00
|
|
|
$key = 'Tables_in_' . $database;
|
|
|
|
foreach ($tables as $table) {
|
|
|
|
$tableName = $table->$key;
|
2023-05-24 07:59:50 -04:00
|
|
|
$this->line("ALTER TABLE `{$tableName}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
|
2017-07-22 10:54:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DB::setDefaultConnection($connection);
|
2023-05-24 07:59:50 -04:00
|
|
|
|
|
|
|
return 0;
|
2017-07-22 10:54:17 -04:00
|
|
|
}
|
|
|
|
}
|