-
Notifications
You must be signed in to change notification settings - Fork 121
Add migrations reset command #1051
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
beb3881
Add migrations reset command for development workflow
dereuromark d76d41f
Fix foreign key handling for PostgreSQL and SQL Server
dereuromark 0cb483f
Update completion test to include reset command
dereuromark b2c7f57
Improve reset command logic
dereuromark 746421f
Simplify reset command to drop all tables
dereuromark ceaf927
Use instanceof for driver type checking and extract helper method
dereuromark 8d5b099
Move FK constraint handling to adapter layer
dereuromark 48ddf9e
Add FK constraint methods to test trait
dereuromark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
| * | ||
| * Licensed under The MIT License | ||
| * Redistributions of files must retain the above copyright notice. | ||
| * | ||
| * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
| * @link https://cakephp.org CakePHP(tm) Project | ||
| * @license https://www.opensource.org/licenses/mit-license.php MIT License | ||
| */ | ||
| namespace Migrations\Command; | ||
|
|
||
| use Cake\Command\Command; | ||
| use Cake\Console\Arguments; | ||
| use Cake\Console\ConsoleIo; | ||
| use Cake\Console\ConsoleOptionParser; | ||
| use Cake\Database\Connection; | ||
| use Cake\Datasource\ConnectionManager; | ||
| use Cake\Event\EventDispatcherTrait; | ||
| use Migrations\Config\ConfigInterface; | ||
| use Migrations\Db\Adapter\AdapterInterface; | ||
| use Migrations\Db\Adapter\DirectActionInterface; | ||
| use Migrations\Migration\ManagerFactory; | ||
| use RuntimeException; | ||
| use Throwable; | ||
|
|
||
| /** | ||
| * Reset command drops all tables and re-runs all migrations. | ||
| * | ||
| * This is a destructive operation intended for development use. | ||
| */ | ||
| class ResetCommand extends Command | ||
| { | ||
| /** | ||
| * @use \Cake\Event\EventDispatcherTrait<\Migrations\Command\ResetCommand> | ||
| */ | ||
| use EventDispatcherTrait; | ||
|
|
||
| /** | ||
| * The default name added to the application command list | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function defaultName(): string | ||
| { | ||
| return 'migrations reset'; | ||
| } | ||
|
|
||
| /** | ||
| * Configure the option parser | ||
| * | ||
| * @param \Cake\Console\ConsoleOptionParser $parser The option parser to configure | ||
| * @return \Cake\Console\ConsoleOptionParser | ||
| */ | ||
| public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser | ||
| { | ||
| $parser->setDescription([ | ||
| 'Drop all tables and re-run all migrations.', | ||
| '', | ||
| '<warning>This is a destructive operation!</warning>', | ||
| 'All data in the database will be lost.', | ||
| '', | ||
| '<info>migrations reset</info>', | ||
| '<info>migrations reset -c secondary</info>', | ||
| '<info>migrations reset --dry-run</info>', | ||
| ])->addOption('plugin', [ | ||
| 'short' => 'p', | ||
| 'help' => 'The plugin to run migrations for', | ||
| ])->addOption('connection', [ | ||
| 'short' => 'c', | ||
| 'help' => 'The datasource connection to use', | ||
| 'default' => 'default', | ||
| ])->addOption('source', [ | ||
| 'short' => 's', | ||
| 'default' => ConfigInterface::DEFAULT_MIGRATION_FOLDER, | ||
| 'help' => 'The folder where your migrations are', | ||
| ])->addOption('dry-run', [ | ||
| 'short' => 'x', | ||
| 'help' => 'Preview what tables would be dropped without making changes', | ||
| 'boolean' => true, | ||
| ])->addOption('no-lock', [ | ||
| 'help' => 'If present, no lock file will be generated after migrating', | ||
| 'boolean' => true, | ||
| ]); | ||
|
|
||
| return $parser; | ||
| } | ||
|
|
||
| /** | ||
| * Execute the command. | ||
| * | ||
| * @param \Cake\Console\Arguments $args The command arguments. | ||
| * @param \Cake\Console\ConsoleIo $io The console io | ||
| * @return int|null The exit code or null for success | ||
| */ | ||
| public function execute(Arguments $args, ConsoleIo $io): ?int | ||
| { | ||
| $event = $this->dispatchEvent('Migration.beforeReset'); | ||
| if ($event->isStopped()) { | ||
| return $event->getResult() ? self::CODE_SUCCESS : self::CODE_ERROR; | ||
| } | ||
|
|
||
| $connectionName = (string)$args->getOption('connection'); | ||
| /** @var \Cake\Database\Connection $connection */ | ||
| $connection = ConnectionManager::get($connectionName); | ||
| $dryRun = (bool)$args->getOption('dry-run'); | ||
|
|
||
| if ($dryRun) { | ||
| $io->out('<warning>DRY-RUN mode enabled - no changes will be made</warning>'); | ||
| $io->out(''); | ||
| } | ||
|
|
||
| // Get tables to drop | ||
| $tablesToDrop = $this->getTablesToDrop($connection); | ||
|
|
||
| if (empty($tablesToDrop)) { | ||
| $io->out('<info>No tables to drop.</info>'); | ||
| $io->out(''); | ||
| $io->out('Running migrations...'); | ||
|
|
||
| return $this->runMigrationsAndDispatch($args, $io); | ||
| } | ||
|
|
||
| // Show what will be dropped | ||
| $io->out('<warning>The following tables will be dropped:</warning>'); | ||
| foreach ($tablesToDrop as $table) { | ||
| $io->out(' - ' . $table); | ||
| } | ||
| $io->out(''); | ||
|
|
||
| // Ask for confirmation (unless dry-run) | ||
| if (!$dryRun) { | ||
| $continue = $io->askChoice( | ||
| 'This will permanently delete all data. Do you want to continue?', | ||
| ['y', 'n'], | ||
| 'n', | ||
| ); | ||
|
Comment on lines
+136
to
+140
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. 👍 Thanks for making this safe by default. |
||
| if ($continue !== 'y') { | ||
| $io->warning('Reset operation aborted.'); | ||
|
|
||
| return self::CODE_SUCCESS; | ||
| } | ||
| } | ||
|
|
||
| // Drop tables | ||
| $io->out(''); | ||
| if (!$dryRun) { | ||
| $factory = new ManagerFactory([ | ||
| 'plugin' => $args->getOption('plugin'), | ||
| 'source' => $args->getOption('source'), | ||
| 'connection' => $args->getOption('connection'), | ||
| ]); | ||
| $manager = $factory->createManager($io); | ||
| $adapter = $manager->getEnvironment()->getAdapter(); | ||
|
|
||
| $this->dropTables($adapter, $tablesToDrop, $io); | ||
| } else { | ||
| $io->info('DRY-RUN: Would drop ' . count($tablesToDrop) . ' table(s).'); | ||
| } | ||
|
|
||
| $io->out(''); | ||
|
|
||
| // Re-run migrations | ||
| if (!$dryRun) { | ||
| return $this->runMigrationsAndDispatch($args, $io); | ||
| } | ||
|
|
||
| $io->info('DRY-RUN: Would re-run all migrations.'); | ||
|
|
||
| return self::CODE_SUCCESS; | ||
| } | ||
|
|
||
| /** | ||
| * Get list of tables to drop. | ||
| * | ||
| * @param \Cake\Database\Connection $connection Database connection | ||
| * @return array<string> List of table names | ||
| */ | ||
| protected function getTablesToDrop(Connection $connection): array | ||
| { | ||
| $schema = $connection->getDriver()->schemaDialect(); | ||
|
|
||
| return $schema->listTables(); | ||
| } | ||
|
|
||
| /** | ||
| * Drop tables with foreign key handling. | ||
| * | ||
| * @param \Migrations\Db\Adapter\AdapterInterface $adapter The adapter | ||
| * @param array<string> $tables Tables to drop | ||
| * @param \Cake\Console\ConsoleIo $io Console IO | ||
| * @return void | ||
| */ | ||
| protected function dropTables(AdapterInterface $adapter, array $tables, ConsoleIo $io): void | ||
| { | ||
| if (!$adapter instanceof DirectActionInterface) { | ||
| throw new RuntimeException('The adapter must implement DirectActionInterface'); | ||
| } | ||
|
|
||
| $adapter->disableForeignKeyConstraints(); | ||
|
|
||
| try { | ||
| foreach ($tables as $table) { | ||
| $io->verbose("Dropping table: {$table}"); | ||
| $adapter->dropTable($table); | ||
| } | ||
| } finally { | ||
| $adapter->enableForeignKeyConstraints(); | ||
| } | ||
|
|
||
| $io->success('Dropped ' . count($tables) . ' table(s).'); | ||
| } | ||
|
|
||
| /** | ||
| * Run migrations and dispatch afterReset event. | ||
| * | ||
| * @param \Cake\Console\Arguments $args The command arguments. | ||
| * @param \Cake\Console\ConsoleIo $io The console io | ||
| * @return int|null The exit code | ||
| */ | ||
| protected function runMigrationsAndDispatch(Arguments $args, ConsoleIo $io): ?int | ||
| { | ||
| $result = $this->runMigrations($args, $io); | ||
| $this->dispatchEvent('Migration.afterReset'); | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * Run migrations. | ||
| * | ||
| * @param \Cake\Console\Arguments $args The command arguments. | ||
| * @param \Cake\Console\ConsoleIo $io The console io | ||
| * @return int|null The exit code | ||
| */ | ||
| protected function runMigrations(Arguments $args, ConsoleIo $io): ?int | ||
| { | ||
| $factory = new ManagerFactory([ | ||
| 'plugin' => $args->getOption('plugin'), | ||
| 'source' => $args->getOption('source'), | ||
| 'connection' => $args->getOption('connection'), | ||
| 'dry-run' => (bool)$args->getOption('dry-run'), | ||
| ]); | ||
|
|
||
| $manager = $factory->createManager($io); | ||
| $config = $manager->getConfig(); | ||
|
|
||
| $io->verbose('<info>using connection</info> ' . (string)$args->getOption('connection')); | ||
| $io->verbose('<info>using paths</info> ' . $config->getMigrationPath()); | ||
|
|
||
| try { | ||
| $start = microtime(true); | ||
| $manager->migrate(null, false, null); | ||
| $end = microtime(true); | ||
| } catch (Throwable $e) { | ||
| $io->err('<error>' . $e->getMessage() . '</error>'); | ||
| $io->verbose($e->getTraceAsString()); | ||
|
|
||
| return self::CODE_ERROR; | ||
| } | ||
|
|
||
| $io->comment('All Done. Took ' . sprintf('%.4fs', $end - $start)); | ||
| $io->out(''); | ||
|
|
||
| $exitCode = self::CODE_SUCCESS; | ||
|
|
||
| // Run dump command to generate lock file | ||
| if (!$args->getOption('no-lock') && !$args->getOption('dry-run')) { | ||
| $io->verbose(''); | ||
| $io->verbose('Dumping the current schema of the database to be used while baking a diff'); | ||
| $io->verbose(''); | ||
|
|
||
| $newArgs = DumpCommand::extractArgs($args); | ||
| $exitCode = $this->executeCommand(DumpCommand::class, $newArgs, $io); | ||
| } | ||
|
|
||
| return $exitCode; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.