feat: implement database management API with MySQL driver and schema operations
This commit is contained in:
@@ -83,4 +83,14 @@ interface DatabaseDriverInterface
|
||||
* Create a new database.
|
||||
*/
|
||||
public function createDatabase(string $name, string $charset = 'utf8mb4', string $collation = 'utf8mb4_unicode_ci'): bool;
|
||||
|
||||
/**
|
||||
* Drop a database.
|
||||
*/
|
||||
public function dropDatabase(string $name): bool;
|
||||
|
||||
/**
|
||||
* Rename a database.
|
||||
*/
|
||||
public function renameDatabase(string $oldName, string $newName): bool;
|
||||
}
|
||||
|
||||
@@ -287,4 +287,31 @@ class SchemaController extends Controller
|
||||
return Response::json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function dropDatabase(Request $request, $database)
|
||||
{
|
||||
try {
|
||||
$this->initializeDriver($request);
|
||||
$this->databaseService->dropDatabase($database);
|
||||
return Response::json(['message' => "Database '{$database}' dropped successfully"]);
|
||||
} catch (\Exception $e) {
|
||||
return Response::json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function renameDatabase(Request $request, $database)
|
||||
{
|
||||
$request->validate([
|
||||
'newName' => 'required|string|max:64',
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->initializeDriver($request);
|
||||
$newName = $request->get('newName');
|
||||
$this->databaseService->renameDatabase($database, $newName);
|
||||
return Response::json(['message' => "Database '{$database}' renamed to '{$newName}' successfully"]);
|
||||
} catch (\Exception $e) {
|
||||
return Response::json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -454,4 +454,39 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
|
||||
DB::connection($this->connectionName)->statement("CREATE DATABASE `{$name}` CHARACTER SET {$charset} COLLATE {$collation}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function dropDatabase(string $name): bool
|
||||
{
|
||||
DB::connection($this->connectionName)->statement("DROP DATABASE `{$name}`");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function renameDatabase(string $oldName, string $newName): bool
|
||||
{
|
||||
// 1. Create new database
|
||||
$this->createDatabase($newName);
|
||||
|
||||
// 2. Get tables from old database
|
||||
$tables = $this->query("SHOW TABLES FROM `{$oldName}`");
|
||||
$key = "Tables_in_{$oldName}";
|
||||
|
||||
if (!empty($tables)) {
|
||||
$renameQueries = [];
|
||||
foreach ($tables as $table) {
|
||||
$tableName = $table->$key;
|
||||
$renameQueries[] = "`{$oldName}`.`{$tableName}` TO `{$newName}`.`{$tableName}`";
|
||||
}
|
||||
|
||||
// 3. Rename all tables
|
||||
if (!empty($renameQueries)) {
|
||||
$sql = "RENAME TABLE " . implode(', ', $renameQueries);
|
||||
DB::connection($this->connectionName)->statement($sql);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Drop old database
|
||||
$this->dropDatabase($oldName);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,4 +172,20 @@ class DatabaseService
|
||||
{
|
||||
return $this->getDriver()->createDatabase($name, $charset, $collation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop a database.
|
||||
*/
|
||||
public function dropDatabase(string $name): bool
|
||||
{
|
||||
return $this->getDriver()->dropDatabase($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename a database.
|
||||
*/
|
||||
public function renameDatabase(string $oldName, string $newName): bool
|
||||
{
|
||||
return $this->getDriver()->renameDatabase($oldName, $newName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user