mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 03:34:04 +00:00
purge connection and add more tests
This commit is contained in:
parent
cd6e989c09
commit
52cce14797
3 changed files with 46 additions and 10 deletions
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Database;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase as Tenant;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase as Tenant;
|
||||||
|
|
@ -129,6 +130,9 @@ class DatabaseConfig
|
||||||
$templateConnection = config("database.connections.{$template}");
|
$templateConnection = config("database.connections.{$template}");
|
||||||
|
|
||||||
if ($this->manager() instanceof Contracts\ManagesDatabaseUsers) {
|
if ($this->manager() instanceof Contracts\ManagesDatabaseUsers) {
|
||||||
|
// We don't need username and password for database creation/deletion
|
||||||
|
// Username and password will be saved in Tenant's config
|
||||||
|
// and used for connecting to tenant Database
|
||||||
unset($config['username']);
|
unset($config['username']);
|
||||||
unset($config['password']);
|
unset($config['password']);
|
||||||
}
|
}
|
||||||
|
|
@ -140,6 +144,19 @@ class DatabaseConfig
|
||||||
return array_replace($templateConnection, $config);
|
return array_replace($templateConnection, $config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purge host database connection.
|
||||||
|
*/
|
||||||
|
public function purgeHostConnection(): void
|
||||||
|
{
|
||||||
|
$tenantHostConnectionName = $this->getTenantHostConnectionName();
|
||||||
|
if (array_key_exists($tenantHostConnectionName, config('database.connections'))) {
|
||||||
|
DB::purge($tenantHostConnectionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
config(["database.connections.{$tenantHostConnectionName}" => null]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Additional config for the database connection, specific to this tenant.
|
* Additional config for the database connection, specific to this tenant.
|
||||||
*/
|
*/
|
||||||
|
|
@ -188,10 +205,12 @@ class DatabaseConfig
|
||||||
/** Get the TenantDatabaseManager for this tenant's connection. */
|
/** Get the TenantDatabaseManager for this tenant's connection. */
|
||||||
public function hostManager(): Contracts\TenantDatabaseManager
|
public function hostManager(): Contracts\TenantDatabaseManager
|
||||||
{
|
{
|
||||||
$tenantHostConnection = $this->getTenantHostConnectionName();
|
$this->purgeHostConnection();
|
||||||
config(["database.connections.{$tenantHostConnection}" => $this->hostConnection()]);
|
|
||||||
|
|
||||||
$driver = config("database.connections.{$tenantHostConnection}.driver");
|
$tenantHostConnectionName = $this->getTenantHostConnectionName();
|
||||||
|
config(["database.connections.{$tenantHostConnectionName}" => $this->hostConnection()]);
|
||||||
|
|
||||||
|
$driver = config("database.connections.{$tenantHostConnectionName}.driver");
|
||||||
$databaseManagers = config('tenancy.database.managers');
|
$databaseManagers = config('tenancy.database.managers');
|
||||||
|
|
||||||
if (! array_key_exists($driver, $databaseManagers)) {
|
if (! array_key_exists($driver, $databaseManagers)) {
|
||||||
|
|
@ -201,7 +220,7 @@ class DatabaseConfig
|
||||||
/** @var Contracts\TenantDatabaseManager $databaseManager */
|
/** @var Contracts\TenantDatabaseManager $databaseManager */
|
||||||
$databaseManager = app($databaseManagers[$driver]);
|
$databaseManager = app($databaseManagers[$driver]);
|
||||||
|
|
||||||
$databaseManager->setConnection($tenantHostConnection);
|
$databaseManager->setConnection($tenantHostConnectionName);
|
||||||
|
|
||||||
return $databaseManager;
|
return $databaseManager;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,6 @@ abstract class TenantDatabaseManager implements Contract // todo better naming?
|
||||||
throw new NoConnectionSetException(static::class);
|
throw new NoConnectionSetException(static::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config("database.connections.{$this->getTenantHostConnectionName()}")) {
|
|
||||||
// DB::purge($this->getTenantHostConnectionName());
|
|
||||||
}
|
|
||||||
|
|
||||||
return DB::connection($this->connection);
|
return DB::connection($this->connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -100,8 +100,6 @@ test('the tenant connection is fully removed', function () {
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
// Connections array can contain other connections built runtime like 'tenant_host_connection'
|
|
||||||
// So check if tenant connection does not exist in connections
|
|
||||||
expect(array_keys(app('db')->getConnections()))->not()->toContain('tenant');
|
expect(array_keys(app('db')->getConnections()))->not()->toContain('tenant');
|
||||||
pest()->assertArrayNotHasKey('tenant', config('database.connections'));
|
pest()->assertArrayNotHasKey('tenant', config('database.connections'));
|
||||||
|
|
||||||
|
|
@ -229,6 +227,29 @@ test('tenant database can be created and deleted on a foreign server', function
|
||||||
expect($manager->databaseExists($name))->toBeFalse();
|
expect($manager->databaseExists($name))->toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('using permission controller MySQL manager creates the database user', function () {
|
||||||
|
config([
|
||||||
|
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$name = 'foo' . Str::random(8);
|
||||||
|
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'tenancy_db_name' => $name,
|
||||||
|
]);
|
||||||
|
$dbUser = $tenant->tenancy_db_username;
|
||||||
|
|
||||||
|
/** @var PermissionControlledMySQLDatabaseManager $manager */
|
||||||
|
$manager = $tenant->database()->manager();
|
||||||
|
$manager->setConnection('mysql');
|
||||||
|
|
||||||
|
expect($manager->userExists($dbUser))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
test('tenant database can be created on template tenant connection', function () {
|
test('tenant database can be created on template tenant connection', function () {
|
||||||
config([
|
config([
|
||||||
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue