first commit
This commit is contained in:
112
app/Http/Controllers/SourceDatabaseController.php
Normal file
112
app/Http/Controllers/SourceDatabaseController.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\SourceDatabase;
|
||||
use App\Services\DatabaseConnectionService;
|
||||
use App\Services\SchemaExtractionService;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class SourceDatabaseController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private DatabaseConnectionService $connectionService,
|
||||
private SchemaExtractionService $extractionService
|
||||
) {}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$databases = SourceDatabase::withCount('tables')->get();
|
||||
|
||||
return Inertia::render('Databases/SourceDatabases', [
|
||||
'databases' => $databases,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return Inertia::render('Databases/SourceDatabaseForm', [
|
||||
'database' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'host' => 'required|string|max:255',
|
||||
'port' => 'required|integer|between:1,65535',
|
||||
'database' => 'required|string|max:255',
|
||||
'username' => 'required|string|max:255',
|
||||
'password' => 'required|string',
|
||||
'driver' => 'nullable|string|max:50',
|
||||
'description' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$validated['driver'] = $validated['driver'] ?? 'pgsql';
|
||||
|
||||
$database = SourceDatabase::create($validated);
|
||||
|
||||
return redirect()->route('databases.source.index')
|
||||
->with('success', 'Source database created successfully.');
|
||||
}
|
||||
|
||||
public function edit(SourceDatabase $database)
|
||||
{
|
||||
return Inertia::render('Databases/SourceDatabaseForm', [
|
||||
'database' => $database,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, SourceDatabase $database)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'host' => 'required|string|max:255',
|
||||
'port' => 'required|integer|between:1,65535',
|
||||
'database' => 'required|string|max:255',
|
||||
'username' => 'required|string|max:255',
|
||||
'password' => 'nullable|string',
|
||||
'driver' => 'nullable|string|max:50',
|
||||
'description' => 'nullable|string',
|
||||
]);
|
||||
|
||||
if (empty($validated['password'])) {
|
||||
unset($validated['password']);
|
||||
}
|
||||
|
||||
$database->update($validated);
|
||||
|
||||
return redirect()->route('databases.source.index')
|
||||
->with('success', 'Source database updated successfully.');
|
||||
}
|
||||
|
||||
public function destroy(SourceDatabase $database)
|
||||
{
|
||||
$database->delete();
|
||||
|
||||
return redirect()->route('databases.source.index')
|
||||
->with('success', 'Source database deleted successfully.');
|
||||
}
|
||||
|
||||
public function testConnection(SourceDatabase $database)
|
||||
{
|
||||
$result = $this->connectionService->testSourceConnection($database);
|
||||
|
||||
return response()->json($result);
|
||||
}
|
||||
|
||||
public function sync(SourceDatabase $database)
|
||||
{
|
||||
try {
|
||||
$this->extractionService->saveSchema($database);
|
||||
|
||||
return redirect()->back()
|
||||
->with('success', 'Schema synchronized successfully.');
|
||||
} catch (\Exception $e) {
|
||||
return redirect()->back()
|
||||
->with('error', 'Failed to sync schema: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user