mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 12:14:45 +02:00
Add connection test to database hosts (#410)
* add connection test to database hosts * fix phpstan
This commit is contained in:
parent
f459987458
commit
fc92a87993
@ -3,10 +3,14 @@
|
||||
namespace App\Filament\Resources\DatabaseHostResource\Pages;
|
||||
|
||||
use App\Filament\Resources\DatabaseHostResource;
|
||||
use App\Services\Databases\Hosts\HostCreationService;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Notifications\Notification;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use PDOException;
|
||||
|
||||
class CreateDatabaseHost extends CreateRecord
|
||||
{
|
||||
@ -79,11 +83,30 @@ class CreateDatabaseHost extends CreateRecord
|
||||
return [
|
||||
$this->getCreateFormAction()->formId('form'),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
protected function getFormActions(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
return resolve(HostCreationService::class)->handle($data);
|
||||
}
|
||||
|
||||
public function exception($e, $stopPropagation): void
|
||||
{
|
||||
if ($e instanceof PDOException) {
|
||||
Notification::make()
|
||||
->title('Error connecting to database host')
|
||||
->body($e->getMessage())
|
||||
->color('danger')
|
||||
->icon('tabler-database')
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
$stopPropagation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,11 +4,15 @@ namespace App\Filament\Resources\DatabaseHostResource\Pages;
|
||||
|
||||
use App\Filament\Resources\DatabaseHostResource;
|
||||
use App\Models\DatabaseHost;
|
||||
use App\Services\Databases\Hosts\HostUpdateService;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Notifications\Notification;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use PDOException;
|
||||
|
||||
class EditDatabaseHost extends EditRecord
|
||||
{
|
||||
@ -90,4 +94,24 @@ class EditDatabaseHost extends EditRecord
|
||||
DatabaseHostResource\RelationManagers\DatabasesRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function handleRecordUpdate($record, array $data): Model
|
||||
{
|
||||
return resolve(HostUpdateService::class)->handle($record->id, $data);
|
||||
}
|
||||
|
||||
public function exception($e, $stopPropagation): void
|
||||
{
|
||||
if ($e instanceof PDOException) {
|
||||
Notification::make()
|
||||
->title('Error connecting to database host')
|
||||
->body($e->getMessage())
|
||||
->color('danger')
|
||||
->icon('tabler-database')
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
$stopPropagation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,13 +33,13 @@ class HostCreationService
|
||||
'host' => array_get($data, 'host'),
|
||||
'port' => array_get($data, 'port'),
|
||||
'username' => array_get($data, 'username'),
|
||||
'max_databases' => null,
|
||||
'max_databases' => array_get($data, 'max_databases'),
|
||||
'node_id' => array_get($data, 'node_id'),
|
||||
]);
|
||||
|
||||
// Confirm access using the provided credentials before saving data.
|
||||
$this->dynamic->set('dynamic', $host);
|
||||
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');
|
||||
$this->databaseManager->connection('dynamic')->getPdo();
|
||||
|
||||
return $host;
|
||||
});
|
||||
|
@ -24,18 +24,21 @@ class HostUpdateService
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function handle(int $hostId, array $data): DatabaseHost
|
||||
public function handle(DatabaseHost|int $host, array $data): DatabaseHost
|
||||
{
|
||||
if (!$host instanceof DatabaseHost) {
|
||||
$host = DatabaseHost::query()->findOrFail($host);
|
||||
}
|
||||
|
||||
if (empty(array_get($data, 'password'))) {
|
||||
unset($data['password']);
|
||||
}
|
||||
|
||||
return $this->connection->transaction(function () use ($data, $hostId) {
|
||||
/** @var DatabaseHost $host */
|
||||
$host = DatabaseHost::query()->findOrFail($hostId);
|
||||
return $this->connection->transaction(function () use ($data, $host) {
|
||||
$host->update($data);
|
||||
|
||||
$this->dynamic->set('dynamic', $host);
|
||||
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');
|
||||
$this->databaseManager->connection('dynamic')->getPdo();
|
||||
|
||||
return $host;
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user