mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 21:34:04 +00:00
refactor TenantDatabaseManagers
This commit is contained in:
parent
824292e6df
commit
d2e1ce0a1e
10 changed files with 52 additions and 126 deletions
|
|
@ -9,6 +9,8 @@ use Stancl\Tenancy\Enums\LogMode;
|
||||||
use Stancl\Tenancy\Events\Contracts\TenancyEvent;
|
use Stancl\Tenancy\Events\Contracts\TenancyEvent;
|
||||||
use Stancl\Tenancy\Tenancy;
|
use Stancl\Tenancy\Tenancy;
|
||||||
|
|
||||||
|
// todo finish this feature
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @mixin Tenancy
|
* @mixin Tenancy
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,17 @@ use Stancl\Tenancy\Database\Contracts\TenantWithDatabase as Tenant;
|
||||||
|
|
||||||
class DatabaseConfig
|
class DatabaseConfig
|
||||||
{
|
{
|
||||||
// todo docblocks
|
/** The tenant whose database we're dealing with. */
|
||||||
public Tenant&Model $tenant;
|
public Tenant&Model $tenant;
|
||||||
|
|
||||||
public static Closure $usernameGenerator;
|
/** Database username generator (can be set by the developer.) */
|
||||||
|
public static Closure|null $usernameGenerator = null;
|
||||||
|
|
||||||
public static Closure $passwordGenerator;
|
/** Database password generator (can be set by the developer.) */
|
||||||
|
public static Closure|null $passwordGenerator = null;
|
||||||
|
|
||||||
public static Closure $databaseNameGenerator;
|
/** Database name generator (can be set by the developer.) */
|
||||||
|
public static Closure|null $databaseNameGenerator = null;
|
||||||
|
|
||||||
public static function __constructStatic(): void
|
public static function __constructStatic(): void
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,34 +2,12 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
// todo likely move all of these classes to Database\
|
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Database\Connection;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantDatabaseManager;
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
|
||||||
|
|
||||||
class MicrosoftSQLDatabaseManager implements TenantDatabaseManager
|
class MicrosoftSQLDatabaseManager extends TenantDatabaseManager
|
||||||
{
|
{
|
||||||
protected string $connection; // todo docblock, in all of these classes
|
|
||||||
|
|
||||||
protected function database(): Connection // todo consider abstracting this method & setConnection() into a base class
|
|
||||||
{
|
|
||||||
if ($this->connection === null) {
|
|
||||||
throw new NoConnectionSetException(static::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
return DB::connection($this->connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setConnection(string $connection): void
|
|
||||||
{
|
|
||||||
$this->connection = $connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||||
{
|
{
|
||||||
$database = $tenant->database()->getName();
|
$database = $tenant->database()->getName();
|
||||||
|
|
@ -48,11 +26,4 @@ class MicrosoftSQLDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
return (bool) $this->database()->select("SELECT name FROM master.sys.databases WHERE name = '$name'");
|
return (bool) $this->database()->select("SELECT name FROM master.sys.databases WHERE name = '$name'");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
|
||||||
{
|
|
||||||
$baseConfig['database'] = $databaseName;
|
|
||||||
|
|
||||||
return $baseConfig;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,31 +4,10 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Database\Connection;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantDatabaseManager;
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
|
||||||
|
|
||||||
class MySQLDatabaseManager implements TenantDatabaseManager
|
class MySQLDatabaseManager extends TenantDatabaseManager
|
||||||
{
|
{
|
||||||
/** @var string */
|
|
||||||
protected $connection;
|
|
||||||
|
|
||||||
protected function database(): Connection
|
|
||||||
{
|
|
||||||
if ($this->connection === null) {
|
|
||||||
throw new NoConnectionSetException(static::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
return DB::connection($this->connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setConnection(string $connection): void
|
|
||||||
{
|
|
||||||
$this->connection = $connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||||
{
|
{
|
||||||
$database = $tenant->database()->getName();
|
$database = $tenant->database()->getName();
|
||||||
|
|
@ -47,11 +26,4 @@ class MySQLDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
return (bool) $this->database()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$name'");
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,11 +54,4 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl
|
||||||
{
|
{
|
||||||
return (bool) $this->database()->select("SELECT count(*) FROM mysql.user WHERE user = '$username'")[0]->{'count(*)'};
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,31 +4,10 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Database\Connection;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantDatabaseManager;
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
|
||||||
|
|
||||||
class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
class PostgreSQLDatabaseManager extends TenantDatabaseManager
|
||||||
{
|
{
|
||||||
/** @var string */
|
|
||||||
protected $connection;
|
|
||||||
|
|
||||||
protected function database(): Connection
|
|
||||||
{
|
|
||||||
if ($this->connection === null) {
|
|
||||||
throw new NoConnectionSetException(static::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
return DB::connection($this->connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setConnection(string $connection): void
|
|
||||||
{
|
|
||||||
$this->connection = $connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||||
{
|
{
|
||||||
return $this->database()->statement("CREATE DATABASE \"{$tenant->database()->getName()}\" WITH TEMPLATE=template0");
|
return $this->database()->statement("CREATE DATABASE \"{$tenant->database()->getName()}\" WITH TEMPLATE=template0");
|
||||||
|
|
@ -43,11 +22,4 @@ class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
return (bool) $this->database()->select("SELECT datname FROM pg_database WHERE datname = '$name'");
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,31 +4,10 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Database\Connection;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantDatabaseManager;
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
|
||||||
|
|
||||||
class PostgreSQLSchemaManager implements TenantDatabaseManager
|
class PostgreSQLSchemaManager extends TenantDatabaseManager
|
||||||
{
|
{
|
||||||
/** @var string */
|
|
||||||
protected $connection;
|
|
||||||
|
|
||||||
protected function database(): Connection
|
|
||||||
{
|
|
||||||
if ($this->connection === null) {
|
|
||||||
throw new NoConnectionSetException(static::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
return DB::connection($this->connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setConnection(string $connection): void
|
|
||||||
{
|
|
||||||
$this->connection = $connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||||
{
|
{
|
||||||
return $this->database()->statement("CREATE SCHEMA \"{$tenant->database()->getName()}\"");
|
return $this->database()->statement("CREATE SCHEMA \"{$tenant->database()->getName()}\"");
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantDatabaseManager;
|
use Stancl\Tenancy\Database\Contracts\TenantDatabaseManager;
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
class SQLiteDatabaseManager implements TenantDatabaseManager
|
class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
|
|
@ -13,7 +14,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return file_put_contents(database_path($tenant->database()->getName()), '');
|
return file_put_contents(database_path($tenant->database()->getName()), '');
|
||||||
} catch (\Throwable) {
|
} catch (Throwable) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -22,7 +23,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return unlink(database_path($tenant->database()->getName()));
|
return unlink(database_path($tenant->database()->getName()));
|
||||||
} catch (\Throwable) {
|
} catch (Throwable) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||||
|
|
||||||
|
use Illuminate\Database\Connection;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Stancl\Tenancy\Database\Contracts\TenantDatabaseManager as Contract;
|
||||||
|
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
||||||
|
|
||||||
|
abstract class TenantDatabaseManager implements Contract // todo better naming?
|
||||||
|
{
|
||||||
|
/** The database connection to the server. */
|
||||||
|
protected string $connection;
|
||||||
|
|
||||||
|
protected function database(): Connection
|
||||||
|
{
|
||||||
|
if (! isset($this->connection)) {
|
||||||
|
throw new NoConnectionSetException(static::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB::connection($this->connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setConnection(string $connection): void
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||||
|
{
|
||||||
|
$baseConfig['database'] = $databaseName;
|
||||||
|
|
||||||
|
return $baseConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -26,9 +26,7 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver
|
||||||
|
|
||||||
/** @var Tenant|null $tenant */
|
/** @var Tenant|null $tenant */
|
||||||
$tenant = config('tenancy.tenant_model')::query()
|
$tenant = config('tenancy.tenant_model')::query()
|
||||||
->whereHas('domains', function (Builder $query) use ($domain) {
|
->whereHas('domains', fn (Builder $query) => $query->where('domain', $domain))
|
||||||
$query->where('domain', $domain);
|
|
||||||
})
|
|
||||||
->with('domains')
|
->with('domains')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue