Skip to content
Merged
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
7 changes: 2 additions & 5 deletions src/Command/SeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Exception;
use Migrations\Config\ConfigInterface;
use Migrations\Migration\ManagerFactory;
use Migrations\Util\Util;

/**
* Seed command runs seeder scripts
Expand Down Expand Up @@ -182,11 +183,7 @@ protected function executeSeeds(Arguments $args, ConsoleIo $io): ?int
$io->out('');
$io->out('<info>The following seeds will be executed:</info>');
foreach ($availableSeeds as $seed) {
$seedName = $seed->getName();
if (str_ends_with($seedName, 'Seed')) {
$seedName = substr($seedName, 0, -4);
}
$io->out(' - ' . $seedName);
$io->out(' - ' . Util::getSeedDisplayName($seed->getName()));
}
$io->out('');
if (!(bool)$args->getOption('force')) {
Expand Down
12 changes: 5 additions & 7 deletions src/Command/SeedResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\Console\ConsoleOptionParser;
use Migrations\Config\ConfigInterface;
use Migrations\Migration\ManagerFactory;
use Migrations\Util\Util;

/**
* Seed reset command removes seeds from the execution log
Expand Down Expand Up @@ -112,11 +113,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
$io->out('');
$io->out('<info>All seeds will be reset:</info>');
foreach ($seedsToReset as $seed) {
$seedName = $seed->getName();
if (str_ends_with($seedName, 'Seed')) {
$seedName = substr($seedName, 0, -4);
}
$io->out(' - ' . $seedName);
$io->out(' - ' . Util::getSeedDisplayName($seed->getName()));
}
$io->out('');

Expand All @@ -132,14 +129,15 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
// Reset the seeds
$count = 0;
foreach ($seedsToReset as $seed) {
$seedName = Util::getSeedDisplayName($seed->getName());
if ($manager->isSeedExecuted($seed)) {
if (!$config->isDryRun()) {
$adapter->removeSeedFromLog($seed);
}
$io->info("Reset: {$seed->getName()}");
$io->info("Reset: {$seedName} seed");
$count++;
} else {
$io->verbose("Skipped (not executed): {$seed->getName()}");
$io->verbose("Skipped (not executed): {$seedName} seed");
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Command/SeedStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Cake\Core\Configure;
use Migrations\Config\ConfigInterface;
use Migrations\Migration\ManagerFactory;
use Migrations\Util\Util;

/**
* Seed status command shows which seeds have been executed
Expand Down Expand Up @@ -129,8 +130,11 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
}
}

// Strip 'Seed' suffix for display and add ' seed' suffix
$displayName = Util::getSeedDisplayName($seedName) . ' seed';

$statuses[] = [
'seedName' => $seedName,
'seedName' => $displayName,
'plugin' => $plugin,
'status' => $executed ? 'executed' : 'pending',
'executedAt' => $executedAt,
Expand Down
2 changes: 1 addition & 1 deletion src/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ protected function printMigrationStatus(MigrationInterface $migration, string $s
protected function printSeedStatus(SeedInterface $seed, string $status, ?string $duration = null): void
{
$this->printStatusOutput(
$seed->getName(),
Util::getSeedDisplayName($seed->getName()) . ' seed',
$status,
$duration,
);
Expand Down
17 changes: 17 additions & 0 deletions src/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ public static function isValidSeedFileName(string $fileName): bool
return (bool)preg_match(static::SEED_FILE_NAME_PATTERN, $fileName);
}

/**
* Get a human-readable display name for a seed class.
*
* Strips the 'Seed' suffix from class names like 'UsersSeed' to produce 'Users'.
*
* @param string $seedName The seed class name
* @return string The display name without the 'Seed' suffix
*/
public static function getSeedDisplayName(string $seedName): string
{
if (str_ends_with($seedName, 'Seed')) {
return substr($seedName, 0, -4);
}

return $seedName;
}

