2017-07-14 02:45:59 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Console\Services\Database;
|
2019-09-17 01:08:26 +08:00
|
|
|
use App\Contracts\Command;
|
2019-09-13 20:05:02 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-08-11 08:42:35 +08:00
|
|
|
use Tivie\OS\Detector;
|
2017-07-14 04:43:56 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class CreateDatabase extends Command
|
2017-07-14 02:45:59 +08:00
|
|
|
{
|
2020-02-09 02:29:34 +08:00
|
|
|
protected $signature = 'database:create {--reset} {--migrate} {--conn=?}';
|
2017-07-14 02:45:59 +08:00
|
|
|
protected $description = 'Create a database';
|
2017-12-06 04:59:31 +08:00
|
|
|
protected $os;
|
2017-07-14 02:45:59 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* CreateDatabase constructor.
|
|
|
|
*/
|
2017-07-14 02:45:59 +08:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2019-08-11 08:42:35 +08:00
|
|
|
$this->os = new Detector();
|
2017-07-14 02:45:59 +08:00
|
|
|
}
|
|
|
|
|
2017-12-06 04:59:31 +08:00
|
|
|
/**
|
2018-01-10 10:47:03 +08:00
|
|
|
* Create the mysql database
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-06 04:59:31 +08:00
|
|
|
* @param $dbkey
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-10 10:47:03 +08:00
|
|
|
* @return bool
|
2017-12-06 04:59:31 +08:00
|
|
|
*/
|
2017-07-14 02:45:59 +08:00
|
|
|
protected function create_mysql($dbkey)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
$host = config($dbkey.'host');
|
|
|
|
$port = config($dbkey.'port');
|
|
|
|
$name = config($dbkey.'database');
|
|
|
|
$user = config($dbkey.'username');
|
|
|
|
$pass = config($dbkey.'password');
|
2018-01-10 10:47:03 +08:00
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
$dbSvc = new Database();
|
2018-01-10 10:47:03 +08:00
|
|
|
$dsn = $dbSvc->createDsn($host, $port);
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::info('Connection string: '.$dsn);
|
2017-07-14 02:45:59 +08:00
|
|
|
|
2018-01-10 10:47:03 +08:00
|
|
|
try {
|
|
|
|
$conn = $dbSvc->createPDO($dsn, $user, $pass);
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
Log::error($e);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-10 10:47:03 +08:00
|
|
|
return false;
|
2017-07-14 02:59:45 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 12:34:16 +08:00
|
|
|
if ($this->option('reset') === true) {
|
2018-01-30 06:02:15 +08:00
|
|
|
$sql = "DROP DATABASE IF EXISTS `$name`";
|
2018-08-27 00:40:04 +08:00
|
|
|
|
2018-01-10 10:47:03 +08:00
|
|
|
try {
|
2018-01-30 06:02:15 +08:00
|
|
|
Log::info('Dropping database: '.$sql);
|
|
|
|
$conn->exec($sql);
|
2018-01-10 10:47:03 +08:00
|
|
|
} catch (\PDOException $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
2017-07-14 02:45:59 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 06:02:15 +08:00
|
|
|
$sql = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET UTF8 COLLATE utf8_unicode_ci";
|
|
|
|
|
2018-01-10 10:47:03 +08:00
|
|
|
try {
|
2018-01-30 06:02:15 +08:00
|
|
|
Log::info('Creating database: '.$sql);
|
|
|
|
$conn->exec($sql);
|
2018-01-10 10:47:03 +08:00
|
|
|
} catch (\PDOException $e) {
|
|
|
|
Log::error($e);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-10 10:47:03 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-14 02:45:59 +08:00
|
|
|
}
|
|
|
|
|
2017-12-06 04:59:31 +08:00
|
|
|
/**
|
|
|
|
* Create the sqlite database
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-06 04:59:31 +08:00
|
|
|
* @param $dbkey
|
|
|
|
*/
|
2017-07-14 02:45:59 +08:00
|
|
|
protected function create_sqlite($dbkey)
|
|
|
|
{
|
2017-12-06 04:59:31 +08:00
|
|
|
$exec = 'sqlite3';
|
|
|
|
if ($this->os->isWindowsLike()) {
|
2017-12-15 12:34:16 +08:00
|
|
|
$exec = 'sqlite3.exe';
|
2017-12-06 04:59:31 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 12:34:16 +08:00
|
|
|
if ($this->option('reset') === true) {
|
2018-03-20 09:50:40 +08:00
|
|
|
$cmd = ['rm', '-rf', config($dbkey.'database')];
|
2017-07-14 02:45:59 +08:00
|
|
|
$this->runCommand($cmd);
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!file_exists(config($dbkey.'database'))) {
|
2017-12-07 07:31:20 +08:00
|
|
|
$cmd = [
|
|
|
|
$exec,
|
2018-03-20 09:50:40 +08:00
|
|
|
config($dbkey.'database'),
|
2017-12-07 07:31:20 +08:00
|
|
|
'""',
|
|
|
|
];
|
2017-07-14 02:45:59 +08:00
|
|
|
|
2017-12-07 07:31:20 +08:00
|
|
|
$this->runCommand($cmd);
|
|
|
|
}
|
2017-07-14 02:45:59 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
protected function create_postgres($dbkey)
|
|
|
|
{
|
|
|
|
$this->error('Not supported yet!');
|
|
|
|
}
|
|
|
|
|
2017-07-14 02:45:59 +08:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2017-07-23 06:28:47 +08:00
|
|
|
/*if ($this->option('reset')) {
|
|
|
|
if(!$this->confirm('The "reset" option will destroy the database, are you sure?')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Using connection "'.config('database.default').'"');
|
2017-07-14 02:45:59 +08:00
|
|
|
|
2017-07-14 04:43:56 +08:00
|
|
|
$conn = config('database.default');
|
2018-03-20 09:50:40 +08:00
|
|
|
$dbkey = 'database.connections.'.$conn.'.';
|
2017-07-14 02:45:59 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (config($dbkey.'driver') === 'mysql') {
|
2017-07-14 02:45:59 +08:00
|
|
|
$this->create_mysql($dbkey);
|
2018-03-20 09:50:40 +08:00
|
|
|
} elseif (config($dbkey.'driver') === 'sqlite') {
|
2017-07-14 02:45:59 +08:00
|
|
|
$this->create_sqlite($dbkey);
|
2018-03-20 09:50:40 +08:00
|
|
|
} // TODO: Eventually
|
|
|
|
elseif (config($dbkey.'driver') === 'postgres') {
|
2017-12-15 06:38:29 +08:00
|
|
|
$this->create_postgres($dbkey);
|
|
|
|
}
|
2017-07-14 02:45:59 +08:00
|
|
|
}
|
|
|
|
}
|