mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 09:34:04 +00:00
[3.x] DB users (#382)
* Initial draft * Apply fixes from StyleCI * Use CI on master branch too * Pass correct argument to queued DB creators/deleters * Apply fixes from StyleCI * Remove new interface from MySQLDBManager * Make phpunit run * Apply fixes from StyleCI * Fix static property * Default databaseName * Use database transactions for creating users & granting permissions * Apply fixes from StyleCI * Get old tests to pass * Apply fixes from StyleCI * Add tests for PermissionControlledMySQLDatabaseManager * Apply fixes from StyleCI * Write test for extra config, fix bug with extra config * Apply fixes from StyleCI
This commit is contained in:
parent
60665517a0
commit
3bb2759fe2
41 changed files with 756 additions and 286 deletions
|
|
@ -9,6 +9,7 @@ use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
|
|||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
|
||||
use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager;
|
||||
use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager;
|
||||
use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager;
|
||||
use Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager;
|
||||
|
|
@ -27,25 +28,46 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
$this->markTestSkipped('As to not bloat your computer with test databases, this test is not run by default.');
|
||||
}
|
||||
|
||||
config()->set([
|
||||
"tenancy.database_managers.$driver" => $databaseManager,
|
||||
]);
|
||||
|
||||
$name = 'db' . $this->randomString();
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => $name,
|
||||
'_tenancy_db_connection' => $driver,
|
||||
]);
|
||||
|
||||
$this->assertFalse(app($databaseManager)->databaseExists($name));
|
||||
app($databaseManager)->createDatabase($name);
|
||||
$tenant->save(); // generate credentials & create DB
|
||||
$this->assertTrue(app($databaseManager)->databaseExists($name));
|
||||
app($databaseManager)->deleteDatabase($name);
|
||||
app($databaseManager)->deleteDatabase($tenant);
|
||||
$this->assertFalse(app($databaseManager)->databaseExists($name));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function dbs_can_be_created_when_another_driver_is_used_for_the_central_db()
|
||||
{
|
||||
$this->assertSame('sqlite', config('database.default'));
|
||||
$this->assertSame('central', config('database.default'));
|
||||
|
||||
$database = 'db' . $this->randomString();
|
||||
app(MySQLDatabaseManager::class)->createDatabase($database);
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => $database,
|
||||
'_tenancy_db_connection' => 'mysql',
|
||||
]);
|
||||
|
||||
$this->assertFalse(app(MySQLDatabaseManager::class)->databaseExists($database));
|
||||
$tenant->save(); // create DB
|
||||
$this->assertTrue(app(MySQLDatabaseManager::class)->databaseExists($database));
|
||||
|
||||
$database = 'db2' . $this->randomString();
|
||||
app(PostgreSQLDatabaseManager::class)->createDatabase($database);
|
||||
$database = 'db' . $this->randomString();
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => $database,
|
||||
'_tenancy_db_connection' => 'pgsql',
|
||||
]);
|
||||
|
||||
$this->assertFalse(app(PostgreSQLDatabaseManager::class)->databaseExists($database));
|
||||
$tenant->save(); // create DB
|
||||
$this->assertTrue(app(PostgreSQLDatabaseManager::class)->databaseExists($database));
|
||||
}
|
||||
|
||||
|
|
@ -59,16 +81,25 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
$this->markTestSkipped('As to not bloat your computer with test databases, this test is not run by default.');
|
||||
}
|
||||
|
||||
config()->set('database.default', $driver);
|
||||
config()->set([
|
||||
'database.default' => $driver,
|
||||
"tenancy.database_managers.$driver" => $databaseManager,
|
||||
]);
|
||||
|
||||
$name = 'db' . $this->randomString();
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => $name,
|
||||
'_tenancy_db_connection' => $driver,
|
||||
]);
|
||||
$tenant->database()->makeCredentials();
|
||||
|
||||
$this->assertFalse(app($databaseManager)->databaseExists($name));
|
||||
$job = new QueuedTenantDatabaseCreator(app($databaseManager), $name);
|
||||
$job = new QueuedTenantDatabaseCreator(app($databaseManager), $tenant);
|
||||
|
||||
$job->handle();
|
||||
$this->assertTrue(app($databaseManager)->databaseExists($name));
|
||||
|
||||
$job = new QueuedTenantDatabaseDeleter(app($databaseManager), $name);
|
||||
$job = new QueuedTenantDatabaseDeleter(app($databaseManager), $tenant);
|
||||
$job->handle();
|
||||
$this->assertFalse(app($databaseManager)->databaseExists($name));
|
||||
}
|
||||
|
|
@ -77,6 +108,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
{
|
||||
return [
|
||||
['mysql', MySQLDatabaseManager::class],
|
||||
['mysql', PermissionControlledMySQLDatabaseManager::class],
|
||||
['sqlite', SQLiteDatabaseManager::class],
|
||||
['pgsql', PostgreSQLDatabaseManager::class],
|
||||
['pgsql', PostgreSQLSchemaManager::class],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue