mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 11:14:04 +00:00
Refactor DB config & Tenant DB managers
This commit is contained in:
parent
3c4d2189dc
commit
64383b4c56
8 changed files with 51 additions and 41 deletions
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Contracts;
|
||||
|
||||
/**
|
||||
* Used by sqlite to wrap database name in database_path().
|
||||
*/
|
||||
interface ModifiesDatabaseNameForConnection
|
||||
{
|
||||
public function getDatabaseNameForConnection(string $original): string;
|
||||
}
|
||||
|
|
@ -6,13 +6,6 @@ namespace Stancl\Tenancy\Contracts;
|
|||
|
||||
interface TenantDatabaseManager
|
||||
{
|
||||
/**
|
||||
* Return the config key that separates databases (e.g. 'database' or 'schema').
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSeparator(): string;
|
||||
|
||||
/**
|
||||
* Create a database.
|
||||
*/
|
||||
|
|
@ -30,4 +23,13 @@ interface TenantDatabaseManager
|
|||
* @return bool
|
||||
*/
|
||||
public function databaseExists(string $name): bool;
|
||||
|
||||
/**
|
||||
* Make a DB connection config array.
|
||||
*
|
||||
* @param array $baseConfig
|
||||
* @param string $databaseName
|
||||
* @return array
|
||||
*/
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,19 +102,11 @@ class DatabaseConfig
|
|||
public function connection(): array
|
||||
{
|
||||
$template = $this->getTemplateConnectionName();
|
||||
|
||||
$templateConnection = config("database.connections.{$template}");
|
||||
|
||||
// todo move a lot of this logic to the tenant DB manager so that we dont have to deal with the separators & modifying DB names here
|
||||
$databaseName = $this->getName();
|
||||
if (($manager = $this->manager()) instanceof ModifiesDatabaseNameForConnection) {
|
||||
/** @var ModifiesDatabaseNameForConnection $manager */
|
||||
$databaseName = $manager->getDatabaseNameForConnection($databaseName);
|
||||
}
|
||||
|
||||
return array_merge($templateConnection, $this->tenantConfig(), [
|
||||
$this->manager()->getSeparator() => $databaseName,
|
||||
]);
|
||||
return $this->manager()->makeConnectionConfig(
|
||||
array_merge($templateConnection, $this->tenantConfig()), $this->getName()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -54,4 +54,11 @@ class MySQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
|||
{
|
||||
return (bool) $this->database()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$name'");
|
||||
}
|
||||
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||
{
|
||||
$baseConfig['database'] = $databaseName;
|
||||
|
||||
return $baseConfig;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,4 +59,11 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl
|
|||
{
|
||||
return (bool) $this->database()->select("SELECT count(*) FROM mysql.user WHERE user = '$username'")[0]->{'count(*)'};
|
||||
}
|
||||
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||
{
|
||||
$baseConfig['database'] = $databaseName;
|
||||
|
||||
return $baseConfig;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use Illuminate\Database\Connection;
|
|||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
|
||||
class PostgreSQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
||||
{
|
||||
|
|
@ -36,12 +36,12 @@ class PostgreSQLDatabaseManager implements TenantDatabaseManager, CanSetConnecti
|
|||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function createDatabase(Tenant $tenant): bool
|
||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
return $this->database()->statement("CREATE DATABASE \"{$tenant->database()->getName()}\" WITH TEMPLATE=template0");
|
||||
}
|
||||
|
||||
public function deleteDatabase(Tenant $tenant): bool
|
||||
public function deleteDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
return $this->database()->statement("DROP DATABASE \"{$tenant->database()->getName()}\"");
|
||||
}
|
||||
|
|
@ -50,4 +50,11 @@ class PostgreSQLDatabaseManager implements TenantDatabaseManager, CanSetConnecti
|
|||
{
|
||||
return (bool) $this->database()->select("SELECT datname FROM pg_database WHERE datname = '$name'");
|
||||
}
|
||||
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||
{
|
||||
$baseConfig['database'] = $databaseName;
|
||||
|
||||
return $baseConfig;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use Illuminate\Database\Connection;
|
|||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
|
||||
class PostgreSQLSchemaManager implements TenantDatabaseManager, CanSetConnection
|
||||
{
|
||||
|
|
@ -36,12 +36,12 @@ class PostgreSQLSchemaManager implements TenantDatabaseManager, CanSetConnection
|
|||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function createDatabase(Tenant $tenant): bool
|
||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
return $this->database()->statement("CREATE SCHEMA \"{$tenant->database()->getName()}\"");
|
||||
}
|
||||
|
||||
public function deleteDatabase(Tenant $tenant): bool
|
||||
public function deleteDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
return $this->database()->statement("DROP SCHEMA \"{$tenant->database()->getName()}\"");
|
||||
}
|
||||
|
|
@ -50,4 +50,11 @@ class PostgreSQLSchemaManager implements TenantDatabaseManager, CanSetConnection
|
|||
{
|
||||
return (bool) $this->database()->select("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$name'");
|
||||
}
|
||||
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||
{
|
||||
$baseConfig['schema'] = $databaseName;
|
||||
|
||||
return $baseConfig;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,10 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||
|
||||
use Stancl\Tenancy\Contracts\ModifiesDatabaseNameForConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
|
||||
class SQLiteDatabaseManager implements TenantDatabaseManager, ModifiesDatabaseNameForConnection
|
||||
class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||
{
|
||||
public function getSeparator(): string
|
||||
{
|
||||
|
|
@ -38,8 +37,10 @@ class SQLiteDatabaseManager implements TenantDatabaseManager, ModifiesDatabaseNa
|
|||
return file_exists(database_path($name));
|
||||
}
|
||||
|
||||
public function getDatabaseNameForConnection(string $original): string
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||
{
|
||||
return database_path($original);
|
||||
$baseConfig['database'] = database_path($databaseName);;
|
||||
|
||||
return $baseConfig;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue