phpvms/app/Console/Commands/CreateDatabase.php

124 lines
2.9 KiB
PHP
Raw Normal View History

2017-07-14 02:45:59 +08:00
<?php
namespace App\Console\Commands;
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
* @param $dbkey
*/
2017-07-14 02:45:59 +08:00
protected function create_mysql($dbkey)
{
2017-12-06 04:59:31 +08:00
$exec = 'mysql';
2017-12-07 07:31:20 +08:00
if ($this->os->isWindowsLike()) {
2017-12-06 04:59:31 +08:00
$exec .= '.exe';
}
2017-07-14 02:45:59 +08:00
$mysql_cmd = [
2017-12-06 04:59:31 +08:00
$exec,
2017-07-14 02:45:59 +08:00
'-u' . config($dbkey . 'username'),
'-h' . config($dbkey . 'host'),
'-P' . config($dbkey . 'port'),
];
2017-07-14 02:59:45 +08:00
# only supply password if it's set
$password = config($dbkey . 'password');
2017-12-07 07:31:20 +08:00
if ($password !== '') {
2017-07-14 02:59:45 +08:00
$mysql_cmd[] = '-p' . $password;
}
2017-12-15 12:34:16 +08:00
if ($this->option('reset') === true) {
2017-07-14 02:45:59 +08:00
$cmd = array_merge(
$mysql_cmd,
2017-12-18 07:01:05 +08:00
["-e 'DROP DATABASE IF EXISTS " . config($dbkey . 'database') . "'"]
2017-07-14 02:45:59 +08:00
);
$this->runCommand($cmd);
}
$cmd = array_merge(
$mysql_cmd,
["-e 'CREATE DATABASE IF NOT EXISTS " . config($dbkey . 'database') . "'"]
);
$this->runCommand($cmd);
}
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
}
}