1414use Symfony \Component \Console \Input \InputInterface ;
1515use Symfony \Component \Console \Input \InputOption ;
1616use Symfony \Component \Console \Output \OutputInterface ;
17+ use UserFrosting \Sprinkle \Core \Database \Migrator \Migrator ;
1718
1819/**
19- * migrate:rollback Bakery Command
20- * Rollback the last migrations ran against the database.
20+ * migrate:clean Bakery Command
21+ * Remove stale migrations from the database.
2122 *
22- * @author Louis Charette
23+ * @author Amos Folz
2324 */
2425class MigrateCleanCommand extends MigrateCommand
2526{
@@ -29,12 +30,8 @@ class MigrateCleanCommand extends MigrateCommand
2930 protected function configure ()
3031 {
3132 $ this ->setName ('migrate:clean ' )
32- ->setDescription ('Clean stale records from migrations database ' )
33- ->addOption ('pretend ' , 'p ' , InputOption::VALUE_NONE , 'Run migrations in "dry run" mode. ' )
34- ->addOption ('force ' , 'f ' , InputOption::VALUE_NONE , 'Force the operation to run when in production. ' )
35- ->addOption ('database ' , 'd ' , InputOption::VALUE_REQUIRED , 'The database connection to use. ' )
36- ->addOption ('migration ' , 'm ' , InputOption::VALUE_REQUIRED , 'The specific migration to rollback. ' )
37- ->addOption ('steps ' , 's ' , InputOption::VALUE_REQUIRED , 'Number of batch to rollback. ' , 1 );
33+ ->setDescription ('Remove stale migrations from the database. ' )
34+ ->addOption ('database ' , 'd ' , InputOption::VALUE_REQUIRED , 'The database connection to use. ' );
3835 }
3936
4037 /**
@@ -61,26 +58,36 @@ protected function execute(InputInterface $input, OutputInterface $output)
6158 $ available = $ migrator ->getAvailableMigrations ();
6259
6360 $ stale = $ this ->getStaleRecords ($ ran , $ available );
64- // print_r($stale);
6561
66- $ this ->cleanStaleRecords ($ stale , $ migrator );
62+ if ($ stale ->count () > 0 ) {
63+ $ this ->io ->section ('Stale migrations ' );
64+ $ this ->io ->listing ($ stale ->toArray ());
6765
68- // Display ran migrations
69- $ this ->io ->section ('Cleaned migrations ' );
70- if ($ ran ->count () > 0 ) {
66+ if (!$ this ->io ->confirm ('Continue and remove stale migrations? ' , false )) {
67+ exit ;
68+ }
69+ $ this ->io ->section ('Cleaned migrations ' );
70+ $ this ->cleanStaleRecords ($ stale , $ migrator );
71+ $ this ->io ->listing ($ stale ->toArray ());
7172 } else {
72- $ this ->io ->note ('No installed migrations ' );
73+ $ this ->io ->note ('No stale migrations ' );
7374 }
7475 }
7576
76- protected function cleanStaleRecords (array $ stale , $ migrator )
77+ /**
78+ * Delete stale migrations from the database.
79+ *
80+ * @param Collection $stale Collection of stale migartion classes.
81+ * @param Migrator $migrator Migrator object
82+ */
83+ protected function cleanStaleRecords (Collection $ stale , Migrator $ migrator )
7784 {
78- // print_r($stale );
79- foreach ( $ stale as $ staleFile ) {
80- print_r ( $ staleFile );
81- print_r ( $ staleFile [ ' migration ' ]);
82- $ migrator -> $ repository -> delete ($ staleFile [ ' migration ' ] );
83- }
85+ $ migrationRepository = $ migrator -> getRepository ( );
86+
87+ //Delete the stale migration classes from the database.
88+ $ stale -> each ( function ( $ class ) use ( $ migrationRepository ) {
89+ $ migrationRepository -> delete ($ class );
90+ });
8491 }
8592
8693 /**
@@ -96,6 +103,6 @@ protected function getStaleRecords(Collection $ran, array $available)
96103 {
97104 return $ filtered = collect ($ ran )->filter (function ($ migration ) use ($ available ) {
98105 return !in_array ($ migration ->migration , $ available );
99- })->toArray ( );
106+ })->pluck ( ' migration ' );
100107 }
101108}
0 commit comments