feat: implement dynamic database management via MySQL driver and API controllers

This commit is contained in:
Ümit Tunç
2026-04-24 12:52:01 +03:00
parent 9fe07a1985
commit 23f8eeb560
8 changed files with 164 additions and 33 deletions
+38 -1
View File
@@ -114,10 +114,16 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
$host = $config['host'] ?? '127.0.0.1';
$port = $config['port'] ?? '3306';
$database = $config['database'] ?? '';
$table = $config['table'] ?? '';
// Build command with flags for a complete and resilient export
$passwordPart = !empty($password) ? "-p" . escapeshellarg($password) : "";
$dbPart = !empty($database) ? escapeshellarg($database) : "--all-databases";
if (!empty($table) && !empty($database)) {
$dbPart = escapeshellarg($database) . " " . escapeshellarg($table);
} else {
$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
@@ -225,4 +231,35 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
$results = $this->query($sql, [$database]);
return (array) ($results[0] ?? []);
}
public function getTableMetadata(string $database, string $table): array
{
$sql = "
SELECT
ENGINE as engine,
TABLE_ROWS as rows,
DATA_LENGTH as data_length,
INDEX_LENGTH as index_length,
DATA_FREE as data_free,
AUTO_INCREMENT as auto_increment,
CREATE_TIME as create_time,
UPDATE_TIME as update_time,
TABLE_COLLATION as collation,
TABLE_COMMENT as comment
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = ? AND
TABLE_NAME = ?
";
$results = $this->query($sql, [$database, $table]);
return (array) ($results[0] ?? []);
}
public function truncateTable(string $table): bool
{
DB::connection($this->connectionName)->statement("TRUNCATE TABLE `{$table}`");
return true;
}
}
+16
View File
@@ -122,4 +122,20 @@ class DatabaseService
{
return $this->getDriver()->getDatabaseMetadata($database);
}
/**
* Get table metadata.
*/
public function getTableMetadata(string $database, string $table): array
{
return $this->getDriver()->getTableMetadata($database, $table);
}
/**
* Truncate a table.
*/
public function truncateTable(string $table): bool
{
return $this->getDriver()->truncateTable($table);
}
}