2017-07-14 02:45:59 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Console\Command;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Log;
|
2017-07-14 04:43:56 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class CreateDatabase
|
|
|
|
* @package App\Console\Commands
|
|
|
|
*/
|
|
|
|
class CreateDatabase extends Command
|
2017-07-14 02:45:59 +08:00
|
|
|
{
|
2017-07-14 04:43:56 +08:00
|
|
|
protected $signature = 'database:create {--reset} {--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();
|
2017-12-06 04:59:31 +08:00
|
|
|
$this->os = new \Tivie\OS\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
|
2017-12-06 04:59:31 +08:00
|
|
|
* @param $dbkey
|
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
|
|
|
|
|
|
|
$dbSvc = new \App\Console\Services\Database();
|
2017-12-06 04:59:31 +08:00
|
|
|
|
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-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
|
|
|
|
* @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
|
|
|
}
|
|
|
|
}
|