mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:04:04 +00:00
* fix #1112 - throw an exception when DATABASE_URL is defined, minor test changes * Fix code style (php-cs-fixer) * fix typo --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
parent
af3812e788
commit
2d500f9780
5 changed files with 33 additions and 1 deletions
|
|
@ -40,6 +40,8 @@ services:
|
||||||
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
|
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
retries: 10
|
retries: 10
|
||||||
|
tmpfs:
|
||||||
|
- /var/lib/mysql
|
||||||
mysql2:
|
mysql2:
|
||||||
image: mysql:5.7
|
image: mysql:5.7
|
||||||
environment:
|
environment:
|
||||||
|
|
@ -51,6 +53,8 @@ services:
|
||||||
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
|
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
retries: 10
|
retries: 10
|
||||||
|
tmpfs:
|
||||||
|
- /var/lib/mysql
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:11
|
image: postgres:11
|
||||||
environment:
|
environment:
|
||||||
|
|
@ -62,6 +66,8 @@ services:
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
tmpfs:
|
||||||
|
- /var/lib/postgresql/data
|
||||||
redis:
|
redis:
|
||||||
image: redis:alpine
|
image: redis:alpine
|
||||||
healthcheck:
|
healthcheck:
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Bootstrappers;
|
namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
|
|
@ -23,6 +24,13 @@ class DatabaseTenancyBootstrapper implements TenancyBootstrapper
|
||||||
public function bootstrap(Tenant $tenant): void
|
public function bootstrap(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
/** @var TenantWithDatabase $tenant */
|
/** @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
|
||||||
|
// 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.');
|
||||||
|
}
|
||||||
|
|
||||||
// Better debugging, but breaks cached lookup in prod
|
// Better debugging, but breaks cached lookup in prod
|
||||||
if (app()->environment('local') || app()->environment('testing')) { // todo@docs mention this change in v4 upgrade guide https://github.com/archtechx/tenancy/pull/945#issuecomment-1268206149
|
if (app()->environment('local') || app()->environment('testing')) { // todo@docs mention this change in v4 upgrade guide https://github.com/archtechx/tenancy/pull/945#issuecomment-1268206149
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\Database;
|
namespace Stancl\Tenancy\Database;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Database;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
|
||||||
|
|
@ -631,6 +631,24 @@ test('fortify route tenancy bootstrapper updates fortify config correctly', func
|
||||||
expect(config('fortify.redirects'))->toBe($originalFortifyRedirects);
|
expect(config('fortify.redirects'))->toBe($originalFortifyRedirects);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('database tenancy bootstrapper throws an exception if DATABASE_URL is set', function (string|null $databaseUrl) {
|
||||||
|
if ($databaseUrl) {
|
||||||
|
config(['database.connections.central.url' => $databaseUrl]);
|
||||||
|
|
||||||
|
pest()->expectException(Exception::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);
|
||||||
|
|
||||||
|
$tenant1 = Tenant::create();
|
||||||
|
|
||||||
|
pest()->artisan('tenants:migrate');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
expect(true)->toBe(true);
|
||||||
|
})->with(['abc.us-east-1.rds.amazonaws.com', null]);
|
||||||
|
|
||||||
function getDiskPrefix(string $disk): string
|
function getDiskPrefix(string $disk): string
|
||||||
{
|
{
|
||||||
/** @var FilesystemAdapter $disk */
|
/** @var FilesystemAdapter $disk */
|
||||||
|
|
|
||||||
|
|
@ -159,6 +159,7 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
|
||||||
});
|
});
|
||||||
})->with([true, false]);
|
})->with([true, false]);
|
||||||
|
|
||||||
|
// todo0 this test appears to be affected by race conditions/similar
|
||||||
test('the tenant used by the job doesnt change when the current tenant changes', function () {
|
test('the tenant used by the job doesnt change when the current tenant changes', function () {
|
||||||
withTenantDatabases();
|
withTenantDatabases();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue