Rewrite how mysql db is created to avoid external processes #132
This commit is contained in:
parent
07ca62920f
commit
f16748167b
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
50
app/Console/Services/Database.php
Normal file
50
app/Console/Services/Database.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user