1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 11:14:03 +00:00
This commit is contained in:
Samuel Štancl 2019-12-02 20:36:44 +01:00
parent 2ff3dd4283
commit 743369cde7
6 changed files with 107 additions and 32 deletions

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy;
use Closure;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
use Illuminate\Foundation\Application;
@ -100,12 +101,9 @@ class DatabaseManager
// Change database name.
$databaseName = $this->getDriver($connectionName) === 'sqlite' ? database_path($databaseName) : $databaseName;
$separateBy = $this->separateBy($connectionName);
if ($this->isUsingSchema($connectionName)) {
$this->app['config']["database.connections.$connectionName.schema"] = $databaseName;
} else {
$this->app['config']["database.connections.$connectionName.database"] = $databaseName;
}
$this->app['config']["database.connections.$connectionName.$separateBy"] = $databaseName;
}
/**
@ -231,7 +229,7 @@ class DatabaseManager
*/
public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
{
$driver = $this->isUsingSchema($tenant->getConnectionName()) ? 'schema' : $this->getDriver($this->getBaseConnection($tenant->getConnectionName()));
$driver = $this->getDriver($this->getBaseConnection($tenant->getConnectionName()));
$databaseManagers = $this->app['config']['tenancy.database_managers'];
@ -243,13 +241,18 @@ class DatabaseManager
}
/**
* Check if using schema connection
* What key on the connection config should be used to separate tenants.
*
* @param string $connectionName
* @return bool
* @return string
*/
protected function isUsingSchema(string $connectionName): bool
public function separateBy(string $connectionName): string
{
return $this->getDriver($this->getBaseConnection($connectionName)) === 'pgsql' && $this->app['config']['tenancy.using_schema_connection'];
if ($this->getDriver($this->getBaseConnection($connectionName)) === 'pgsql'
&& $this->app['config']['tenancy.separate_by'] === 'schema') {
return 'schema';
}
return 'database';
}
}