Skip to content

Commit

Permalink
Make DynamicDatabaseConnection static
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoResonance committed Jan 9, 2025
1 parent 9cfd870 commit c81a161
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/Extensions/DynamicDatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DynamicDatabaseConnection
/**
* Adds a dynamic database connection entry to the runtime config.
*/
public function set(string $connection, DatabaseHost|int $host, string $database = 'mysql'): void
public static function set(string $connection, DatabaseHost|int $host, string $database = 'mysql'): void
{
if (!$host instanceof DatabaseHost) {
$host = DatabaseHost::query()->findOrFail($host);
Expand Down
5 changes: 2 additions & 3 deletions app/Services/Databases/DatabaseManagementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class DatabaseManagementService

public function __construct(
protected ConnectionInterface $connection,
protected DynamicDatabaseConnection $dynamic,
) {}

/**
Expand Down Expand Up @@ -91,7 +90,7 @@ public function create(Server $server, array $data): Database
return $this->connection->transaction(function () use ($data) {
$database = $this->createModel($data);

$this->dynamic->set('dynamic', $data['database_host_id']);
DynamicDatabaseConnection::set('dynamic', $data['database_host_id']);

$database->createDatabase($database->database);
$database->createUser(
Expand All @@ -114,7 +113,7 @@ public function create(Server $server, array $data): Database
*/
public function delete(Database $database): ?bool
{
$this->dynamic->set('dynamic', $database->database_host_id);
DynamicDatabaseConnection::set('dynamic', $database->database_host_id);

$database->dropDatabase($database->database);
$database->dropUser($database->username, $database->remote);
Expand Down
3 changes: 1 addition & 2 deletions app/Services/Databases/DatabasePasswordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class DatabasePasswordService
*/
public function __construct(
private ConnectionInterface $connection,
private DynamicDatabaseConnection $dynamic,
) {}

/**
Expand All @@ -29,7 +28,7 @@ public function handle(Database|int $database): void
$password = Utilities::randomStringWithSpecialCharacters(24);

$this->connection->transaction(function () use ($database, $password) {
$this->dynamic->set('dynamic', $database->database_host_id);
DynamicDatabaseConnection::set('dynamic', $database->database_host_id);

$database->update([
'password' => $password,
Expand Down
3 changes: 1 addition & 2 deletions app/Services/Databases/Hosts/HostCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class HostCreationService
public function __construct(
private ConnectionInterface $connection,
private DatabaseManager $databaseManager,
private DynamicDatabaseConnection $dynamic,
) {}

/**
Expand All @@ -38,7 +37,7 @@ public function handle(array $data): DatabaseHost
$host->nodes()->sync(array_get($data, 'node_ids', []));

// Confirm access using the provided credentials before saving data.
$this->dynamic->set('dynamic', $host);
DynamicDatabaseConnection::set('dynamic', $host);
$this->databaseManager->connection('dynamic')->getPdo();

return $host;
Expand Down
3 changes: 1 addition & 2 deletions app/Services/Databases/Hosts/HostUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class HostUpdateService
public function __construct(
private ConnectionInterface $connection,
private DatabaseManager $databaseManager,
private DynamicDatabaseConnection $dynamic,
) {}

/**
Expand All @@ -36,7 +35,7 @@ public function handle(DatabaseHost|int $host, array $data): DatabaseHost
return $this->connection->transaction(function () use ($data, $host) {
$host->update($data);

$this->dynamic->set('dynamic', $host);
DynamicDatabaseConnection::set('dynamic', $host);
$this->databaseManager->connection('dynamic')->getPdo();

return $host;
Expand Down

0 comments on commit c81a161

Please sign in to comment.