Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Jul 28, 2024
1 parent 13fe6c9 commit 8976552
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions install/mysqlRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//streamer config
$global['createDatabase'] = 1;
$doNotIncludeConfig = 1;
require_once __DIR__.'/../videos/configuration.php';
require_once __DIR__ . '/../videos/configuration.php';

if (php_sapi_name() !== 'cli') {
return die('Command Line only');
Expand All @@ -15,7 +15,7 @@
echo "Searching [{$globPattern}]" . PHP_EOL;
$glob = glob($globPattern);
foreach ($glob as $key => $file) {
echo "($key) {$file} ".humanFileSize(filesize($file)) . PHP_EOL;
echo "($key) {$file} " . humanFileSize(filesize($file)) . PHP_EOL;
}

// Check for command line argument
Expand Down Expand Up @@ -61,26 +61,63 @@ function executeFile($filename) {
$templine = '';
// Read in entire file
$lines = file($filename);
$lockedTables = [];

// Função para bloquear tabelas
function lockTables($tables) {
global $global;
$lockQuery = 'LOCK TABLES ' . implode(' WRITE, ', $tables) . ' WRITE;';
if (!$global['mysqli']->query($lockQuery)) {
throw new Exception('Error locking tables: ' . $global['mysqli']->error);
}
}

// Loop through each line
foreach ($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
if (substr($line, 0, 2) == '--' || trim($line) == '')
continue;

// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';') {
if (substr(trim($line), -1) == ';') {
// Perform the query
try {
if (!$global['mysqli']->query($templine)) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $templine . '\': ' . $global['mysqli']->error . '<br /><br />');
throw new Exception($global['mysqli']->error);
}
} catch (\Exception $th) {
echo $th->getMessage().PHP_EOL;
$error = $th->getMessage();
if (preg_match("/Table '(.*?)' was not locked with LOCK TABLES/", $error, $matches)) {
$tableName = $matches[1];
if (!in_array($tableName, $lockedTables)) {
$lockedTables[] = $tableName;
try {
lockTables($lockedTables);
// Retry the query after locking the tables
if (!$global['mysqli']->query($templine)) {
throw new Exception('Error performing query after locking tables: ' . $global['mysqli']->error);
}
} catch (\Exception $lockException) {
echo 'ERROR: Failed to lock tables: ' . $lockException->getMessage() . PHP_EOL;
}
} else {
echo 'ERROR: Table was not locked and could not be locked: ' . $error . PHP_EOL;
}
} else {
echo 'ERROR: ' . $error . PHP_EOL;
}
}
// Reset temp variable to empty
$templine = '';
}
}

// Unlock all tables at the end
try {
$global['mysqli']->query('UNLOCK TABLES;');
} catch (\Exception $th) {
echo 'ERROR: Failed to unlock tables: ' . $th->getMessage() . PHP_EOL;
}
}

0 comments on commit 8976552

Please sign in to comment.