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';
|
$port = $config['port'] ?? '3306';
|
||||||
$database = $config['database'] ?? '';
|
$database = $config['database'] ?? '';
|
||||||
|
|
||||||
// Build command
|
// Build command with flags for a complete and resilient export
|
||||||
$passwordPart = !empty($password) ? "-p" . escapeshellarg($password) : "";
|
$passwordPart = !empty($password) ? "-p" . escapeshellarg($password) : "";
|
||||||
$dbPart = !empty($database) ? escapeshellarg($database) : "--all-databases";
|
$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(
|
$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),
|
$mysqldumpPath === 'mysqldump' ? 'mysqldump' : escapeshellarg($mysqldumpPath),
|
||||||
escapeshellarg($username),
|
escapeshellarg($username),
|
||||||
$passwordPart,
|
$passwordPart,
|
||||||
escapeshellarg($host),
|
escapeshellarg($host),
|
||||||
escapeshellarg($port),
|
escapeshellarg($port),
|
||||||
|
$flags,
|
||||||
$dbPart,
|
$dbPart,
|
||||||
escapeshellarg($path),
|
escapeshellarg($path),
|
||||||
escapeshellarg($errorPath)
|
escapeshellarg($errorPath)
|
||||||
@@ -136,10 +143,13 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
|
|||||||
if (file_exists($errorPath)) {
|
if (file_exists($errorPath)) {
|
||||||
$errors = file_get_contents($errorPath);
|
$errors = file_get_contents($errorPath);
|
||||||
unlink($errorPath);
|
unlink($errorPath);
|
||||||
if (!empty(trim($errors))) {
|
|
||||||
if (!file_exists($path) || filesize($path) === 0) {
|
// Some errors are fatal even if some output was produced (like the definer issue)
|
||||||
$instruction = "Please check your .env file and ensure MYSQLDUMP_PATH is correct. Example: MYSQLDUMP_PATH=C:\\xampp\\mysql\\bin\\mysqldump.exe";
|
if (!empty(trim($errors)) && (str_contains(strtolower($errors), 'error') || !file_exists($path) || filesize($path) < 1000)) {
|
||||||
throw new \Exception("Export failed: " . $errors . "\n\nInstructions: " . $instruction);
|
// 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