mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-03 15:02:34 +00:00
Add database and database users sync (#537)
* Add database and database users sync * get mysl users * add mariadb and postgres * fix phpstan
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\SSH\Services\Database;
|
||||
|
||||
use App\Actions\Database\SyncDatabases;
|
||||
use App\Enums\BackupStatus;
|
||||
use App\Exceptions\ServiceInstallationFailed;
|
||||
use App\Exceptions\SSHError;
|
||||
@ -16,6 +17,11 @@ abstract class AbstractDatabase extends AbstractService implements Database
|
||||
*/
|
||||
protected array $systemDbs = [];
|
||||
|
||||
/**
|
||||
* @var array<string>
|
||||
*/
|
||||
protected array $systemUsers = [];
|
||||
|
||||
protected string $defaultCharset;
|
||||
|
||||
protected string $separator = "\t";
|
||||
@ -60,9 +66,8 @@ public function install(): void
|
||||
$status = $this->service->server->systemd()->status($this->service->unit);
|
||||
$this->service->validateInstall($status);
|
||||
$this->service->server->os()->cleanup();
|
||||
|
||||
$this->updateCharsets();
|
||||
$this->syncDatabases();
|
||||
/** @TODO implement post-install for services and move it there */
|
||||
app(SyncDatabases::class)->sync($this->service->server);
|
||||
}
|
||||
|
||||
public function deletionRules(): array
|
||||
@ -245,7 +250,7 @@ public function restoreBackup(BackupFile $backupFile, string $database): void
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function updateCharsets(): void
|
||||
public function getCharsets(): array
|
||||
{
|
||||
$data = $this->service->server->ssh()->exec(
|
||||
view($this->getScriptView('get-charsets')),
|
||||
@ -281,25 +286,22 @@ public function updateCharsets(): void
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($results as $charset => $data) {
|
||||
foreach ($results as $charset => $value) {
|
||||
$results[$charset]['list'] = $charsetCollations[$charset];
|
||||
}
|
||||
|
||||
ksort($results);
|
||||
|
||||
$data = array_merge(
|
||||
$this->service->type_data ?? [],
|
||||
['charsets' => $results, 'defaultCharset' => $this->defaultCharset]
|
||||
);
|
||||
|
||||
$this->service->update(['type_data' => $data]);
|
||||
|
||||
return [
|
||||
'charsets' => $results,
|
||||
'defaultCharset' => $this->defaultCharset,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function syncDatabases(bool $createNew = true): void
|
||||
public function getDatabases(): array
|
||||
{
|
||||
$data = $this->service->server->ssh()->exec(
|
||||
view($this->getScriptView('get-db-list')),
|
||||
@ -308,35 +310,36 @@ public function syncDatabases(bool $createNew = true): void
|
||||
|
||||
$databases = $this->tableToArray($data);
|
||||
|
||||
foreach ($databases as $database) {
|
||||
if (in_array($database[0], $this->systemDbs)) {
|
||||
continue;
|
||||
}
|
||||
return array_values(array_filter($databases, function ($database) {
|
||||
return ! in_array($database[0], $this->systemDbs);
|
||||
}));
|
||||
}
|
||||
|
||||
/** @var ?\App\Models\Database $db */
|
||||
$db = $this->service->server->databases()
|
||||
->where('name', $database[0])
|
||||
->first();
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function getUsers(): array
|
||||
{
|
||||
$data = $this->service->server->ssh()->exec(
|
||||
view($this->getScriptView('get-users-list')),
|
||||
'get-users-list'
|
||||
);
|
||||
|
||||
if ($db === null) {
|
||||
if ($createNew) {
|
||||
$this->service->server->databases()->create([
|
||||
'name' => $database[0],
|
||||
'collation' => $database[2],
|
||||
'charset' => $database[1],
|
||||
]);
|
||||
}
|
||||
$users = $this->tableToArray($data);
|
||||
|
||||
continue;
|
||||
}
|
||||
$users = array_values(array_filter($users, function ($users) {
|
||||
return ! in_array($users[0], $this->systemUsers);
|
||||
}));
|
||||
|
||||
if ($db->collation !== $database[2] || $db->charset !== $database[1]) {
|
||||
$db->update([
|
||||
'collation' => $database[2],
|
||||
'charset' => $database[1],
|
||||
]);
|
||||
}
|
||||
foreach ($users as $key => $user) {
|
||||
$databases = explode(',', $user[2]);
|
||||
$databases = array_values(array_filter($databases, function ($database) {
|
||||
return ! in_array($database, $this->systemDbs);
|
||||
}));
|
||||
$users[$key][2] = implode(',', $databases);
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user