mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 20:34:03 +00:00
always register MigrateFreshOverride
This commit is contained in:
parent
912f9a964d
commit
607ea723e5
3 changed files with 11 additions and 43 deletions
|
|
@ -10,7 +10,9 @@ class MigrateFreshOverride extends FreshCommand
|
||||||
{
|
{
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
tenancy()->model()::all()->each->delete();
|
if (config('tenancy.database.drop_tenant_databases_on_migrate_fresh')) {
|
||||||
|
tenancy()->model()::cursor()->each->delete();
|
||||||
|
}
|
||||||
|
|
||||||
return parent::handle();
|
return parent::handle();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -91,12 +91,8 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Commands\Up::class,
|
Commands\Up::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->app->extend(FreshCommand::class, function (FreshCommand $originalCommand) {
|
$this->app->extend(FreshCommand::class, function () {
|
||||||
if ($this->app['config']['tenancy.database.drop_tenant_databases_on_migrate_fresh']) {
|
return new Commands\MigrateFreshOverride;
|
||||||
return new Commands\MigrateFreshOverride;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $originalCommand;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
|
|
|
||||||
|
|
@ -18,16 +18,11 @@ use Stancl\Tenancy\Events\TenantCreated;
|
||||||
use Stancl\Tenancy\Events\TenantDeleted;
|
use Stancl\Tenancy\Events\TenantDeleted;
|
||||||
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
||||||
use Stancl\Tenancy\Events\DeletingTenant;
|
use Stancl\Tenancy\Events\DeletingTenant;
|
||||||
use Stancl\Tenancy\TenancyServiceProvider;
|
|
||||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Illuminate\Console\ContainerCommandLoader;
|
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Illuminate\Database\Console\Migrations\FreshCommand;
|
|
||||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
|
|
||||||
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
|
@ -276,9 +271,8 @@ test('run command works when sub command asks questions and accepts arguments',
|
||||||
expect($user->email)->toBe('email@localhost');
|
expect($user->email)->toBe('email@localhost');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('migrate fresh command only deletes tenant databases if drop_tenant_databases_on_migrate_fresh is true', function() {
|
test('migrate fresh command only deletes tenant databases if drop_tenant_databases_on_migrate_fresh is true', function (bool $dropTenantDBsOnMigrateFresh) {
|
||||||
Event::listen(
|
Event::listen(DeletingTenant::class,
|
||||||
DeletingTenant::class,
|
|
||||||
JobPipeline::make([DeleteDomains::class])->send(function (DeletingTenant $event) {
|
JobPipeline::make([DeleteDomains::class])->send(function (DeletingTenant $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->shouldBeQueued(false)->toListener()
|
})->shouldBeQueued(false)->toListener()
|
||||||
|
|
@ -291,9 +285,8 @@ test('migrate fresh command only deletes tenant databases if drop_tenant_databas
|
||||||
})->shouldBeQueued(false)->toListener()
|
})->shouldBeQueued(false)->toListener()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
config(['tenancy.database.drop_tenant_databases_on_migrate_fresh' => $dropTenantDBsOnMigrateFresh]);
|
||||||
config(['tenancy.database.drop_tenant_databases_on_migrate_fresh' => false]);
|
$shouldHaveDBAfterMigrateFresh = ! $dropTenantDBsOnMigrateFresh;
|
||||||
app()->forgetInstance(FreshCommand::class);
|
|
||||||
|
|
||||||
/** @var Tenant[] $tenants */
|
/** @var Tenant[] $tenants */
|
||||||
$tenants = [
|
$tenants = [
|
||||||
|
|
@ -315,32 +308,9 @@ test('migrate fresh command only deletes tenant databases if drop_tenant_databas
|
||||||
]);
|
]);
|
||||||
|
|
||||||
foreach ($tenants as $tenant) {
|
foreach ($tenants as $tenant) {
|
||||||
expect($tenantHasDatabase($tenant))->toBeTrue();
|
expect($tenantHasDatabase($tenant))->toBe($shouldHaveDBAfterMigrateFresh);
|
||||||
}
|
}
|
||||||
|
})->with([true, false]);
|
||||||
config(['tenancy.database.drop_tenant_databases_on_migrate_fresh' => true]);
|
|
||||||
app()->forgetInstance(FreshCommand::class);
|
|
||||||
|
|
||||||
$tenants = [
|
|
||||||
Tenant::create(),
|
|
||||||
Tenant::create(),
|
|
||||||
Tenant::create(),
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($tenants as $tenant) {
|
|
||||||
expect($tenantHasDatabase($tenant))->toBeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
pest()->artisan('migrate:fresh', [
|
|
||||||
'--force' => true,
|
|
||||||
'--path' => __DIR__ . '/../assets/migrations',
|
|
||||||
'--realpath' => true,
|
|
||||||
]);
|
|
||||||
|
|
||||||
foreach ($tenants as $tenant) {
|
|
||||||
expect($tenantHasDatabase($tenant))->toBeFalse();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// todo@tests
|
// todo@tests
|
||||||
function runCommandWorks(): void
|
function runCommandWorks(): void
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue