1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-06-21 03:04:04 +00:00

Use simple tenants table check during hardening

For a niche opt-in feature, checking if the tenants table is in the current schema is good enough for determining if the current DB is central -- the solution where we added getCurrentDatabaseName to each manager was overly complicated for this (though a bit more correct, not worth the added complexity).
This commit is contained in:
lukinovec 2026-06-12 15:34:39 +02:00
parent ac54c4f65b
commit 0d177c7e9b
6 changed files with 24 additions and 45 deletions

View file

@ -39,10 +39,17 @@ test('harden prevents tenants from using the central database', function (bool $
"tenancy.database.managers.{$connection}" => $manager,
]);
// Set up and migrate the central database
// Point the central connection at the tested connection's config and migrate it
// (so that the central database/schema contains the tenants table).
$centralConnection = config('tenancy.database.central_connection');
$centralConfig = config("database.connections.{$connection}");
if ($connection === 'sqlite') {
$centralConfig['database'] = database_path($sqliteCentralDb = 'central.sqlite');
}
DB::purge($centralConnection);
config(["database.connections.{$centralConnection}" => config("database.connections.{$connection}")]);
config(["database.connections.{$centralConnection}" => $centralConfig]);
pest()->artisan('migrate:fresh', [
'--force' => true,
@ -56,11 +63,18 @@ test('harden prevents tenants from using the central database', function (bool $
return $event->tenant;
})->toListener());
// Create the tenant with its own database, then repoint it at the central database/schema.
// Create the tenant with its own database, then repoint it at the central database/schema
// (which contains the tenants table that the hardening check looks for).
$tenant = Tenant::create(['tenancy_db_connection' => $connection]);
$tenant->update([
'tenancy_db_name' => $tenant->database()->manager()->getCurrentDatabaseName(DB::connection($centralConnection)),
]);
$central = DB::connection($centralConnection);
$centralName = match (true) {
$manager === PostgreSQLSchemaManager::class => $central->selectOne('SELECT current_schema() AS schema')->schema, // Central schema name
$connection === 'sqlite' => $sqliteCentralDb, // Central SQLite DB name
default => $central->getDatabaseName(), // Central DB name
};
$tenant->update(['tenancy_db_name' => $centralName]);
if ($harden) {
// Harden blocks initialization for tenants that use the central database