Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/en/advanced/integration-and-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/en/guides/seeding.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion src/Command/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
'<info>migrations reset</info>',
'<info>migrations reset -c secondary</info>',
'<info>migrations reset --dry-run</info>',
'<info>migrations reset --yes</info>',
])->addOption('plugin', [
'short' => 'p',
'help' => 'The plugin to run migrations for',
Expand All @@ -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;
Expand Down Expand Up @@ -136,8 +141,14 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
}
$io->out('');

if (!$dryRun && !$args->getOption('yes') && $args->getOption('quiet')) {
$io->err('<error>Refusing to drop tables without confirmation in non-interactive mode. Pass --yes to continue.</error>');

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'],
Expand Down
17 changes: 12 additions & 5 deletions src/Command/SeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
'<info>seeds run Users,Posts</info>',
'<info>seeds run --plugin Demo</info>',
'<info>seeds run --connection secondary</info>',
'<info>seeds run --yes</info>',
'',
'Runs all seeds if no seed names are specified. When running all seeds',
'in an interactive terminal, a confirmation prompt is shown.',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/Command/SeedResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
'<info>seeds reset --seed Users,Posts</info>',
'<info>seeds reset --plugin Demo</info>',
'<info>seeds reset -c secondary</info>',
'<info>seeds reset --yes</info>',
])->addOption('seed', [
'help' => 'Comma-separated list of specific seeds to reset. Resets all seeds if not specified.',
])->addOption('plugin', [
Expand All @@ -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;
Expand Down Expand Up @@ -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('<error>Refusing to reset seeds without confirmation in non-interactive mode. Pass --yes to continue.</error>');

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.');
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Command/ResetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
48 changes: 48 additions & 0 deletions tests/TestCase/Command/SeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading