diff --git a/app/Console/Commands/CreateDatabase.php b/app/Console/Commands/CreateDatabase.php index 7c051f1a..9baa0091 100644 --- a/app/Console/Commands/CreateDatabase.php +++ b/app/Console/Commands/CreateDatabase.php @@ -2,6 +2,9 @@ namespace App\Console\Commands; +use Log; +use PDO; + use App\Console\BaseCommand; class CreateDatabase extends BaseCommand @@ -17,44 +20,44 @@ class CreateDatabase extends BaseCommand } /** - * create the mysql database + * Create the mysql database * @param $dbkey + * @return bool */ protected function create_mysql($dbkey) { - $exec = 'mysql'; - if ($this->os->isWindowsLike()) { - $exec .= '.exe'; - } + $host = config($dbkey . 'host'); + $port = config($dbkey . 'port'); + $name = config($dbkey . 'database'); + $user = config($dbkey . 'username'); + $pass = config($dbkey . 'password'); - $mysql_cmd = [ - $exec, - '-u' . config($dbkey . 'username'), - '-h' . config($dbkey . 'host'), - '-P' . config($dbkey . 'port'), - ]; + $dbSvc = new \App\Console\Services\Database(); - # only supply password if it's set - $password = config($dbkey . 'password'); - if ($password !== '') { - $mysql_cmd[] = '-p' . $password; + $dsn = $dbSvc->createDsn($host, $port); + Log::info('Connection string: ' . $dsn); + + try { + $conn = $dbSvc->createPDO($dsn, $user, $pass); + } catch (\PDOException $e) { + Log::error($e); + return false; } if ($this->option('reset') === true) { - $cmd = array_merge( - $mysql_cmd, - ["-e 'DROP DATABASE IF EXISTS " . config($dbkey . 'database') . "'"] - ); - - $this->runCommand($cmd); + try { + $conn->exec("DROP DATABASE IF EXISTS `$name`"); + } catch (\PDOException $e) { + Log::error($e); + } } - $cmd = array_merge( - $mysql_cmd, - ["-e 'CREATE DATABASE IF NOT EXISTS " . config($dbkey . 'database') . "'"] - ); - - $this->runCommand($cmd); + try { + $conn->exec("CREATE DATABASE IF NOT EXISTS `$name`"); + } catch (\PDOException $e) { + Log::error($e); + return false; + } } /** diff --git a/app/Console/Services/Database.php b/app/Console/Services/Database.php new file mode 100644 index 00000000..c13844f4 --- /dev/null +++ b/app/Console/Services/Database.php @@ -0,0 +1,50 @@ +setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); + } catch (\PDOException $e) { + throw $e; + } + + return $conn; + } +} diff --git a/modules/Installer/Services/DatabaseService.php b/modules/Installer/Services/DatabaseService.php index 4e987e0b..db575cf5 100644 --- a/modules/Installer/Services/DatabaseService.php +++ b/modules/Installer/Services/DatabaseService.php @@ -17,7 +17,7 @@ class DatabaseService { Log::info('Testing Connection: '.$driver.'::'.$user.':'.$pass.'@'.$host.':'.$port.';'.$name); if($driver === 'mysql') { - $dsn = "mysql:host=$host;port=$port;"; + $dsn = "mysql:host=$host;port=$port;dbname=$name"; Log::info('Connection string: '. $dsn); try { $conn = new PDO($dsn, $user, $pass); @@ -26,7 +26,7 @@ class DatabaseService { } } - // Needs testing + // TODO: Needs testing elseif ($driver === 'postgres') { $dsn = "pgsql:host=$host;port=$port;dbname=$name"; try { @@ -46,8 +46,10 @@ class DatabaseService { { $output = ''; - \Artisan::call('database:create'); - $output .= \Artisan::output(); + if(config('database.default') === 'sqlite') { + \Artisan::call('database:create'); + $output .= \Artisan::output(); + } \Artisan::call('migrate'); $output .= trim(\Artisan::output());