feat: implement MySqlDriver to handle database connections, schema discovery, and backup operations
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user