1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 21:54: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:
Noor Adiana 2020-03-11 02:15:07 +07:00 committed by GitHub
parent e02bd3927a
commit d0023c482a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 221 additions and 2 deletions

View file

@ -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';
}
}