/**
* Expands a set of paths with curly braces (if supported by the OS).
*
Expand Down
46 changes: 23 additions & 23 deletions tests/TestCase/Command/SeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testSeederOne(): void
$this->exec('seeds run -c test NumbersSeed');

$this->assertExitSuccess();
$this->assertOutputContains('NumbersSeed:</info> <comment>seeding');
$this->assertOutputContains('Numbers seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand All @@ -110,8 +110,8 @@ public function testSeederBaseSeed(): void
$this->createTables();
$this->exec('seeds run -c test --source BaseSeeds MigrationSeedNumbers');
$this->assertExitSuccess();
$this->assertOutputContains('MigrationSeedNumbers:</info> <comment>seeding');
$this->assertOutputContains('AnotherNumbersSeed:</info> <comment>seeding');
$this->assertOutputContains('MigrationSeedNumbers seed:</info> <comment>seeding');
$this->assertOutputContains('AnotherNumbers seed:</info> <comment>seeding');
$this->assertOutputContains('radix=10');
$this->assertOutputContains('fetchRow=121');
$this->assertOutputContains('hasTable=1');
Expand Down Expand Up @@ -154,8 +154,8 @@ public function testSeederMultiple(): void
$this->exec('seeds run -c test --source CallSeeds LettersSeed,NumbersCallSeed');

$this->assertExitSuccess();
$this->assertOutputContains('NumbersCallSeed:</info> <comment>seeding');
$this->assertOutputContains('LettersSeed:</info> <comment>seeding');
$this->assertOutputContains('NumbersCall seed:</info> <comment>seeding');
$this->assertOutputContains('Letters seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand All @@ -182,7 +182,7 @@ public function testSeederWithTimestampFields(): void
$this->exec('seeds run -c test StoresSeed');

$this->assertExitSuccess();
$this->assertOutputContains('StoresSeed:</info> <comment>seeding');
$this->assertOutputContains('Stores seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand All @@ -208,7 +208,7 @@ public function testDryRunModeWarning(): void

$this->assertExitSuccess();
$this->assertOutputContains('DRY-RUN mode enabled');
$this->assertOutputContains('NumbersSeed:</info> <comment>seeding');
$this->assertOutputContains('Numbers seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');
}

Expand All @@ -219,7 +219,7 @@ public function testDryRunModeShortOption(): void

$this->assertExitSuccess();
$this->assertOutputContains('DRY-RUN mode enabled');
$this->assertOutputContains('NumbersSeed:</info> <comment>seeding');
$this->assertOutputContains('Numbers seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');
}

Expand All @@ -245,8 +245,8 @@ public function testDryRunModeMultipleSeeds(): void

$this->assertExitSuccess();
$this->assertOutputContains('DRY-RUN mode enabled');
$this->assertOutputContains('NumbersCallSeed:</info> <comment>seeding');
$this->assertOutputContains('LettersSeed:</info> <comment>seeding');
$this->assertOutputContains('NumbersCall seed:</info> <comment>seeding');
$this->assertOutputContains('Letters seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand Down Expand Up @@ -303,7 +303,7 @@ public function testDryRunModeWithStoresSeed(): void
$this->exec('seeds run -c test StoresSeed --dry-run');
$this->assertExitSuccess();
$this->assertOutputContains('DRY-RUN mode enabled');
$this->assertOutputContains('StoresSeed:</info> <comment>seeding');
$this->assertOutputContains('Stores seed:</info> <comment>seeding');

$finalCount = $connection->execute('SELECT COUNT(*) FROM stores')->fetchColumn(0);
$this->assertEquals($initialCount, $finalCount, 'Dry-run mode should not modify stores table');
Expand All @@ -315,7 +315,7 @@ public function testSeederAnonymousClass(): void
$this->exec('seeds run -c test AnonymousStoreSeed');

$this->assertExitSuccess();
$this->assertOutputContains('AnonymousStoreSeed:</info> <comment>seeding');
$this->assertOutputContains('AnonymousStore seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand All @@ -334,7 +334,7 @@ public function testSeederShortName(): void
$this->exec('seeds run -c test Numbers');

$this->assertExitSuccess();
$this->assertOutputContains('NumbersSeed:</info> <comment>seeding');
$this->assertOutputContains('Numbers seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand All @@ -349,8 +349,8 @@ public function testSeederShortNameMultiple(): void
$this->exec('seeds run -c test --source CallSeeds Letters,NumbersCall');

$this->assertExitSuccess();
$this->assertOutputContains('NumbersCallSeed:</info> <comment>seeding');
$this->assertOutputContains('LettersSeed:</info> <comment>seeding');
$this->assertOutputContains('NumbersCall seed:</info> <comment>seeding');
$this->assertOutputContains('Letters seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand All @@ -368,7 +368,7 @@ public function testSeederShortNameAnonymous(): void
$this->exec('seeds run -c test AnonymousStore');

$this->assertExitSuccess();
$this->assertOutputContains('AnonymousStoreSeed:</info> <comment>seeding');
$this->assertOutputContains('AnonymousStore seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand Down Expand Up @@ -417,7 +417,7 @@ public function testSeederSpecificSeedSkipsConfirmation(): void
$this->assertExitSuccess();
$this->assertOutputNotContains('The following seeds will be executed:');
$this->assertOutputNotContains('Do you want to continue?');
$this->assertOutputContains('NumbersSeed:</info> <comment>seeding');
$this->assertOutputContains('Numbers seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');
}

Expand All @@ -427,8 +427,8 @@ public function testSeederCommaSeparated(): void
$this->exec('seeds run -c test --source CallSeeds Letters,NumbersCall');

$this->assertExitSuccess();
$this->assertOutputContains('NumbersCallSeed:</info> <comment>seeding');
$this->assertOutputContains('LettersSeed:</info> <comment>seeding');
$this->assertOutputContains('NumbersCall seed:</info> <comment>seeding');
$this->assertOutputContains('Letters seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
Expand All @@ -450,7 +450,7 @@ public function testSeedStateTracking(): void
// First run should execute the seed
$this->exec('seeds run -c test NumbersSeed');
$this->assertExitSuccess();
$this->assertOutputContains('NumbersSeed:</info> <comment>seeding');
$this->assertOutputContains('Numbers seed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

// Verify data was inserted
Expand All @@ -460,7 +460,7 @@ public function testSeedStateTracking(): void
// Second run should skip the seed (already executed)
$this->exec('seeds run -c test NumbersSeed');
$this->assertExitSuccess();
$this->assertOutputContains('NumbersSeed:</info> <comment>already executed');
$this->assertOutputContains('Numbers seed:</info> <comment>already executed');
$this->assertOutputNotContains('seeding');

// Verify no additional data was inserted
Expand All @@ -470,7 +470,7 @@ public function testSeedStateTracking(): void
// Run with --force should re-execute
$this->exec('seeds run -c test NumbersSeed --force');
$this->assertExitSuccess();
$this->assertOutputContains('NumbersSeed:</info> <comment>seeding');
$this->assertOutputContains('Numbers seed:</info> <comment>seeding');

// Verify data was inserted again (now 2 records)
$query = $connection->execute('SELECT COUNT(*) FROM numbers');
Expand All @@ -495,7 +495,7 @@ public function testSeedStatusCommand(): void
$this->exec('seeds status -c test');
$this->assertExitSuccess();
$this->assertOutputContains('executed');
$this->assertOutputContains('NumbersSeed');
$this->assertOutputContains('Numbers');
}

public function testSeedResetCommand(): void
Expand Down