From 9fe07a19855c6797445ebc8812c1d1342b6a5ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Fri, 24 Apr 2026 12:36:25 +0300 Subject: [PATCH] feat: implement MySqlDriver to handle database connections, schema discovery, and backup operations --- backend/app/Services/Database/MySqlDriver.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/backend/app/Services/Database/MySqlDriver.php b/backend/app/Services/Database/MySqlDriver.php index 059f422..e60664e 100644 --- a/backend/app/Services/Database/MySqlDriver.php +++ b/backend/app/Services/Database/MySqlDriver.php @@ -115,17 +115,24 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface $port = $config['port'] ?? '3306'; $database = $config['database'] ?? ''; - // Build command + // Build command with flags for a complete and resilient export $passwordPart = !empty($password) ? "-p" . escapeshellarg($password) : ""; $dbPart = !empty($database) ? escapeshellarg($database) : "--all-databases"; + // --single-transaction: for InnoDB tables, ensures consistency without locking + // --skip-lock-tables: avoids issues with views/definers that might prevent locking + // --routines, --triggers, --events: ensures all database objects are included + // --quick: useful for large tables + $flags = "--single-transaction --skip-lock-tables --routines --triggers --events --quick"; + $command = sprintf( - '%s -u %s %s -h %s -P %s %s > %s 2> %s', + '%s -u %s %s -h %s -P %s %s %s > %s 2> %s', $mysqldumpPath === 'mysqldump' ? 'mysqldump' : escapeshellarg($mysqldumpPath), escapeshellarg($username), $passwordPart, escapeshellarg($host), escapeshellarg($port), + $flags, $dbPart, escapeshellarg($path), escapeshellarg($errorPath) @@ -136,10 +143,13 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface if (file_exists($errorPath)) { $errors = file_get_contents($errorPath); unlink($errorPath); - if (!empty(trim($errors))) { - if (!file_exists($path) || filesize($path) === 0) { - $instruction = "Please check your .env file and ensure MYSQLDUMP_PATH is correct. Example: MYSQLDUMP_PATH=C:\\xampp\\mysql\\bin\\mysqldump.exe"; - throw new \Exception("Export failed: " . $errors . "\n\nInstructions: " . $instruction); + + // Some errors are fatal even if some output was produced (like the definer issue) + if (!empty(trim($errors)) && (str_contains(strtolower($errors), 'error') || !file_exists($path) || filesize($path) < 1000)) { + // If it's just a warning (like SSL), we might want to continue, + // but if it's a "Got error", we should probably fail. + if (str_contains(strtolower($errors), 'error')) { + throw new \Exception("Export failed: " . $errors); } } }