mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 00:34:44 +02:00
Remove DynamicDatabaseConnection
(#1290)
This commit is contained in:
parent
6427903f9f
commit
3deada57c6
@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Extensions;
|
|
||||||
|
|
||||||
use App\Models\DatabaseHost;
|
|
||||||
|
|
||||||
class DynamicDatabaseConnection
|
|
||||||
{
|
|
||||||
public const DB_CHARSET = 'utf8';
|
|
||||||
|
|
||||||
public const DB_COLLATION = 'utf8_unicode_ci';
|
|
||||||
|
|
||||||
public const DB_DRIVER = 'mysql';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a dynamic database connection entry to the runtime config.
|
|
||||||
*/
|
|
||||||
public function set(string $connection, DatabaseHost|int $host, string $database = 'mysql'): void
|
|
||||||
{
|
|
||||||
if (!$host instanceof DatabaseHost) {
|
|
||||||
$host = DatabaseHost::query()->findOrFail($host);
|
|
||||||
}
|
|
||||||
|
|
||||||
config()->set('database.connections.' . $connection, [
|
|
||||||
'driver' => self::DB_DRIVER,
|
|
||||||
'host' => $host->host,
|
|
||||||
'port' => $host->port,
|
|
||||||
'database' => $database,
|
|
||||||
'username' => $host->username,
|
|
||||||
'password' => $host->password,
|
|
||||||
'charset' => self::DB_CHARSET,
|
|
||||||
'collation' => self::DB_COLLATION,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,7 +8,6 @@ use Illuminate\Database\Eloquent\Casts\Attribute;
|
|||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property int $id
|
* @property int $id
|
||||||
@ -36,8 +35,6 @@ class Database extends Model implements Validatable
|
|||||||
*/
|
*/
|
||||||
public const RESOURCE_NAME = 'server_database';
|
public const RESOURCE_NAME = 'server_database';
|
||||||
|
|
||||||
public const DEFAULT_CONNECTION_NAME = 'dynamic';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes excluded from the model's JSON form.
|
* The attributes excluded from the model's JSON form.
|
||||||
*/
|
*/
|
||||||
@ -104,7 +101,7 @@ class Database extends Model implements Validatable
|
|||||||
*/
|
*/
|
||||||
private function run(string $statement): bool
|
private function run(string $statement): bool
|
||||||
{
|
{
|
||||||
return DB::connection(self::DEFAULT_CONNECTION_NAME)->statement($statement);
|
return $this->host->buildConnection()->statement($statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,10 +4,12 @@ namespace App\Models;
|
|||||||
|
|
||||||
use App\Contracts\Validatable;
|
use App\Contracts\Validatable;
|
||||||
use App\Traits\HasValidation;
|
use App\Traits\HasValidation;
|
||||||
|
use Illuminate\Database\Connection;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property int $id
|
* @property int $id
|
||||||
@ -82,4 +84,21 @@ class DatabaseHost extends Model implements Validatable
|
|||||||
{
|
{
|
||||||
return $this->hasMany(Database::class);
|
return $this->hasMany(Database::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function buildConnection(string $database = 'mysql', string $charset = 'utf8', string $collation = 'utf8_unicode_ci'): Connection
|
||||||
|
{
|
||||||
|
/** @var Connection $connection */
|
||||||
|
$connection = DB::build([
|
||||||
|
'driver' => 'mysql',
|
||||||
|
'host' => $this->host,
|
||||||
|
'port' => $this->port,
|
||||||
|
'database' => $database,
|
||||||
|
'username' => $this->username,
|
||||||
|
'password' => $this->password,
|
||||||
|
'charset' => $charset,
|
||||||
|
'collation' => $collation,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $connection;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ use App\Models\Server;
|
|||||||
use App\Models\Database;
|
use App\Models\Database;
|
||||||
use App\Helpers\Utilities;
|
use App\Helpers\Utilities;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use App\Extensions\DynamicDatabaseConnection;
|
|
||||||
use App\Exceptions\Repository\DuplicateDatabaseNameException;
|
use App\Exceptions\Repository\DuplicateDatabaseNameException;
|
||||||
use App\Exceptions\Service\Database\TooManyDatabasesException;
|
use App\Exceptions\Service\Database\TooManyDatabasesException;
|
||||||
use App\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
|
use App\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
|
||||||
@ -32,7 +31,6 @@ class DatabaseManagementService
|
|||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected ConnectionInterface $connection,
|
protected ConnectionInterface $connection,
|
||||||
protected DynamicDatabaseConnection $dynamic,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,8 +92,6 @@ class DatabaseManagementService
|
|||||||
return $this->connection->transaction(function () use ($data) {
|
return $this->connection->transaction(function () use ($data) {
|
||||||
$database = $this->createModel($data);
|
$database = $this->createModel($data);
|
||||||
|
|
||||||
$this->dynamic->set('dynamic', $data['database_host_id']);
|
|
||||||
|
|
||||||
$database->createDatabase($database->database);
|
$database->createDatabase($database->database);
|
||||||
$database->createUser(
|
$database->createUser(
|
||||||
$database->username,
|
$database->username,
|
||||||
@ -122,8 +118,6 @@ class DatabaseManagementService
|
|||||||
*/
|
*/
|
||||||
public function delete(Database $database): ?bool
|
public function delete(Database $database): ?bool
|
||||||
{
|
{
|
||||||
$this->dynamic->set('dynamic', $database->database_host_id);
|
|
||||||
|
|
||||||
$database->dropDatabase($database->database);
|
$database->dropDatabase($database->database);
|
||||||
$database->dropUser($database->username, $database->remote);
|
$database->dropUser($database->username, $database->remote);
|
||||||
$database->flush();
|
$database->flush();
|
||||||
|
@ -5,7 +5,6 @@ namespace App\Services\Databases;
|
|||||||
use App\Models\Database;
|
use App\Models\Database;
|
||||||
use App\Helpers\Utilities;
|
use App\Helpers\Utilities;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use App\Extensions\DynamicDatabaseConnection;
|
|
||||||
|
|
||||||
class DatabasePasswordService
|
class DatabasePasswordService
|
||||||
{
|
{
|
||||||
@ -14,7 +13,6 @@ class DatabasePasswordService
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ConnectionInterface $connection,
|
private ConnectionInterface $connection,
|
||||||
private DynamicDatabaseConnection $dynamic,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,8 +27,6 @@ class DatabasePasswordService
|
|||||||
$password = Utilities::randomStringWithSpecialCharacters(24);
|
$password = Utilities::randomStringWithSpecialCharacters(24);
|
||||||
|
|
||||||
$this->connection->transaction(function () use ($database, $password) {
|
$this->connection->transaction(function () use ($database, $password) {
|
||||||
$this->dynamic->set('dynamic', $database->database_host_id);
|
|
||||||
|
|
||||||
$database->update([
|
$database->update([
|
||||||
'password' => $password,
|
'password' => $password,
|
||||||
]);
|
]);
|
||||||
|
@ -3,9 +3,7 @@
|
|||||||
namespace App\Services\Databases\Hosts;
|
namespace App\Services\Databases\Hosts;
|
||||||
|
|
||||||
use App\Models\DatabaseHost;
|
use App\Models\DatabaseHost;
|
||||||
use Illuminate\Database\DatabaseManager;
|
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use App\Extensions\DynamicDatabaseConnection;
|
|
||||||
|
|
||||||
class HostCreationService
|
class HostCreationService
|
||||||
{
|
{
|
||||||
@ -14,8 +12,6 @@ class HostCreationService
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ConnectionInterface $connection,
|
private ConnectionInterface $connection,
|
||||||
private DatabaseManager $databaseManager,
|
|
||||||
private DynamicDatabaseConnection $dynamic,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,8 +44,7 @@ class HostCreationService
|
|||||||
$host->nodes()->sync(array_get($data, 'node_ids', []));
|
$host->nodes()->sync(array_get($data, 'node_ids', []));
|
||||||
|
|
||||||
// Confirm access using the provided credentials before saving data.
|
// Confirm access using the provided credentials before saving data.
|
||||||
$this->dynamic->set('dynamic', $host);
|
$host->buildConnection()->getPdo();
|
||||||
$this->databaseManager->connection('dynamic')->getPdo();
|
|
||||||
|
|
||||||
return $host;
|
return $host;
|
||||||
});
|
});
|
||||||
|
@ -3,9 +3,7 @@
|
|||||||
namespace App\Services\Databases\Hosts;
|
namespace App\Services\Databases\Hosts;
|
||||||
|
|
||||||
use App\Models\DatabaseHost;
|
use App\Models\DatabaseHost;
|
||||||
use Illuminate\Database\DatabaseManager;
|
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use App\Extensions\DynamicDatabaseConnection;
|
|
||||||
|
|
||||||
class HostUpdateService
|
class HostUpdateService
|
||||||
{
|
{
|
||||||
@ -14,8 +12,6 @@ class HostUpdateService
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ConnectionInterface $connection,
|
private ConnectionInterface $connection,
|
||||||
private DatabaseManager $databaseManager,
|
|
||||||
private DynamicDatabaseConnection $dynamic,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,8 +34,8 @@ class HostUpdateService
|
|||||||
return $this->connection->transaction(function () use ($data, $host) {
|
return $this->connection->transaction(function () use ($data, $host) {
|
||||||
$host->update($data);
|
$host->update($data);
|
||||||
|
|
||||||
$this->dynamic->set('dynamic', $host);
|
// Confirm access using the provided credentials before saving data.
|
||||||
$this->databaseManager->connection('dynamic')->getPdo();
|
$host->buildConnection()->getPdo();
|
||||||
|
|
||||||
return $host;
|
return $host;
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user