mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:04:03 +00:00
Add support for postgres schema (#237)
* Add support for postgres schema * wip * Apply fixes from StyleCI * revert to db as default for pgsql * Move separate_by to database * Fixing testing * Fixing style * Reverted change * Store string instead of Connection instance * Remove use statement * Add use statement for DB facade * mysql -> pgsql Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
parent
e02bd3927a
commit
d0023c482a
6 changed files with 221 additions and 2 deletions
|
|
@ -9,6 +9,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
|
|||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
||||
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
|
||||
|
|
@ -101,7 +102,9 @@ class DatabaseManager
|
|||
|
||||
// Change database name.
|
||||
$databaseName = $this->getDriver($connectionName) === 'sqlite' ? database_path($databaseName) : $databaseName;
|
||||
$this->app['config']["database.connections.$connectionName.database"] = $databaseName;
|
||||
$separateBy = $this->separateBy($connectionName);
|
||||
|
||||
$this->app['config']["database.connections.$connectionName.$separateBy"] = $databaseName;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -147,6 +150,8 @@ class DatabaseManager
|
|||
* @param Tenant $tenant
|
||||
* @return void
|
||||
* @throws TenantCannotBeCreatedException
|
||||
* @throws DatabaseManagerNotRegisteredException
|
||||
* @throws TenantDatabaseAlreadyExistsException
|
||||
*/
|
||||
public function ensureTenantCanBeCreated(Tenant $tenant): void
|
||||
{
|
||||
|
|
@ -161,6 +166,7 @@ class DatabaseManager
|
|||
* @param Tenant $tenant
|
||||
* @param ShouldQueue[]|callable[] $afterCreating
|
||||
* @return void
|
||||
* @throws DatabaseManagerNotRegisteredException
|
||||
*/
|
||||
public function createDatabase(Tenant $tenant, array $afterCreating = [])
|
||||
{
|
||||
|
|
@ -202,6 +208,7 @@ class DatabaseManager
|
|||
*
|
||||
* @param Tenant $tenant
|
||||
* @return void
|
||||
* @throws DatabaseManagerNotRegisteredException
|
||||
*/
|
||||
public function deleteDatabase(Tenant $tenant)
|
||||
{
|
||||
|
|
@ -224,6 +231,7 @@ class DatabaseManager
|
|||
*
|
||||
* @param Tenant $tenant
|
||||
* @return TenantDatabaseManager
|
||||
* @throws DatabaseManagerNotRegisteredException
|
||||
*/
|
||||
public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
|
||||
{
|
||||
|
|
@ -243,4 +251,20 @@ class DatabaseManager
|
|||
|
||||
return $databaseManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* What key on the connection config should be used to separate tenants.
|
||||
*
|
||||
* @param string $connectionName
|
||||
* @return string
|
||||
*/
|
||||
public function separateBy(string $connectionName): string
|
||||
{
|
||||
if ($this->getDriver($this->getBaseConnection($connectionName)) === 'pgsql'
|
||||
&& $this->app['config']['tenancy.database.separate_by'] === 'schema') {
|
||||
return 'schema';
|
||||
}
|
||||
|
||||
return 'database';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
47
src/TenantDatabaseManagers/PostgreSQLSchemaManager.php
Normal file
47
src/TenantDatabaseManagers/PostgreSQLSchemaManager.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
|
||||
class PostgreSQLSchemaManager implements TenantDatabaseManager, CanSetConnection
|
||||
{
|
||||
/** @var string */
|
||||
protected $connection;
|
||||
|
||||
public function __construct(Repository $config)
|
||||
{
|
||||
$this->connection = $config->get('tenancy.database_manager_connections.pgsql');
|
||||
}
|
||||
|
||||
protected function database(): Connection
|
||||
{
|
||||
return DB::connection($this->connection);
|
||||
}
|
||||
|
||||
public function setConnection(string $connection): void
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function createDatabase(string $name): bool
|
||||
{
|
||||
return $this->database()->statement("CREATE SCHEMA \"$name\"");
|
||||
}
|
||||
|
||||
public function deleteDatabase(string $name): bool
|
||||
{
|
||||
return $this->database()->statement("DROP SCHEMA \"$name\"");
|
||||
}
|
||||
|
||||
public function databaseExists(string $name): bool
|
||||
{
|
||||
return (bool) $this->database()->select("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$name'");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue