From 08057f8effe13999a7192d8ccb4ff07a872374a7 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 12 Sep 2022 16:24:24 +0200 Subject: [PATCH] Add and test Migrate command's skip-failing option --- src/Commands/Migrate.php | 13 +++++++++++-- tests/CommandsTest.php | 33 +++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index 52ecd47f..c0fc4997 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Commands; +use Exception; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Console\Migrations\MigrateCommand; use Illuminate\Database\Migrations\Migrator; @@ -28,6 +29,8 @@ class Migrate extends MigrateCommand { parent::__construct($migrator, $dispatcher); + $this->addOption('skip-failing'); + $this->specifyParameters(); } @@ -51,8 +54,14 @@ class Migrate extends MigrateCommand event(new MigratingDatabase($tenant)); - // Migrate - parent::handle(); + try { + // Migrate + parent::handle(); + } catch (Exception $th) { + if (! $this->option('skip-failing')) { + throw $th; + } + } event(new DatabaseMigrated($tenant)); }); diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 19018c9a..ea8ad908 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -2,21 +2,21 @@ declare(strict_types=1); -use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; +use Stancl\Tenancy\Tests\Etc\User; +use Stancl\JobPipeline\JobPipeline; +use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Schema; -use Stancl\JobPipeline\JobPipeline; -use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; +use Illuminate\Support\Facades\Artisan; use Stancl\Tenancy\Events\TenancyEnded; -use Stancl\Tenancy\Events\TenancyInitialized; -use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Jobs\CreateDatabase; +use Stancl\Tenancy\Events\TenantCreated; +use Stancl\Tenancy\Tests\Etc\ExampleSeeder; +use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; -use Stancl\Tenancy\Tests\Etc\ExampleSeeder; -use Stancl\Tenancy\Tests\Etc\Tenant; -use Stancl\Tenancy\Tests\Etc\User; +use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; beforeEach(function () { Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { @@ -94,6 +94,23 @@ test('migrate command loads schema state', function () { expect(Schema::hasTable('users'))->toBeTrue(); }); +test('migrate command doesnt throw error if skip-failing is passed', function() { + Tenant::create(); + + Event::forget(TenantCreated::class); + + Tenant::create(['id' => 'withoutdb']); + + Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { + return $event->tenant; + })->toListener()); + + Tenant::create(); + + expect(fn() => pest()->artisan('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump"'))->toThrow(Exception::class); + expect(fn() => pest()->artisan('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump" --skip-failing'))->not()->toThrow(Exception::class); +})->group('migrate'); + test('dump command works', function () { $tenant = Tenant::create(); Artisan::call('tenants:migrate');