Skip to content

Commit 0d177c7

Browse files
committed
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).
1 parent ac54c4f commit 0d177c7

6 files changed

Lines changed: 24 additions & 45 deletions

File tree

src/Bootstrappers/DatabaseTenancyBootstrapper.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Stancl\Tenancy\Bootstrappers;
66

77
use Exception;
8-
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Support\Facades\Schema;
99
use RuntimeException;
1010
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
1111
use Stancl\Tenancy\Contracts\Tenant;
@@ -90,8 +90,7 @@ public function revert(): void
9090
protected function verifyTenantCanUseDatabase(Tenant $tenant): void
9191
{
9292
/** @var \Stancl\Tenancy\Database\Models\Tenant&TenantWithDatabase $tenant */
93-
$tenantDbConfig = $tenant->database();
94-
$tenantDbName = $tenantDbConfig->getName();
93+
$tenantDbName = $tenant->database()->getName();
9594

9695
// Check that no other tenant uses this tenant's database
9796
if ($tenant::where($tenant->getTenantKeyName(), '!=', $tenant->getTenantKey())
@@ -100,14 +99,8 @@ protected function verifyTenantCanUseDatabase(Tenant $tenant): void
10099
throw new RuntimeException('Tenant cannot use a database of another tenant.');
101100
}
102101

103-
$manager = $tenantDbConfig->manager();
104-
105-
$centralConnection = DB::connection(config('tenancy.database.central_connection', 'central'));
106-
$currentConnection = DB::connection();
107-
108-
// Throw if the current database/schema is central.
109-
// At this point the connection should be the tenant's, so it should not match central.
110-
if ($manager->getCurrentDatabaseName($currentConnection) === $manager->getCurrentDatabaseName($centralConnection)) {
102+
if (Schema::hasTable($tenant->getTable())) {
103+
// Throw if the current database/schema has the tenants table (i.e. it's not central)
111104
throw new RuntimeException('Tenant cannot use the central database.');
112105
}
113106
}

src/Database/Contracts/TenantDatabaseManager.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Stancl\Tenancy\Database\Contracts;
66

7-
use Illuminate\Database\Connection;
8-
97
interface TenantDatabaseManager
108
{
119
/** Create a database. */
@@ -19,12 +17,4 @@ public function databaseExists(string $name): bool;
1917

2018
/** Construct a DB connection config array. */
2119
public function makeConnectionConfig(array $baseConfig, string $databaseName): array;
22-
23-
/**
24-
* Get the schema/database name that the given connection points to.
25-
*
26-
* In database managers, this should return the *database* name of the passed connection,
27-
* while in schema managers, this should return the *schema* name of the passed connection.
28-
*/
29-
public function getCurrentDatabaseName(Connection $connection): string;
3020
}

src/Database/TenantDatabaseManagers/PostgreSQLSchemaManager.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
66

7-
use Illuminate\Database\Connection;
87
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
98

109
class PostgreSQLSchemaManager extends TenantDatabaseManager
@@ -38,10 +37,4 @@ public function makeConnectionConfig(array $baseConfig, string $databaseName): a
3837

3938
return $baseConfig;
4039
}
41-
42-
public function getCurrentDatabaseName(Connection $connection): string
43-
{
44-
// Get the *schema* name (not the database name)
45-
return $connection->selectOne('SELECT current_schema() AS schema')->schema;
46-
}
4740
}

src/Database/TenantDatabaseManagers/SQLiteDatabaseManager.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
66

77
use Closure;
8-
use Illuminate\Database\Connection;
98
use Illuminate\Database\Eloquent\Model;
109
use InvalidArgumentException;
1110
use PDO;
@@ -150,11 +149,6 @@ public function makeConnectionConfig(array $baseConfig, string $databaseName): a
150149
return $baseConfig;
151150
}
152151

153-
public function getCurrentDatabaseName(Connection $connection): string
154-
{
155-
return $connection->getDatabaseName();
156-
}
157-
158152
public function getPath(string $name): string
159153
{
160154
$this->validateDatabaseName($name);

src/Database/TenantDatabaseManagers/TenantDatabaseManager.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,4 @@ public function makeConnectionConfig(array $baseConfig, string $databaseName): a
3737

3838
return $baseConfig;
3939
}
40-
41-
public function getCurrentDatabaseName(Connection $connection): string
42-
{
43-
return $connection->getDatabaseName();
44-
}
4540
}

tests/Bootstrappers/DatabaseTenancyBootstrapperTest.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@
3939
"tenancy.database.managers.{$connection}" => $manager,
4040
]);
4141

42-
// Set up and migrate the central database
42+
// Point the central connection at the tested connection's config and migrate it
43+
// (so that the central database/schema contains the tenants table).
4344
$centralConnection = config('tenancy.database.central_connection');
45+
$centralConfig = config("database.connections.{$connection}");
46+
47+
if ($connection === 'sqlite') {
48+
$centralConfig['database'] = database_path($sqliteCentralDb = 'central.sqlite');
49+
}
50+
4451
DB::purge($centralConnection);
45-
config(["database.connections.{$centralConnection}" => config("database.connections.{$connection}")]);
52+
config(["database.connections.{$centralConnection}" => $centralConfig]);
4653

4754
pest()->artisan('migrate:fresh', [
4855
'--force' => true,
@@ -56,11 +63,18 @@
5663
return $event->tenant;
5764
})->toListener());
5865

59-
// Create the tenant with its own database, then repoint it at the central database/schema.
66+
// Create the tenant with its own database, then repoint it at the central database/schema
67+
// (which contains the tenants table that the hardening check looks for).
6068
$tenant = Tenant::create(['tenancy_db_connection' => $connection]);
61-
$tenant->update([
62-
'tenancy_db_name' => $tenant->database()->manager()->getCurrentDatabaseName(DB::connection($centralConnection)),
63-
]);
69+
70+
$central = DB::connection($centralConnection);
71+
$centralName = match (true) {
72+
$manager === PostgreSQLSchemaManager::class => $central->selectOne('SELECT current_schema() AS schema')->schema, // Central schema name
73+
$connection === 'sqlite' => $sqliteCentralDb, // Central SQLite DB name
74+
default => $central->getDatabaseName(), // Central DB name
75+
};
76+
77+
$tenant->update(['tenancy_db_name' => $centralName]);
6478

6579
if ($harden) {
6680
// Harden blocks initialization for tenants that use the central database

0 commit comments

Comments
 (0)