diff --git a/install/mysqlRestore.php b/install/mysqlRestore.php index 13e93ff30a14..8863f9328f7e 100644 --- a/install/mysqlRestore.php +++ b/install/mysqlRestore.php @@ -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'); @@ -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 @@ -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 \'' . $templine . '\': ' . $global['mysqli']->error . '

'); + 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; + } }