mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
[2.2.4] [WIP] Respect custom connections when creating database (#244)
* Add TenantDatabaseManager::setConnection() * Apply fixes from StyleCI
This commit is contained in:
parent
5145b1f13e
commit
fed8c0f9d1
4 changed files with 40 additions and 5 deletions
15
src/Contracts/Future/CanSetConnection.php
Normal file
15
src/Contracts/Future/CanSetConnection.php
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Contracts\Future;
|
||||||
|
|
||||||
|
use Illuminate\Database\Connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface *might* be part of the TenantDatabaseManager interface in 3.x.
|
||||||
|
*/
|
||||||
|
interface CanSetConnection
|
||||||
|
{
|
||||||
|
public function setConnection(Connection $connection);
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,7 @@ use Closure;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
|
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||||
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
|
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
|
||||||
|
|
@ -226,7 +227,7 @@ class DatabaseManager
|
||||||
*/
|
*/
|
||||||
public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
|
public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
|
||||||
{
|
{
|
||||||
$driver = $this->getDriver($this->getBaseConnection($tenant->getConnectionName()));
|
$driver = $this->getDriver($this->getBaseConnection($connectionName = $tenant->getConnectionName()));
|
||||||
|
|
||||||
$databaseManagers = $this->app['config']['tenancy.database_managers'];
|
$databaseManagers = $this->app['config']['tenancy.database_managers'];
|
||||||
|
|
||||||
|
|
@ -234,6 +235,12 @@ class DatabaseManager
|
||||||
throw new DatabaseManagerNotRegisteredException($driver);
|
throw new DatabaseManagerNotRegisteredException($driver);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->app[$databaseManagers[$driver]];
|
$databaseManager = $this->app[$databaseManagers[$driver]];
|
||||||
|
|
||||||
|
if ($connectionName !== 'tenant' && $databaseManager instanceof CanSetConnection) {
|
||||||
|
$databaseManager->setConnection($this->database->connection($connectionName));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $databaseManager;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,14 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Contracts\Config\Repository;
|
use Illuminate\Contracts\Config\Repository;
|
||||||
|
use Illuminate\Database\Connection;
|
||||||
use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager;
|
use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager;
|
||||||
|
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||||
|
|
||||||
class MySQLDatabaseManager implements TenantDatabaseManager
|
class MySQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
||||||
{
|
{
|
||||||
/** @var \Illuminate\Database\Connection */
|
/** @var Connection */
|
||||||
protected $database;
|
protected $database;
|
||||||
|
|
||||||
public function __construct(Repository $config, IlluminateDatabaseManager $databaseManager)
|
public function __construct(Repository $config, IlluminateDatabaseManager $databaseManager)
|
||||||
|
|
@ -18,6 +20,11 @@ class MySQLDatabaseManager implements TenantDatabaseManager
|
||||||
$this->database = $databaseManager->connection($config['tenancy.database_manager_connections.mysql']);
|
$this->database = $databaseManager->connection($config['tenancy.database_manager_connections.mysql']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setConnection(Connection $connection)
|
||||||
|
{
|
||||||
|
$this->database = $connection;
|
||||||
|
}
|
||||||
|
|
||||||
public function createDatabase(string $name): bool
|
public function createDatabase(string $name): bool
|
||||||
{
|
{
|
||||||
$charset = $this->database->getConfig('charset');
|
$charset = $this->database->getConfig('charset');
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,13 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Contracts\Config\Repository;
|
use Illuminate\Contracts\Config\Repository;
|
||||||
|
use Illuminate\Database\Connection;
|
||||||
use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager;
|
use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager;
|
||||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||||
|
|
||||||
class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
/** @var \Illuminate\Database\Connection */
|
/** @var Connection */
|
||||||
protected $database;
|
protected $database;
|
||||||
|
|
||||||
public function __construct(Repository $config, IlluminateDatabaseManager $databaseManager)
|
public function __construct(Repository $config, IlluminateDatabaseManager $databaseManager)
|
||||||
|
|
@ -18,6 +19,11 @@ class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
||||||
$this->database = $databaseManager->connection($config['tenancy.database_manager_connections.pgsql']);
|
$this->database = $databaseManager->connection($config['tenancy.database_manager_connections.pgsql']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setConnection(Connection $connection)
|
||||||
|
{
|
||||||
|
$this->database = $connection;
|
||||||
|
}
|
||||||
|
|
||||||
public function createDatabase(string $name): bool
|
public function createDatabase(string $name): bool
|
||||||
{
|
{
|
||||||
return $this->database->statement("CREATE DATABASE \"$name\" WITH TEMPLATE=template0");
|
return $this->database->statement("CREATE DATABASE \"$name\" WITH TEMPLATE=template0");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue