feat: implement database schema discovery and management services with frontend integration

This commit is contained in:
Ümit Tunç
2026-04-24 13:46:38 +03:00
parent 53b40e95e5
commit a447d5a08e
8 changed files with 372 additions and 11 deletions
@@ -48,4 +48,34 @@ interface DatabaseDriverInterface
* Get database metadata (charset, collation, size, etc.)
*/
public function getDatabaseMetadata(string $database): array;
/**
* Get table metadata.
*/
public function getTableMetadata(string $database, string $table): array;
/**
* Get metadata for all tables in a database.
*/
public function getTablesMetadata(string $database): array;
/**
* Truncate a table.
*/
public function truncateTable(string $table): bool;
/**
* Drop a table.
*/
public function dropTable(string $table): bool;
/**
* Optimize a table.
*/
public function optimizeTable(string $table): bool;
/**
* Perform batch update on a table.
*/
public function batchUpdate(string $table, array $changes): bool;
}
@@ -248,4 +248,19 @@ class SchemaController extends Controller
return Response::json(['error' => $e->getMessage()], 400);
}
}
public function batchUpdate(Request $request, $table)
{
$request->validate([
'changes' => 'required|array',
]);
try {
$this->initializeDriver($request);
$this->databaseService->batchUpdate($table, $request->changes);
return Response::json(['message' => 'Batch update successful']);
} catch (\Exception $e) {
return Response::json(['error' => $e->getMessage()], 400);
}
}
}
@@ -302,4 +302,41 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
return $this->query($sql, [$database]);
}
public function batchUpdate(string $table, array $changes): bool
{
$connection = $this->getConnection();
// Find primary key
$schema = $this->getTableSchema($table);
$primaryKey = 'id'; // default
foreach ($schema as $col) {
if (($col->Key ?? '') === 'PRI') {
$primaryKey = $col->Field;
break;
}
}
$connection->beginTransaction();
try {
foreach ($changes as $change) {
if (!isset($change[$primaryKey])) {
continue;
}
$id = $change[$primaryKey];
$updateData = $change;
unset($updateData[$primaryKey]);
if (empty($updateData)) continue;
$connection->table($table)->where($primaryKey, $id)->update($updateData);
}
$connection->commit();
return true;
} catch (\Exception $e) {
$connection->rollBack();
throw $e;
}
}
}
+8
View File
@@ -156,4 +156,12 @@ class DatabaseService
{
return $this->getDriver()->getTablesMetadata($database);
}
/**
* Perform batch update on a table.
*/
public function batchUpdate(string $table, array $changes): bool
{
return $this->getDriver()->batchUpdate($table, $changes);
}
}