Skip to content

Commit

Permalink
Update DbUpdate class
Browse files Browse the repository at this point in the history
  • Loading branch information
DariusIII committed Jul 5, 2018
1 parent dcd005f commit 399b221
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
88 changes: 47 additions & 41 deletions Blacklight/db/DbUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
use Blacklight\ColorCLI;
use Blacklight\utility\Git;
use Blacklight\utility\Utility;
use Illuminate\Support\Facades\DB;

class DbUpdate
{
/**
* @var
*/
public $backedup;

/**
* @var \Blacklight\db\DB
* @var \PDO
*/
public $pdo;

Expand All @@ -51,20 +55,13 @@ class DbUpdate
public $settings;

/**
* @var string
*/
protected $_DbSystem;

/**
* @var bool Has the Db been backed up?
* Has the Db been backed up?
*
*
* @var bool
*/
private $backedUp = false;

/**
* @var bool Should we perform a backup?
*/
private $backup;

/**
* DbUpdate constructor.
*
Expand All @@ -80,12 +77,9 @@ public function __construct(array $options = [])
'logger' => new ColorCLI(),
];

$this->backup = $options['backup'];
$this->git = $options['git'];
$this->log = $options['logger'];
// Must be DB not Settings because the Settings table may not exist yet.
$this->pdo = (($options['db'] instanceof DB) ? $options['db'] : new DB());
$this->_DbSystem = strtolower($this->pdo->DbSystem());
$this->pdo = DB::connection()->getPdo();
}

/**
Expand All @@ -104,7 +98,7 @@ public function newPatches(array $options = []): void
{
$defaults = [
'ext' => 'sql',
'path' => NN_RES.'db'.DS.'patches'.DS.$this->_DbSystem,
'path' => NN_RES.'db'.DS.'patches'.DS.'mysql',
'regex' => '#^'.Utility::PATH_REGEX.'\+(?P<order>\d+)~(?P<table>\w+)\.sql$#',
'safe' => true,
];
Expand All @@ -120,7 +114,7 @@ public function newPatches(array $options = []): void
if ($count > 0) {
ColorCLI::doEcho(ColorCLI::header('Processing...'), true);
natsort($files);
$local = $this->pdo->isLocalDb() ? '' : 'LOCAL ';
$local = $this->isLocalDb() ? '' : 'LOCAL ';

foreach ($files as $file) {
if (! preg_match($options['regex'], $file, $matches)) {
Expand Down Expand Up @@ -156,7 +150,7 @@ public function processPatches(array $options = []): int
$patched = 0;
$defaults = [
'ext' => 'sql',
'path' => NN_RES.'db'.DS.'patches'.DS.$this->_DbSystem,
'path' => NN_RES.'db'.DS.'patches'.DS.'mysql',
'regex' => '#^'.Utility::PATH_REGEX.'(?P<patch>\d{4})~(?P<table>\w+)\.sql$#',
'safe' => true,
];
Expand All @@ -171,11 +165,11 @@ public function processPatches(array $options = []): int

if (\count($files)) {
natsort($files);
$local = $this->pdo->isLocalDb() ? '' : 'LOCAL ';
$local = $this->isLocalDb() ? '' : 'LOCAL ';
ColorCLI::doEcho(ColorCLI::primary('Looking for unprocessed patches...'), true);
foreach ($files as $file) {
$setPatch = false;
$fp = fopen($file, 'r');
$fp = fopen($file, 'rb');
$patch = fread($fp, filesize($file));

if (preg_match($options['regex'], str_replace('\\', '/', $file), $matches)) {
Expand All @@ -193,9 +187,6 @@ public function processPatches(array $options = []): int
}
if ($patch > $currentVersion) {
ColorCLI::doEcho(ColorCLI::header('Processing patch file: '.$file), true);
if (! $this->backedUp && $options['safe']) {
$this->_backupDb();
}
$this->splitSQL($file, ['local' => $local]);
if ($setPatch) {
Settings::query()->where('setting', '=', 'sqlpatch')->update(['value' => $patch]);
Expand Down Expand Up @@ -279,8 +270,7 @@ public function splitSQL($file, array $options = []): void
}

try {
$qry = $this->pdo->Prepare($query);
$qry->execute();
$this->pdo->exec($query);
ColorCLI::doEcho(ColorCLI::alternateOver('SUCCESS: ').ColorCLI::primary($query), true);
} catch (\PDOException $e) {
// Log the problem and the query.
Expand Down Expand Up @@ -308,18 +298,16 @@ public function splitSQL($file, array $options = []): void
$e->errorInfo[1]."}.\n"
), true);
}
} else {
if (preg_match('/ALTER IGNORE/i', $query)) {
$this->pdo->queryExec('SET SESSION old_alter_table = 1');
try {
$this->pdo->exec($query);
ColorCLI::doEcho(ColorCLI::alternateOver('SUCCESS: ').ColorCLI::primary($query));
} catch (\PDOException $e) {
exit(ColorCLI::error("$query Failed \{".$e->errorInfo[1]."}\n\t".$e->errorInfo[2]));
}
} else {
} elseif (preg_match('/ALTER IGNORE/i', $query)) {
$this->pdo->exec('SET SESSION old_alter_table = 1');
try {
$this->pdo->exec($query);
ColorCLI::doEcho(ColorCLI::alternateOver('SUCCESS: ').ColorCLI::primary($query));
} catch (\PDOException $e) {
exit(ColorCLI::error("$query Failed \{".$e->errorInfo[1]."}\n\t".$e->errorInfo[2]));
}
} else {
exit(ColorCLI::error("$query Failed \{".$e->errorInfo[1]."}\n\t".$e->errorInfo[2]));
}
}

Expand All @@ -337,12 +325,30 @@ public function splitSQL($file, array $options = []): void
}
}

protected function _backupDb(): void

/**
* Attempts to determine if the Db is on the local machine.
*
* If the method returns true, then the Db is definitely on the local machine. However,
* returning false only indicates that it could not positively be determined to be local - so
* assume remote.
*
* @return bool Whether the Db is definitely on the local machine.
*/
public function isLocalDb(): bool
{
$PHP = 'php';
$local = false;
if (! empty(config('database.connections.nntmux.port')) || config('database.connections.nntmux.host') === 'localhost') {
$local = true;
} else {
preg_match_all('/inet'.'6?'.' addr: ?([^ ]+)/', `ifconfig`, $ips);

// Check for dotted quad - if exists compare against local IP number(s)
if (preg_match('#^\d+\.\d+\.\d+\.\d+$#', config('database.connections.nntmux.host')) && \in_array(config('database.connections.nntmux.host'), $ips[1], false)) {
$local = true;
}
}

system("$PHP ".NN_MISC.'testing'.DS.'DB'.DS.$this->_DbSystem.
'dump_tables.php db dump');
$this->backedup = true;
return $local;
}
}
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
2018-07-05 DariusIII
* Chg: Update DbUpdate class
* Chg: Fix tables count in monitor pane
* Chg: Use laravel DB facade in tmux related classes
2018-07-04 DariusIII
Expand Down

0 comments on commit 399b221

Please sign in to comment.