phpvms/app/Console/Commands/CreateDatabase.php

135 lines
3.3 KiB
PHP
Raw Normal View History

2017-07-14 02:45:59 +08:00
<?php
namespace App\Console\Commands;
use App\Console\Command;
2018-02-21 12:33:09 +08:00
use Log;
/**
* Class CreateDatabase
* @package App\Console\Commands
*/
class CreateDatabase extends Command
2017-07-14 02:45:59 +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
/**
* 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
/**
* Create the mysql database
2017-12-06 04:59:31 +08:00
* @param $dbkey
* @return bool
2017-12-06 04:59:31 +08:00
*/
2017-07-14 02:45:59 +08:00
protected function create_mysql($dbkey)
{
$host = config($dbkey.'host');
$port = config($dbkey.'port');
$name = config($dbkey.'database');
$user = config($dbkey.'username');
$pass = config($dbkey.'password');
$dbSvc = new \App\Console\Services\Database();
2017-12-06 04:59:31 +08:00
$dsn = $dbSvc->createDsn($host, $port);
Log::info('Connection string: '.$dsn);
2017-07-14 02:45:59 +08:00
try {
$conn = $dbSvc->createPDO($dsn, $user, $pass);
} catch (\PDOException $e) {
Log::error($e);
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`";
try {
2018-01-30 06:02:15 +08:00
Log::info('Dropping database: '.$sql);
$conn->exec($sql);
} 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";
try {
2018-01-30 06:02:15 +08:00
Log::info('Creating database: '.$sql);
$conn->exec($sql);
} catch (\PDOException $e) {
Log::error($e);
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) {
$cmd = ['rm', '-rf', config($dbkey.'database')];
2017-07-14 02:45:59 +08:00
$this->runCommand($cmd);
}
if (!file_exists(config($dbkey.'database'))) {
2017-12-07 07:31:20 +08:00
$cmd = [
$exec,
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
}
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;
}
}*/
$this->info('Using connection "'.config('database.default').'"');
2017-07-14 02:45:59 +08:00
$conn = config('database.default');
$dbkey = 'database.connections.'.$conn.'.';
2017-07-14 02:45:59 +08:00
if (config($dbkey.'driver') === 'mysql') {
2017-07-14 02:45:59 +08:00
$this->create_mysql($dbkey);
} elseif (config($dbkey.'driver') === 'sqlite') {
2017-07-14 02:45:59 +08:00
$this->create_sqlite($dbkey);
} // TODO: Eventually
elseif (config($dbkey.'driver') === 'postgres') {
$this->create_postgres($dbkey);
}
2017-07-14 02:45:59 +08:00
}
}