1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 05:44:04 +00:00

Add skip-failing option to the Migrate command (#945)

* Add and test Migrate command's skip-failing option

* Improve naming

* Move migration event dispatching inside try block

* Change test name

* Fix skip-failing test

* Use QueryException instead of Exception

* Correct TenantDatabaseDoesNotExistException import

* Correct test

* Check for the the testing env in DB bootstrapper

* Correct the Migrate command

* Fix code style (php-cs-fixer)

* add docs todo

* Add QueryException to the Migrat command try/catch

* Return status codes in Migrate

* Fix code style (php-cs-fixer)

* Add test for not stopping tenants:migrate after the first failure

* Update Migrate command

* Fix code style (php-cs-fixer)

* Fix code style (php-cs-fixer)

* Use `getTenants()`

* Use withtenantDatabases where needed

* Add withTenantDatabases to test

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
lukinovec 2023-02-01 06:55:26 +01:00 committed by GitHub
parent f741f44527
commit 342c67fe02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 95 additions and 20 deletions

View file

@ -7,9 +7,11 @@ namespace Stancl\Tenancy\Commands;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Console\Migrations\MigrateCommand;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Database\QueryException;
use Stancl\Tenancy\Concerns\DealsWithMigrations;
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
use Stancl\Tenancy\Concerns\HasTenantOptions;
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
use Stancl\Tenancy\Events\DatabaseMigrated;
use Stancl\Tenancy\Events\MigratingDatabase;
@ -28,6 +30,8 @@ class Migrate extends MigrateCommand
{
parent::__construct($migrator, $dispatcher);
$this->addOption('skip-failing');
$this->specifyParameters();
}
@ -43,16 +47,23 @@ class Migrate extends MigrateCommand
return 1;
}
tenancy()->runForMultiple($this->getTenants(), function ($tenant) {
$this->components->info("Tenant: {$tenant->getTenantKey()}");
foreach ($this->getTenants() as $tenant) {
try {
$tenant->run(function ($tenant) {
$this->line("Tenant: {$tenant->getTenantKey()}");
event(new MigratingDatabase($tenant));
event(new MigratingDatabase($tenant));
// Migrate
parent::handle();
// Migrate
parent::handle();
event(new DatabaseMigrated($tenant));
});
event(new DatabaseMigrated($tenant));
});
} catch (TenantDatabaseDoesNotExistException|QueryException $th) {
if (! $this->option('skip-failing')) {
throw $th;
}
}
}
return 0;
}