Skip to content

Commit df4be2e

Browse files
lukinoveclordofthebrainstancl
authored
migrate-fresh: show migration output when verbose (#1464)
Resubmission of #1369 (by @lordofthebrain), changes adapted to v4. Also added a test (passes with the MigrateFresh changes, fails without them). --------- Co-authored-by: lordofthebrain <f.mangelsdorf@gmail.com> Co-authored-by: Samuel Stancl <samuel@archte.ch>
1 parent aa9d1d7 commit df4be2e

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/Commands/MigrateFresh.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
1515
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
1616
use Symfony\Component\Console\Input\InputOption;
17+
use Symfony\Component\Console\Output\NullOutput;
1718
use Symfony\Component\Console\Output\OutputInterface as OI;
1819

1920
class MigrateFresh extends BaseCommand
@@ -72,11 +73,13 @@ protected function wipeDB(): bool
7273

7374
protected function migrateTenant(TenantWithDatabase $tenant): bool
7475
{
75-
return $this->callSilently('tenants:migrate', [
76+
$output = $this->getOutput()->isVerbose() ? $this->output : new NullOutput;
77+
78+
return $this->runCommand('tenants:migrate', [
7679
'--tenants' => [$tenant->getTenantKey()],
7780
'--step' => $this->option('step'),
7881
'--force' => true,
79-
]) === 0;
82+
], $output) === 0;
8083
}
8184

8285
protected function childHandle(mixed ...$args): bool

tests/CommandsTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,36 @@
366366
expect(DB::table('users')->exists())->toBeFalse();
367367
});
368368

369+
test('migrate fresh command only shows migration output when run with the verbose option', function () {
370+
$tenant = Tenant::create();
371+
$migratingOutput = 'Migrating tenant ' . $tenant->getTenantKey();
372+
373+
// CI runs pest with --verbose which makes Artisan::call() inherit the verbosity
374+
// so we cannot easily test commands without -v. To work around that, we temporarily
375+
// override $_ENV['SHELL_VERBOSITY'] immediately before executing the command. If this
376+
// ever stops working, try also overriding the value in $_SERVER and putenv().
377+
$emptySentinel = new \stdClass();
378+
$originalVerbosity = $_ENV['SHELL_VERBOSITY'] ?? $emptySentinel;
379+
try {
380+
$_ENV['SHELL_VERBOSITY'] = 0;
381+
Artisan::call('tenants:migrate-fresh');
382+
$defaultOutput = Artisan::output();
383+
} finally {
384+
if ($originalVerbosity === $emptySentinel) {
385+
unset($_ENV['SHELL_VERBOSITY']);
386+
} else {
387+
$_ENV['SHELL_VERBOSITY'] = $originalVerbosity;
388+
}
389+
}
390+
391+
Artisan::call('tenants:migrate-fresh -v');
392+
$verboseOutput = Artisan::output();
393+
394+
// The output is silent by default and only shown with the verbose option
395+
expect($defaultOutput)->not()->toContain($migratingOutput);
396+
expect($verboseOutput)->toContain($migratingOutput);
397+
});
398+
369399
test('migrate fresh command respects force option in production', function () {
370400
// Set environment to production
371401
app()->detectEnvironment(fn() => 'production');

0 commit comments

Comments
 (0)