From e0520a777366cc1f8c5dfccc92c629bae12e260b Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Fri, 20 Nov 2026 15:10:51 +0100 Subject: [PATCH] Add --yes to skip seed and reset confirmations seeds run, seeds reset and migrations reset now accept --yes (-y) to skip the confirmation prompt while keeping normal output. In non-interactive mode (-q) without --yes, seeds reset and migrations reset now exit with an error instead of answering the prompt with its default "n" and exiting 0 without doing anything. seeds run -q keeps skipping its prompt. --- .../en/advanced/integration-and-deployment.md | 14 ++++++ docs/en/guides/seeding.md | 4 ++ src/Command/ResetCommand.php | 13 ++++- src/Command/SeedCommand.php | 17 +++++-- src/Command/SeedResetCommand.php | 13 ++++- tests/TestCase/Command/ResetCommandTest.php | 30 ++++++++++++ tests/TestCase/Command/SeedCommandTest.php | 48 +++++++++++++++++++ 7 files changed, 132 insertions(+), 7 deletions(-) diff --git a/docs/en/advanced/integration-and-deployment.md b/docs/en/advanced/integration-and-deployment.md index 4426968d..f5204d5f 100644 --- a/docs/en/advanced/integration-and-deployment.md +++ b/docs/en/advanced/integration-and-deployment.md @@ -209,6 +209,20 @@ bin/cake migrations migrate bin/cake schema_cache clear ``` +### Non-interactive Commands + +Running all seeds with `seeds run`, resetting seeds with `seeds reset`, and +resetting migrations with `migrations reset` ask for confirmation. Pass `--yes` +to skip the prompt while keeping normal output. For `seeds run`, `-q` also skips +the prompt. Without `--yes`, `seeds reset -q` and `migrations reset -q` fail +with a non-zero exit code. + +```bash +bin/cake migrations migrate +bin/cake seeds run --yes +bin/cake schema_cache clear +``` + ## Alert of Missing Migrations You can use the `Migrations.PendingMigrations` middleware in local development diff --git a/docs/en/guides/seeding.md b/docs/en/guides/seeding.md index 13411cd7..b08d9e81 100644 --- a/docs/en/guides/seeding.md +++ b/docs/en/guides/seeding.md @@ -198,6 +198,10 @@ To reset all seeds' execution state (allowing them to run again without `--force bin/cake seeds reset ``` +Running all seeds and resetting seeds ask for confirmation. Pass `--yes` (`-y`) +to skip the prompt in scripts. The `--force` option re-runs already executed +seeds; it does not skip the prompt. + > [!NOTE] > When re-running seeds with `--force`, be careful to ensure your seeds are > idempotent (safe to run multiple times) or they may create duplicate data. diff --git a/src/Command/ResetCommand.php b/src/Command/ResetCommand.php index f5a8d021..437edf92 100644 --- a/src/Command/ResetCommand.php +++ b/src/Command/ResetCommand.php @@ -71,6 +71,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption 'migrations reset', 'migrations reset -c secondary', 'migrations reset --dry-run', + 'migrations reset --yes', ])->addOption('plugin', [ 'short' => 'p', 'help' => 'The plugin to run migrations for', @@ -89,6 +90,10 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption ])->addOption('no-lock', [ 'help' => 'If present, no lock file will be generated after migrating', 'boolean' => true, + ])->addOption('yes', [ + 'short' => 'y', + 'help' => 'Skip the confirmation prompt and continue', + 'boolean' => true, ]); return $parser; @@ -136,8 +141,14 @@ public function execute(Arguments $args, ConsoleIo $io): ?int } $io->out(''); + if (!$dryRun && !$args->getOption('yes') && $args->getOption('quiet')) { + $io->err('Refusing to drop tables without confirmation in non-interactive mode. Pass --yes to continue.'); + + return static::CODE_ERROR; + } + // Ask for confirmation (unless dry-run) - if (!$dryRun) { + if (!$dryRun && !$args->getOption('yes')) { $continue = $io->askChoice( 'This will permanently delete all data. Do you want to continue?', ['y', 'n'], diff --git a/src/Command/SeedCommand.php b/src/Command/SeedCommand.php index a764d064..dd057f46 100644 --- a/src/Command/SeedCommand.php +++ b/src/Command/SeedCommand.php @@ -65,6 +65,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption 'seeds run Users,Posts', 'seeds run --plugin Demo', 'seeds run --connection secondary', + 'seeds run --yes', '', 'Runs all seeds if no seed names are specified. When running all seeds', 'in an interactive terminal, a confirmation prompt is shown.', @@ -99,6 +100,11 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption 'help' => 'Force re-running seeds that have already been executed', 'boolean' => true, ]) + ->addOption('yes', [ + 'short' => 'y', + 'help' => 'Skip the confirmation prompt and continue', + 'boolean' => true, + ]) ->addOption('fake', [ 'help' => 'Mark seeds as executed without actually running them', 'boolean' => true, @@ -227,12 +233,13 @@ protected function executeSeeds(Arguments $args, ConsoleIo $io): ?int } $io->out(''); - // Ask for confirmation - $continue = $io->askChoice('Do you want to continue?', ['y', 'n'], 'n'); - if ($continue !== 'y') { - $io->warning('Seed operation aborted.'); + if (!$args->getOption('yes')) { + $continue = $io->askChoice('Do you want to continue?', ['y', 'n'], 'n'); + if ($continue !== 'y') { + $io->warning('Seed operation aborted.'); - return self::CODE_SUCCESS; + return self::CODE_SUCCESS; + } } } diff --git a/src/Command/SeedResetCommand.php b/src/Command/SeedResetCommand.php index 34d5ae3f..88d15a05 100644 --- a/src/Command/SeedResetCommand.php +++ b/src/Command/SeedResetCommand.php @@ -61,6 +61,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption 'seeds reset --seed Users,Posts', 'seeds reset --plugin Demo', 'seeds reset -c secondary', + 'seeds reset --yes', ])->addOption('seed', [ 'help' => 'Comma-separated list of specific seeds to reset. Resets all seeds if not specified.', ])->addOption('plugin', [ @@ -78,6 +79,10 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption 'short' => 'd', 'help' => 'Show what would be reset without actually doing it', 'boolean' => true, + ])->addOption('yes', [ + 'short' => 'y', + 'help' => 'Skip the confirmation prompt and continue', + 'boolean' => true, ]); return $parser; @@ -146,7 +151,13 @@ public function execute(Arguments $args, ConsoleIo $io): ?int } $io->out(''); - if (!$config->isDryRun()) { + if (!$config->isDryRun() && !$args->getOption('yes') && $args->getOption('quiet')) { + $io->err('Refusing to reset seeds without confirmation in non-interactive mode. Pass --yes to continue.'); + + return static::CODE_ERROR; + } + + if (!$config->isDryRun() && !$args->getOption('yes')) { $continue = $io->askChoice('Do you want to continue?', ['y', 'n'], 'n'); if ($continue !== 'y') { $io->warning('Reset operation aborted.'); diff --git a/tests/TestCase/Command/ResetCommandTest.php b/tests/TestCase/Command/ResetCommandTest.php index 7ccd45a7..1da8c04a 100644 --- a/tests/TestCase/Command/ResetCommandTest.php +++ b/tests/TestCase/Command/ResetCommandTest.php @@ -93,6 +93,36 @@ public function testResetConfirmed(): void $this->assertOutputContains('All Done'); } + public function testResetWithYes(): void + { + $this->exec('migrations migrate -c test --no-lock'); + $this->assertExitSuccess(); + $this->resetOutput(); + + $this->exec('migrations reset -c test --no-lock --yes'); + $this->assertExitSuccess(); + $this->assertOutputContains('Dropped'); + $this->assertOutputContains('All Done'); + $this->assertOutputNotContains('Do you want to continue?'); + } + + public function testResetQuietRefuses(): void + { + $connection = ConnectionManager::get('test'); + $tablesBefore = $connection->getSchemaCollection()->listTables(); + + $this->exec('migrations reset -c test --no-lock -q'); + $this->assertExitError(); + $this->assertErrorContains('--yes'); + $this->assertSame($tablesBefore, $connection->getSchemaCollection()->listTables()); + } + + public function testResetQuietDryRun(): void + { + $this->exec('migrations reset -c test --no-lock --dry-run -q'); + $this->assertExitSuccess(); + } + public function testResetWithLock(): void { $migrationPath = ROOT . DS . 'config' . DS . 'Migrations'; diff --git a/tests/TestCase/Command/SeedCommandTest.php b/tests/TestCase/Command/SeedCommandTest.php index e9398410..860bf31c 100644 --- a/tests/TestCase/Command/SeedCommandTest.php +++ b/tests/TestCase/Command/SeedCommandTest.php @@ -617,6 +617,54 @@ public function testSeedResetCommand(): void $this->assertOutputNotContains('already executed'); } + public function testRunAllWithYes(): void + { + $this->createTables(); + $this->exec('seeds run -c test --yes'); + $this->assertExitSuccess(); + $this->assertOutputContains('The following seeds will be executed:'); + $this->assertOutputNotContains('Do you want to continue?'); + $this->assertSame(1, (int)ConnectionManager::get('test')->execute('SELECT COUNT(*) FROM numbers')->fetchColumn(0)); + } + + public function testRunAllWithShortYes(): void + { + $this->createTables(); + $this->exec('seeds run -c test -y'); + $this->assertExitSuccess(); + $this->assertOutputNotContains('Do you want to continue?'); + $this->assertSame(1, (int)ConnectionManager::get('test')->execute('SELECT COUNT(*) FROM numbers')->fetchColumn(0)); + } + + public function testSeedResetWithYes(): void + { + $this->createTables(); + $this->exec('seeds run -c test NumbersSeed'); + $this->exec('seeds reset -c test --yes'); + $this->assertExitSuccess(); + $this->assertOutputNotContains('Do you want to continue?'); + $this->assertSame(0, (int)ConnectionManager::get('test')->execute('SELECT COUNT(*) FROM cake_seeds')->fetchColumn(0)); + } + + public function testSeedResetQuietRefuses(): void + { + $this->createTables(); + $this->exec('seeds run -c test NumbersSeed'); + $this->exec('seeds reset -c test -q'); + $this->assertExitError(); + $this->assertErrorContains('--yes'); + $this->assertSame(1, (int)ConnectionManager::get('test')->execute('SELECT COUNT(*) FROM cake_seeds')->fetchColumn(0)); + } + + public function testSeedResetQuietWithYes(): void + { + $this->createTables(); + $this->exec('seeds run -c test NumbersSeed'); + $this->exec('seeds reset -c test -q --yes'); + $this->assertExitSuccess(); + $this->assertSame(0, (int)ConnectionManager::get('test')->execute('SELECT COUNT(*) FROM cake_seeds')->fetchColumn(0)); + } + public function testIdempotentSeed(): void { $this->createTables();