1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 09:34:04 +00:00

Get rid of tenant DB manager connection config

This commit is contained in:
Samuel Štancl 2020-05-21 14:47:29 +02:00
parent 2fedd5ce88
commit 15a7e52208
11 changed files with 58 additions and 41 deletions

View file

@ -152,16 +152,6 @@ return [
// 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class, // Separate by schema instead of database
],
/**
* Connections used by TenantDatabaseManagers. This tells, for example, the
* MySQLDatabaseManager to use the mysql connection to create databases.
*/
'database_manager_connections' => [
'sqlite' => 'sqlite',
'mysql' => 'mysql',
'pgsql' => 'pgsql',
],
/**
* Features are classes that provide additional functionality
* not needed for tenancy to be bootstrapped. They are run

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Contracts;
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
interface TenantDatabaseManager
{
/**
@ -35,6 +37,8 @@ interface TenantDatabaseManager
/**
* Set the DB connection that should be used by the tenant database manager.
*
* @throws NoConnectionSetException
*
* @param string $connection
* @return void

View file

@ -4,7 +4,7 @@ namespace Stancl\Tenancy\Exceptions;
use Exception;
class ModelNotSyncMaster extends Exception
class ModelNotSyncMasterException extends Exception
{
public function __construct(string $class)
{

View file

@ -0,0 +1,13 @@
<?php
namespace Stancl\Tenancy\Exceptions;
use Exception;
class NoConnectionSetException extends Exception
{
public function __construct($manager)
{
parent::__construct("No connection was set on this $manager instance.");
}
}

View file

@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Relations\Pivot;
use Stancl\Tenancy\Contracts\SyncMaster;
use Stancl\Tenancy\Events\SyncedResourceChangedInForeignDatabase;
use Stancl\Tenancy\Events\SyncedResourceSaved;
use Stancl\Tenancy\Exceptions\ModelNotSyncMaster;
use Stancl\Tenancy\Exceptions\ModelNotSyncMasterException;
class UpdateSyncedResource extends QueueableListener
{
@ -31,7 +31,7 @@ class UpdateSyncedResource extends QueueableListener
{
if (! $centralModel instanceof SyncMaster) {
// If we're trying to use a tenant User model instead of the central User model, for example.
throw new ModelNotSyncMaster(get_class($centralModel));
throw new ModelNotSyncMasterException(get_class($centralModel));
}
/** @var SyncMaster|Model $centralModel */

View file

@ -4,24 +4,23 @@ 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\TenantDatabaseManager;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
class MySQLDatabaseManager implements TenantDatabaseManager
{
/** @var string */
protected $connection;
public function __construct(Repository $config)
{
$this->connection = $config->get('tenancy.database_manager_connections.mysql');
}
protected function database(): Connection
{
if ($this->connection === null) {
throw new NoConnectionSetException(static::class);
}
return DB::connection($this->connection);
}

View file

@ -4,24 +4,23 @@ 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\TenantDatabaseManager;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
class PostgreSQLDatabaseManager implements TenantDatabaseManager
{
/** @var string */
protected $connection;
public function __construct(Repository $config)
{
$this->connection = $config->get('tenancy.database_manager_connections.pgsql');
}
protected function database(): Connection
{
if ($this->connection === null) {
throw new NoConnectionSetException(static::class);
}
return DB::connection($this->connection);
}

View file

@ -9,19 +9,19 @@ use Illuminate\Database\Connection;
use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
class PostgreSQLSchemaManager implements TenantDatabaseManager
{
/** @var string */
protected $connection;
public function __construct(Repository $config)
{
$this->connection = $config->get('tenancy.database_manager_connections.pgsql');
}
protected function database(): Connection
{
if ($this->connection === null) {
throw new NoConnectionSetException(static::class);
}
return DB::connection($this->connection);
}

View file

@ -29,7 +29,10 @@ class DatabasePreparationTest extends TestCase
$tenant = Tenant::create();
$this->assertTrue(app(MySQLDatabaseManager::class)->databaseExists($tenant->database()->getName()));
$manager = app(MySQLDatabaseManager::class);
$manager->setConnection('mysql');
$this->assertTrue($manager->databaseExists($tenant->database()->getName()));
}
/** @test */

View file

@ -22,7 +22,7 @@ use Stancl\Tenancy\Events\SyncedResourceSaved;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Exceptions\ModelNotSyncMaster;
use Stancl\Tenancy\Exceptions\ModelNotSyncMasterException;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Tests\Etc\Tenant;
@ -187,7 +187,7 @@ class ResourceSyncingTest extends TestCase
tenancy()->end();
$this->assertFalse(tenancy()->initialized);
$this->expectException(ModelNotSyncMaster::class);
$this->expectException(ModelNotSyncMasterException::class);
ResourceUser::first()->update(['role' => 'foobar']);
}

View file

@ -43,16 +43,19 @@ class TenantDatabaseManagerTest extends TestCase
$name = 'db' . $this->randomString();
$this->assertFalse(app($databaseManager)->databaseExists($name));
$manager = app($databaseManager);
$manager->setConnection($driver);
$this->assertFalse($manager->databaseExists($name));
$tenant = Tenant::create([
'tenancy_db_name' => $name,
'tenancy_db_connection' => $driver,
]);
$this->assertTrue(app($databaseManager)->databaseExists($name));
app($databaseManager)->deleteDatabase($tenant);
$this->assertFalse(app($databaseManager)->databaseExists($name));
$this->assertTrue($manager->databaseExists($name));
$manager->deleteDatabase($tenant);
$this->assertFalse($manager->databaseExists($name));
}
/** @test */
@ -68,23 +71,29 @@ class TenantDatabaseManagerTest extends TestCase
$database = 'db' . $this->randomString();
$this->assertFalse(app(MySQLDatabaseManager::class)->databaseExists($database));
$mysqlmanager = app(MySQLDatabaseManager::class);
$mysqlmanager->setConnection('mysql');
$this->assertFalse($mysqlmanager->databaseExists($database));
Tenant::create([
'tenancy_db_name' => $database,
'tenancy_db_connection' => 'mysql',
]);
$this->assertTrue(app(MySQLDatabaseManager::class)->databaseExists($database));
$this->assertTrue($mysqlmanager->databaseExists($database));
$postgresManager = app(PostgreSQLDatabaseManager::class);
$postgresManager->setConnection('pgsql');
$database = 'db' . $this->randomString();
$this->assertFalse(app(PostgreSQLDatabaseManager::class)->databaseExists($database));
$this->assertFalse($postgresManager->databaseExists($database));
Tenant::create([
'tenancy_db_name' => $database,
'tenancy_db_connection' => 'pgsql',
]);
$this->assertTrue(app(PostgreSQLDatabaseManager::class)->databaseExists($database));
$this->assertTrue($postgresManager->databaseExists($database));
}
public function database_manager_provider()