|
27 | 27 | use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
28 | 28 | use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
|
29 | 29 | use function Stancl\Tenancy\Tests\pest;
|
| 30 | +use Stancl\Tenancy\Events\MigratingDatabase; |
30 | 31 |
|
31 | 32 | beforeEach(function () {
|
32 | 33 | if (file_exists($schemaPath = 'tests/Etc/tenant-schema-test.dump')) {
|
|
95 | 96 | expect(Schema::hasTable('users'))->toBeTrue();
|
96 | 97 | });
|
97 | 98 |
|
| 99 | +test('migrate command uses the passed database option as the template tenant connection', function () { |
| 100 | + $originalTemplateConnection = config('tenancy.database.template_tenant_connection'); |
| 101 | + |
| 102 | + // Add a custom connection that will be used as the template for the tenant connection |
| 103 | + // Identical to the default (mysql), just with different charset and collation |
| 104 | + config(['database.connections.custom_connection' => [ |
| 105 | + "driver" => "mysql", |
| 106 | + "url" => "", |
| 107 | + "host" => "mysql", |
| 108 | + "port" => "3306", |
| 109 | + "database" => "main", |
| 110 | + "username" => "root", |
| 111 | + "password" => "password", |
| 112 | + "unix_socket" => "", |
| 113 | + "charset" => "latin1", // Different from the default (utf8mb4) |
| 114 | + "collation" => "latin1_swedish_ci", // Different from the default (utf8mb4_unicode_ci) |
| 115 | + "prefix" => "", |
| 116 | + "prefix_indexes" => true, |
| 117 | + "strict" => true, |
| 118 | + "engine" => null, |
| 119 | + "options" => [] |
| 120 | + ]]); |
| 121 | + |
| 122 | + $templateConnectionDuringMigration = null; |
| 123 | + $tenantConnectionDuringMigration = null; |
| 124 | + |
| 125 | + Event::listen(MigratingDatabase::class, function() use (&$templateConnectionDuringMigration, &$tenantConnectionDuringMigration) { |
| 126 | + $templateConnectionDuringMigration = config('tenancy.database.template_tenant_connection'); |
| 127 | + $tenantConnectionDuringMigration = DB::connection('tenant')->getConfig(); |
| 128 | + }); |
| 129 | + |
| 130 | + // The original tenant template connection config remains default |
| 131 | + expect(config('tenancy.database.template_tenant_connection'))->toBe($originalTemplateConnection); |
| 132 | + |
| 133 | + Tenant::create(); |
| 134 | + |
| 135 | + // The original template connection is used when the --database option is not passed |
| 136 | + pest()->artisan('tenants:migrate'); |
| 137 | + expect($templateConnectionDuringMigration)->toBe($originalTemplateConnection); |
| 138 | + |
| 139 | + Tenant::create(); |
| 140 | + |
| 141 | + // The migrate command temporarily uses the connection passed in the --database option |
| 142 | + pest()->artisan('tenants:migrate', ['--database' => 'custom_connection']); |
| 143 | + expect($templateConnectionDuringMigration)->toBe('custom_connection'); |
| 144 | + |
| 145 | + // The tenant connection during migration actually used custom_connection's config |
| 146 | + expect($tenantConnectionDuringMigration['charset'])->toBe('latin1'); |
| 147 | + expect($tenantConnectionDuringMigration['collation'])->toBe('latin1_swedish_ci'); |
| 148 | + |
| 149 | + // The tenant template connection config is restored to the original after migrating |
| 150 | + expect(config('tenancy.database.template_tenant_connection'))->toBe($originalTemplateConnection); |
| 151 | +}); |
| 152 | + |
98 | 153 | test('migrate command only throws exceptions if skip-failing is not passed', function() {
|
99 | 154 | Tenant::create();
|
100 | 155 |
|
|
311 | 366 | expect(DB::table('users')->exists())->toBeFalse();
|
312 | 367 | });
|
313 | 368 |
|
| 369 | +test('migrate fresh command respects force option in production', function () { |
| 370 | + // Set environment to production |
| 371 | + app()->detectEnvironment(fn() => 'production'); |
| 372 | + |
| 373 | + Tenant::create(); |
| 374 | + |
| 375 | + // Without --force in production, command should prompt for confirmation |
| 376 | + pest()->artisan('tenants:migrate-fresh') |
| 377 | + ->expectsConfirmation('Are you sure you want to run this command?'); |
| 378 | + |
| 379 | + // With --force, command should succeed without prompting |
| 380 | + pest()->artisan('tenants:migrate-fresh', ['--force' => true]) |
| 381 | + ->assertSuccessful(); |
| 382 | +}); |
| 383 | + |
314 | 384 | test('run command with array of tenants works', function () {
|
315 | 385 | $tenantId1 = Tenant::create()->getTenantKey();
|
316 | 386 | $tenantId2 = Tenant::create()->getTenantKey();
|
|
0 commit comments