mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:14:03 +00:00
add public connection() method to the Tenant DB manager interface
This commit is contained in:
parent
5d688e6e5d
commit
fe0a322b87
6 changed files with 34 additions and 17 deletions
22
src/Database/Contracts/StatefulTenantDatabaseManager.php
Normal file
22
src/Database/Contracts/StatefulTenantDatabaseManager.php
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Database\Contracts;
|
||||||
|
|
||||||
|
use Illuminate\Database\Connection;
|
||||||
|
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tenant database manager with a persistent connection.
|
||||||
|
*/
|
||||||
|
interface StatefulTenantDatabaseManager extends TenantDatabaseManager
|
||||||
|
{
|
||||||
|
/** Get the DB connection used by the tenant database manager. */
|
||||||
|
public function database(): Connection; // todo rename to connection()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the DB connection that should be used by the tenant database manager.
|
||||||
|
*
|
||||||
|
* @throws NoConnectionSetException
|
||||||
|
*/
|
||||||
|
public function setConnection(string $connection): void;
|
||||||
|
}
|
||||||
|
|
@ -4,8 +4,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Database\Contracts;
|
namespace Stancl\Tenancy\Database\Contracts;
|
||||||
|
|
||||||
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
|
||||||
|
|
||||||
interface TenantDatabaseManager
|
interface TenantDatabaseManager
|
||||||
{
|
{
|
||||||
/** Create a database. */
|
/** Create a database. */
|
||||||
|
|
@ -19,11 +17,4 @@ interface TenantDatabaseManager
|
||||||
|
|
||||||
/** Construct a DB connection config array. */
|
/** Construct a DB connection config array. */
|
||||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array;
|
public function makeConnectionConfig(array $baseConfig, string $databaseName): array;
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the DB connection that should be used by the tenant database manager.
|
|
||||||
*
|
|
||||||
* @throws NoConnectionSetException
|
|
||||||
*/
|
|
||||||
public function setConnection(string $connection): void;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -154,7 +154,9 @@ class DatabaseConfig
|
||||||
/** @var Contracts\TenantDatabaseManager $databaseManager */
|
/** @var Contracts\TenantDatabaseManager $databaseManager */
|
||||||
$databaseManager = app($databaseManagers[$driver]);
|
$databaseManager = app($databaseManagers[$driver]);
|
||||||
|
|
||||||
$databaseManager->setConnection($this->getTemplateConnectionName());
|
if ($databaseManager instanceof Contracts\StatefulTenantDatabaseManager) {
|
||||||
|
$databaseManager->setConnection($this->getTemplateConnectionName());
|
||||||
|
}
|
||||||
|
|
||||||
return $databaseManager;
|
return $databaseManager;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,15 @@ namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Database\Connection;
|
use Illuminate\Database\Connection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantDatabaseManager as Contract;
|
use Stancl\Tenancy\Database\Contracts\StatefulTenantDatabaseManager;
|
||||||
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException;
|
||||||
|
|
||||||
abstract class TenantDatabaseManager implements Contract // todo better naming?
|
abstract class TenantDatabaseManager implements StatefulTenantDatabaseManager
|
||||||
{
|
{
|
||||||
/** The database connection to the server. */
|
/** The database connection to the server. */
|
||||||
protected string $connection;
|
protected string $connection;
|
||||||
|
|
||||||
protected function database(): Connection
|
public function database(): Connection
|
||||||
{
|
{
|
||||||
if (! isset($this->connection)) {
|
if (! isset($this->connection)) {
|
||||||
throw new NoConnectionSetException(static::class);
|
throw new NoConnectionSetException(static::class);
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,7 @@ test('database can be created after tenant creation', function () {
|
||||||
})->toListener());
|
})->toListener());
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
$manager = $tenant->database()->manager();
|
||||||
$manager = app(MySQLDatabaseManager::class);
|
|
||||||
$manager->setConnection('mysql');
|
|
||||||
|
|
||||||
expect($manager->databaseExists($tenant->database()->getName()))->toBeTrue();
|
expect($manager->databaseExists($tenant->database()->getName()))->toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Stancl\JobPipeline\JobPipeline;
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Database\Contracts\StatefulTenantDatabaseManager;
|
||||||
use Stancl\Tenancy\Database\DatabaseManager;
|
use Stancl\Tenancy\Database\DatabaseManager;
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
|
|
@ -36,7 +37,10 @@ test('databases can be created and deleted', function ($driver, $databaseManager
|
||||||
$name = 'db' . pest()->randomString();
|
$name = 'db' . pest()->randomString();
|
||||||
|
|
||||||
$manager = app($databaseManager);
|
$manager = app($databaseManager);
|
||||||
$manager->setConnection($driver);
|
|
||||||
|
if ($manager instanceof StatefulTenantDatabaseManager) {
|
||||||
|
$manager->setConnection($driver);
|
||||||
|
}
|
||||||
|
|
||||||
expect($manager->databaseExists($name))->toBeFalse();
|
expect($manager->databaseExists($name))->toBeFalse();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue