feat: implement MySQL driver with shell-based import/export and add database transfer UI component

This commit is contained in:
Ümit Tunç
2026-04-24 12:29:20 +03:00
parent 60cd2fe051
commit f38f67189e
3 changed files with 24 additions and 9 deletions
+14 -8
View File
@@ -106,6 +106,8 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
$path = $directory . DIRECTORY_SEPARATOR . $filename;
$errorPath = $directory . DIRECTORY_SEPARATOR . $filename . '.err';
$mysqldumpPath = env('MYSQLDUMP_PATH', 'mysqldump');
// Ensure we have a username
$username = $config['username'] ?? 'root';
$password = $config['password'] ?? '';
@@ -117,10 +119,9 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
$passwordPart = !empty($password) ? "-p" . escapeshellarg($password) : "";
$dbPart = !empty($database) ? escapeshellarg($database) : "--all-databases";
// On Windows, we might need to handle double quotes in escapeshellarg
// Let's use a more robust way to execute and capture errors
$command = sprintf(
'mysqldump -u %s %s -h %s -P %s %s > %s 2> %s',
'%s -u %s %s -h %s -P %s %s > %s 2> %s',
$mysqldumpPath === 'mysqldump' ? 'mysqldump' : escapeshellarg($mysqldumpPath),
escapeshellarg($username),
$passwordPart,
escapeshellarg($host),
@@ -136,15 +137,16 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
$errors = file_get_contents($errorPath);
unlink($errorPath);
if (!empty(trim($errors))) {
// If the file is 0 bytes and there are errors, it definitely failed
if (!file_exists($path) || filesize($path) === 0) {
throw new \Exception("Export failed: " . $errors);
$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);
}
}
}
if (!file_exists($path) || filesize($path) === 0) {
throw new \Exception("Export failed: Resulting file is empty. Ensure mysqldump is installed and in the system PATH.");
$instruction = "The 'mysqldump' binary was not found. Please set MYSQLDUMP_PATH in your .env file to the absolute path of the mysqldump executable.";
throw new \Exception("Export failed: Binary not found.\n\nInstructions: " . $instruction);
}
return $path;
@@ -157,6 +159,8 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
mkdir($directory, 0755, true);
}
$errorPath = $directory . DIRECTORY_SEPARATOR . 'import_error.err';
$mysqlPath = env('MYSQL_BINARY_PATH', 'mysql');
$username = $config['username'] ?? 'root';
$password = $config['password'] ?? '';
@@ -168,7 +172,8 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
$dbPart = !empty($database) ? escapeshellarg($database) : "";
$command = sprintf(
'mysql -u %s %s -h %s -P %s %s < %s 2> %s',
'%s -u %s %s -h %s -P %s %s < %s 2> %s',
$mysqlPath === 'mysql' ? 'mysql' : escapeshellarg($mysqlPath),
escapeshellarg($username),
$passwordPart,
escapeshellarg($host),
@@ -184,7 +189,8 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
$errors = file_get_contents($errorPath);
unlink($errorPath);
if (!empty(trim($errors))) {
throw new \Exception("Import failed: " . $errors);
$instruction = "Please check your .env file and ensure MYSQL_BINARY_PATH is correct. Example: MYSQL_BINARY_PATH=C:\\xampp\\mysql\\bin\\mysql.exe";
throw new \Exception("Import failed: " . $errors . "\n\nInstructions: " . $instruction);
}
}