phpvms/app/Console/Commands/CreateDatabase.php

127 lines
3.0 KiB
PHP
Raw Normal View History

2017-07-14 02:45:59 +08:00
<?php
namespace App\Console\Commands;
use Log;
use PDO;
2017-12-30 00:23:42 +08:00
use App\Console\BaseCommand;
2017-12-30 00:23:42 +08:00
class CreateDatabase extends BaseCommand
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
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) {
try {
$conn->exec("DROP DATABASE IF EXISTS `$name`");
} catch (\PDOException $e) {
Log::error($e);
}
2017-07-14 02:45:59 +08:00
}
try {
$conn->exec("CREATE DATABASE IF NOT EXISTS `$name`");
} 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) {
2017-07-14 02:45:59 +08:00
$cmd = ['rm', '-rf', config($dbkey . 'database')];
$this->runCommand($cmd);
}
2017-12-07 07:31:20 +08:00
if (!file_exists(config($dbkey . 'database'))) {
$cmd = [
$exec,
config($dbkey . 'database'),
'""',
];
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;
}
}*/
2017-12-07 07:31:20 +08:00
$this->info('Using connection "' . config('database.default') . '"');
2017-07-14 02:45:59 +08:00
$conn = config('database.default');
2017-12-07 07:31:20 +08:00
$dbkey = 'database.connections.' . $conn . '.';
2017-07-14 02:45:59 +08:00
2017-12-07 07:31:20 +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
}
}