-
Notifications
You must be signed in to change notification settings - Fork 120
Make seeds a command argument. #935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
a8611c5
dc3bd1a
ea8b71e
094b4a7
c9699db
f7b81da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,8 @@ | |
| use Cake\Console\Arguments; | ||
| use Cake\Console\ConsoleIo; | ||
| use Cake\Console\ConsoleOptionParser; | ||
| use Cake\Core\Configure; | ||
| use Cake\Event\EventDispatcherTrait; | ||
| use Exception; | ||
| use Migrations\Config\ConfigInterface; | ||
| use Migrations\Migration\ManagerFactory; | ||
|
|
||
|
|
@@ -50,33 +50,44 @@ public static function defaultName(): string | |
| */ | ||
| public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser | ||
| { | ||
| $parser->setDescription([ | ||
| $description = [ | ||
| 'Seed the database with data', | ||
| '', | ||
| 'Runs a seeder script that can populate the database with data, or run mutations', | ||
| 'Runs a seeder script that can populate the database with data, or run mutations:', | ||
| '', | ||
| '<info>migrations seed --connection secondary --seed UserSeed</info>', | ||
| '<info>migrations seed Posts</info>', | ||
| '<info>migrations seed Users,Posts</info>', | ||
| '<info>migrations seed --plugin Demo</info>', | ||
| '<info>migrations seed --connection secondary</info>', | ||
| '', | ||
| 'The `--seed` option can be supplied multiple times to run more than one seed', | ||
| ])->addOption('plugin', [ | ||
| 'short' => 'p', | ||
| 'help' => 'The plugin to run seeds in', | ||
| ])->addOption('connection', [ | ||
| 'short' => 'c', | ||
| 'help' => 'The datasource connection to use', | ||
| 'default' => 'default', | ||
| ])->addOption('dry-run', [ | ||
| 'short' => 'x', | ||
| 'help' => 'Dump queries to stdout instead of executing them', | ||
| 'boolean' => true, | ||
| ])->addOption('source', [ | ||
| 'short' => 's', | ||
| 'default' => ConfigInterface::DEFAULT_SEED_FOLDER, | ||
| 'help' => 'The folder where your seeds are.', | ||
| ])->addOption('seed', [ | ||
| 'help' => 'The name of the seed that you want to run.', | ||
| 'multiple' => true, | ||
| ]); | ||
| 'Runs all seeds if no seed names are specified. When running all seeds', | ||
| 'in an interactive terminal, a confirmation prompt is shown.', | ||
| ]; | ||
|
|
||
| $parser->setDescription($description) | ||
| ->addArgument('seed', [ | ||
| 'help' => 'The name(s) of the seed(s) to run (comma-separated for multiple). Run all seeds if not specified.', | ||
| 'required' => false, | ||
| ]) | ||
| ->addOption('plugin', [ | ||
| 'short' => 'p', | ||
| 'help' => 'The plugin to run seeds in', | ||
| ]) | ||
| ->addOption('connection', [ | ||
| 'short' => 'c', | ||
| 'help' => 'The datasource connection to use', | ||
| 'default' => 'default', | ||
| ]) | ||
| ->addOption('dry-run', [ | ||
| 'short' => 'd', | ||
| 'help' => 'Dump queries to stdout instead of executing them', | ||
| 'boolean' => true, | ||
| ]) | ||
| ->addOption('source', [ | ||
| 'short' => 's', | ||
| 'default' => ConfigInterface::DEFAULT_SEED_FOLDER, | ||
| 'help' => 'The folder where your seeds are.', | ||
| ]); | ||
|
|
||
| return $parser; | ||
| } | ||
|
|
@@ -119,10 +130,20 @@ protected function executeSeeds(Arguments $args, ConsoleIo $io): ?int | |
| $manager = $factory->createManager($io); | ||
| $config = $manager->getConfig(); | ||
|
|
||
| if (version_compare(Configure::version(), '5.2.0', '>=')) { | ||
| $seeds = (array)$args->getArrayOption('seed'); | ||
| } else { | ||
| $seeds = (array)$args->getMultipleOption('seed'); | ||
| // Get seed names from arguments | ||
| $seeds = []; | ||
| if ($args->hasArgument('seed')) { | ||
| $seedArg = $args->getArgument('seed'); | ||
| if ($seedArg !== null) { | ||
| // Split by comma to support comma-separated list | ||
| $seedList = explode(',', $seedArg); | ||
| foreach ($seedList as $seed) { | ||
| $trimmed = trim($seed); | ||
| if ($trimmed !== '') { | ||
| $seeds[] = $trimmed; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $versionOrder = $config->getVersionOrder(); | ||
|
|
@@ -136,10 +157,54 @@ protected function executeSeeds(Arguments $args, ConsoleIo $io): ?int | |
|
|
||
| $start = microtime(true); | ||
| if (!$seeds) { | ||
| // Get all available seeds and ask for confirmation | ||
| try { | ||
| $availableSeeds = $manager->getSeeds(); | ||
| } catch (Exception $e) { | ||
| $io->error('Failed to load seeds: ' . $e->getMessage()); | ||
|
|
||
| return static::CODE_ERROR; | ||
| } | ||
|
|
||
| if (!$availableSeeds) { | ||
| $io->warning('No seeds found.'); | ||
|
|
||
| return self::CODE_SUCCESS; | ||
| } | ||
|
|
||
| // Display the seeds that will be run and ask for confirmation | ||
| // Skip confirmation in quiet mode or non-interactive environments | ||
| $isInteractive = function_exists('posix_isatty') && @posix_isatty(STDIN); | ||
dereuromark marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ($io->level() > ConsoleIo::QUIET && $isInteractive) { | ||
| $io->out(''); | ||
| $io->out('<info>The following seeds will be executed:</info>'); | ||
| foreach ($availableSeeds as $seed) { | ||
| $seedName = $seed->getName(); | ||
| // Remove 'Seed' suffix for display | ||
| if (str_ends_with($seedName, 'Seed')) { | ||
| $seedName = substr($seedName, 0, -4); | ||
| } | ||
| $io->out(' - ' . $seedName); | ||
| } | ||
| $io->out(''); | ||
| $io->out('<warning>Note:</warning> Seeds do not track execution state. They will run'); | ||
| $io->out('regardless of whether they have been executed before. Ensure your'); | ||
| $io->out('seeds are idempotent or manually verify they should be (re)run.'); | ||
| $io->out(''); | ||
|
|
||
| // Ask for confirmation | ||
| $continue = $io->askChoice('Do you want to continue?', ['y', 'n'], 'n'); | ||
| if ($continue !== 'y') { | ||
| $io->warning('Seed operation aborted.'); | ||
|
|
||
| return self::CODE_SUCCESS; | ||
|
Comment on lines
+193
to
+197
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could always ask for confirmation. If users don't want to deal with a confirmation prompt they can use the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats already how its done above with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was suggesting that we don't have a confirm to go with the comment about having |
||
| } | ||
| } | ||
|
|
||
| // run all the seed(ers) | ||
| $manager->seed(); | ||
| } else { | ||
| // run seed(ers) specified in a comma-separated list of classes | ||
| // run seed(ers) specified as arguments | ||
| foreach ($seeds as $seed) { | ||
| $manager->seed(trim($seed)); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that defaulting to running all seeds is a risky default, that should be an error instead. If there is a need for a 'run all' mode we could add a
--alloption.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats why there is now a list of what seeds run and the confirmation before doing. The Extra option is not needed this way. You cannot a accidently do it anymore.