Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Bootstrappers/DatabaseTenancyBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function bootstrap(Tenant $tenant): void
{
/** @var TenantWithDatabase $tenant */
if (data_get($tenant->database()->getTemplateConnection(), 'url')) {
// The package works with individual parts of the database connection config, so DATABASE_URL is not supported.
// When DATABASE_URL is set, this bootstrapper can silently fail i.e. keep using the template connection's database URL
// The package works with individual parts of the database connection config, so DB_URL is not supported.
// When DB_URL is set, this bootstrapper can silently fail i.e. keep using the template connection's database URL
// which takes precedence over individual segments of the connection config. This issue can be hard to debug as it can be
// production-specific. Therefore, we throw an exception (that effectively blocks all tenant pages) to prevent incorrect DB use.
throw new Exception('The template connection must NOT have URL defined. Specify the connection using individual parts instead of a database URL.');
Expand Down
24 changes: 11 additions & 13 deletions tests/Bootstrappers/DatabaseTenancyBootstrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use Stancl\Tenancy\Database\TenantDatabaseManagers\MySQLDatabaseManager;
use Stancl\Tenancy\Database\TenantDatabaseManagers\SQLiteDatabaseManager;
use Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLDatabaseManager;
Expand Down Expand Up @@ -116,7 +115,9 @@
expect(fn () => tenancy()->initialize($tenant))->toThrow(RuntimeException::class);

// Connection should be reverted back to central
expect(DB::connection()->getName())->toBe('central');
$centralConnection = config('tenancy.database.central_connection');

expect(DB::connection()->getName())->toBe($centralConnection);
} else {
expect(fn() => tenancy()->initialize($tenant))->not()->toThrow(Throwable::class);

Expand All @@ -128,25 +129,22 @@
'hardening disabled' => false,
])->with('db_managers');

test('database tenancy bootstrapper throws an exception if DATABASE_URL is set', function (string|null $databaseUrl) {
config(['database.connections.central.url' => $databaseUrl]);

test('database tenancy bootstrapper throws an exception if DB_URL is set', function (string|null $databaseUrl) {
config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);

Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());

if ($databaseUrl) {
expect(fn() => Tenant::create())->toThrow(QueryException::class);
} else {
expect(function() {
$tenant1 = Tenant::create();
$tenant = Tenant::create();

pest()->artisan('tenants:migrate');
config(['database.connections.central.url' => $databaseUrl]);

tenancy()->initialize($tenant1);
})->not()->toThrow(Throwable::class);
if ($databaseUrl) {
expect(fn() => tenancy()->initialize($tenant))
->toThrow(Exception::class, 'The template connection must NOT have URL defined.');
} else {
expect(fn() => tenancy()->initialize($tenant))->not()->toThrow(Throwable::class);
}
})->with(['abc.us-east-1.rds.amazonaws.com', null]);

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function getEnvironmentSetUp($app)
'cache.stores.apc' => ['driver' => 'apc'],
'database.connections.central' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'url' => env('DB_URL'),
Comment thread
lukinovec marked this conversation as resolved.
'host' => 'mysql',
'port' => env('DB_PORT', '3306'),
'database' => 'main',
Expand Down
Loading