feat: implement database management service layer and frontend SQL import/export utility

This commit is contained in:
Ümit Tunç
2026-04-28 21:12:20 +03:00
parent 01ddb81aa9
commit ab5a12f8f2
6 changed files with 48 additions and 24 deletions
@@ -37,7 +37,7 @@ interface DatabaseDriverInterface
/**
* Export the database.
*/
public function export(array $config, array $filters = []): string;
public function export(array $config, array $filters = [], array $options = []): string;
/**
* Import the database.
@@ -112,8 +112,11 @@ class SchemaController extends Controller
$this->initializeDriver($request);
$config = $request->only(['host', 'username', 'password', 'database', 'port', 'table']);
$filters = json_decode($request->get('filters', '[]'), true);
$options = [
'structureOnly' => filter_var($request->get('structureOnly'), FILTER_VALIDATE_BOOLEAN)
];
$filePath = $this->databaseService->export($config, $filters);
$filePath = $this->databaseService->export($config, $filters, $options);
return Response::download($filePath)->deleteFileAfterSend(true);
} catch (\Exception $e) {
@@ -147,7 +147,7 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
return $this->query($sql, [$table, $dbName]);
}
public function export(array $config, array $filters = []): string
public function export(array $config, array $filters = [], array $options = []): string
{
$database = $config['database'] ?? '';
$table = $config['table'] ?? '';
@@ -157,9 +157,11 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
return $this->exportFilteredTable($database, $table, $filters);
}
$isStructureOnly = $options['structureOnly'] ?? false;
$filename = !empty($table)
? "{$table}-" . date('Y-m-d') . ".sql"
: "backup-" . ($database ?: 'all') . "-" . date('Y-m-d') . ".sql";
? "{$table}-" . ($isStructureOnly ? 'schema-' : '') . date('Y-m-d') . ".sql"
: "backup-" . ($database ?: 'all') . "-" . ($isStructureOnly ? 'schema-' : '') . date('Y-m-d') . ".sql";
$directory = storage_path('app/backups');
if (!is_dir($directory)) {
@@ -191,6 +193,10 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
// --quick: useful for large tables
$flags = "--single-transaction --skip-lock-tables --routines --triggers --events --quick";
if ($isStructureOnly) {
$flags .= " --no-data";
}
$command = sprintf(
'%s -u %s %s -h %s -P %s %s %s > %s 2> %s',
$mysqldumpPath === 'mysqldump' ? 'mysqldump' : escapeshellarg($mysqldumpPath),
+2 -2
View File
@@ -102,9 +102,9 @@ class DatabaseService
/**
* Export the database.
*/
public function export(array $config, array $filters = []): string
public function export(array $config, array $filters = [], array $options = []): string
{
return $this->getDriver()->export($config, $filters);
return $this->getDriver()->export($config, $filters, $options);
}
/**