mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-19 18:01:37 +00:00
* fix: allow reuse of a database name * style --------- Co-authored-by: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com> Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
85 lines
2.4 KiB
PHP
Executable File
85 lines
2.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Actions\Database;
|
|
|
|
use App\Enums\DatabaseStatus;
|
|
use App\Models\Database;
|
|
use App\Models\Server;
|
|
use App\Models\Service;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class CreateDatabase
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $input
|
|
*/
|
|
public function create(Server $server, array $input): Database
|
|
{
|
|
$database = new Database([
|
|
'server_id' => $server->id,
|
|
'charset' => $input['charset'],
|
|
'collation' => $input['collation'],
|
|
'name' => $input['name'],
|
|
]);
|
|
|
|
/** @var Service $service */
|
|
$service = $server->database();
|
|
|
|
/** @var \App\SSH\Services\Database\Database $databaseHandler */
|
|
$databaseHandler = $service->handler();
|
|
$databaseHandler->create($database->name, $database->charset, $database->collation);
|
|
$database->status = DatabaseStatus::READY;
|
|
$database->save();
|
|
|
|
if (isset($input['user']) && $input['user']) {
|
|
$databaseUser = app(CreateDatabaseUser::class)->create($server, $input, [$database->name]);
|
|
|
|
app(LinkUser::class)->link($databaseUser, ['databases' => [$database->name]]);
|
|
}
|
|
|
|
return $database;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $input
|
|
* @return array<string, mixed>
|
|
*
|
|
* @throws ValidationException
|
|
*/
|
|
public static function rules(Server $server, array $input): array
|
|
{
|
|
$rules = [
|
|
'name' => [
|
|
'required',
|
|
'alpha_dash',
|
|
Rule::unique('databases', 'name')->where('server_id', $server->id)->whereNull('deleted_at'),
|
|
],
|
|
'charset' => [
|
|
'required',
|
|
'string',
|
|
],
|
|
'collation' => [
|
|
'required',
|
|
'string',
|
|
],
|
|
];
|
|
if (isset($input['user']) && $input['user']) {
|
|
$rules['username'] = [
|
|
'required',
|
|
'alpha_dash',
|
|
Rule::unique('database_users', 'username')->where('server_id', $server->id),
|
|
];
|
|
$rules['password'] = [
|
|
'required',
|
|
'min:6',
|
|
];
|
|
}
|
|
if (isset($input['remote']) && $input['remote']) {
|
|
$rules['host'] = 'required';
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
}
|