Rewrite how mysql db is created to avoid external processes #132

This commit is contained in:
Nabeel Shahzad 2018-01-09 20:47:03 -06:00
parent 07ca62920f
commit f16748167b
3 changed files with 86 additions and 31 deletions

View File

@ -2,6 +2,9 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use Log;
use PDO;
use App\Console\BaseCommand; use App\Console\BaseCommand;
class CreateDatabase extends BaseCommand class CreateDatabase extends BaseCommand
@ -17,44 +20,44 @@ class CreateDatabase extends BaseCommand
} }
/** /**
* create the mysql database * Create the mysql database
* @param $dbkey * @param $dbkey
* @return bool
*/ */
protected function create_mysql($dbkey) protected function create_mysql($dbkey)
{ {
$exec = 'mysql'; $host = config($dbkey . 'host');
if ($this->os->isWindowsLike()) { $port = config($dbkey . 'port');
$exec .= '.exe'; $name = config($dbkey . 'database');
} $user = config($dbkey . 'username');
$pass = config($dbkey . 'password');
$mysql_cmd = [ $dbSvc = new \App\Console\Services\Database();
$exec,
'-u' . config($dbkey . 'username'),
'-h' . config($dbkey . 'host'),
'-P' . config($dbkey . 'port'),
];
# only supply password if it's set $dsn = $dbSvc->createDsn($host, $port);
$password = config($dbkey . 'password'); Log::info('Connection string: ' . $dsn);
if ($password !== '') {
$mysql_cmd[] = '-p' . $password; try {
$conn = $dbSvc->createPDO($dsn, $user, $pass);
} catch (\PDOException $e) {
Log::error($e);
return false;
} }
if ($this->option('reset') === true) { if ($this->option('reset') === true) {
$cmd = array_merge( try {
$mysql_cmd, $conn->exec("DROP DATABASE IF EXISTS `$name`");
["-e 'DROP DATABASE IF EXISTS " . config($dbkey . 'database') . "'"] } catch (\PDOException $e) {
); Log::error($e);
}
$this->runCommand($cmd);
} }
$cmd = array_merge( try {
$mysql_cmd, $conn->exec("CREATE DATABASE IF NOT EXISTS `$name`");
["-e 'CREATE DATABASE IF NOT EXISTS " . config($dbkey . 'database') . "'"] } catch (\PDOException $e) {
); Log::error($e);
return false;
$this->runCommand($cmd); }
} }
/** /**

View File

@ -0,0 +1,50 @@
<?php
namespace App\Console\Services;
use PDO;
use Doctrine\DBAL\Driver\PDOException;
/**
* Class Database
* @package App\Console\Services
*/
class Database
{
/**
* Create the base connection DSN, optionally include the DB name
* @param $host
* @param $port
* @param null $name
* @return string
*/
public function createDsn($host, $port, $name=null)
{
$conn = config('database.default');
$dsn = "$conn:host=$host;port=$port";
if(filled($name)) {
$dsn .= ';dbname='.$name;
}
return $dsn;
}
/**
* @param $dsn
* @param $user
* @param $pass
* @return PDO
* @throws \PDOException
*/
public function createPDO($dsn, $user, $pass)
{
try {
$conn = new PDO($dsn, $user, $pass);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (\PDOException $e) {
throw $e;
}
return $conn;
}
}

View File

@ -17,7 +17,7 @@ class DatabaseService {
Log::info('Testing Connection: '.$driver.'::'.$user.':'.$pass.'@'.$host.':'.$port.';'.$name); Log::info('Testing Connection: '.$driver.'::'.$user.':'.$pass.'@'.$host.':'.$port.';'.$name);
if($driver === 'mysql') { if($driver === 'mysql') {
$dsn = "mysql:host=$host;port=$port;"; $dsn = "mysql:host=$host;port=$port;dbname=$name";
Log::info('Connection string: '. $dsn); Log::info('Connection string: '. $dsn);
try { try {
$conn = new PDO($dsn, $user, $pass); $conn = new PDO($dsn, $user, $pass);
@ -26,7 +26,7 @@ class DatabaseService {
} }
} }
// Needs testing // TODO: Needs testing
elseif ($driver === 'postgres') { elseif ($driver === 'postgres') {
$dsn = "pgsql:host=$host;port=$port;dbname=$name"; $dsn = "pgsql:host=$host;port=$port;dbname=$name";
try { try {
@ -46,8 +46,10 @@ class DatabaseService {
{ {
$output = ''; $output = '';
\Artisan::call('database:create'); if(config('database.default') === 'sqlite') {
$output .= \Artisan::output(); \Artisan::call('database:create');
$output .= \Artisan::output();
}
\Artisan::call('migrate'); \Artisan::call('migrate');
$output .= trim(\Artisan::output()); $output .= trim(\Artisan::output());