@@ -37,24 +37,20 @@ public function handle()
3737 return 1 ;
3838 }
3939
40- // At first, we will check if the database does not contain any table
41- // then run the `migrate` command.
42- if (empty ($ this ->getTables ())) {
43- $ this ->components ->task (
44- 'Migrating your database schema ' ,
45- fn () => $ this ->call ('migrate ' , ['--force ' => true ])
46- );
47- }
40+ $ this ->components ->task (
41+ 'Migrating your database schema ' ,
42+ fn () => $ this ->call ('migrate ' , ['--force ' => true ])
43+ );
4844
49- $ migrationsInfo = $ this ->getMigrationsInfo (
45+ $ migrations = $ this ->getMigrations (
5046 explode (", " , $ this ->argument ("table " ))
5147 );
5248
5349 $ this ->components ->info ('Preparing database. ' );
5450
5551 $ this ->components ->task ('Dropping the tables ' , $ this ->dropTables (
56- $ migrationsInfo ["tableNames " ],
57- $ migrationsInfo ["migrationNames " ]
52+ $ migrations ["tableNames " ],
53+ $ migrations ["migrationNames " ]
5854 ));
5955
6056 $ this ->call ('migrate ' , ['--force ' => true ]);
@@ -63,7 +59,7 @@ public function handle()
6359 }
6460
6561 /**
66- * Get an array of all the tables in the database.
62+ * Get the listed tables in the database.
6763 *
6864 * @return array
6965 */
@@ -73,45 +69,73 @@ protected function getTables()
7369 }
7470
7571 /**
76- * Get an array of the migration file names and correct table names .
72+ * Get the correct table names with their migration .
7773 *
7874 * @param array $tablesToBeDropped
7975 * @return array
8076 */
81- protected function getMigrationsInfo (array $ tablesToBeDropped )
77+ protected function getMigrations (array $ tablesToBeDropped )
8278 {
8379 $ migrationsPath = database_path ('migrations ' );
84- $ migrationsInfo = ["migrationNames " => [], "tableNames " => []];
8580
86- // In addition, we will filter the given array of tables, and then iterate over
87- // each one and check if the passed table exists to get its relevant migrations.
81+ // At first, we will filter the given array of tables, and then iterate over
82+ // each one to check if the passed table has a migration then we will check if
83+ // the `tableNames` has been set by the `getMigrationsInfo` method or not
84+ // because if it is set, it means the table is there
85+ // or we will ask the developer to choose the correct table instead.
8886 foreach (array_filter ($ tablesToBeDropped ) as $ index => $ table ) {
89- $ migrationsInfo = $ this ->checkMigrationFileExistence (
90- $ migrationsPath ,
91- $ table ,
92- $ migrationsInfo
93- );
87+ $ migrationsInfo = $ this ->getMigrationsInfo ($ migrationsPath , $ table );
9488
9589 if (empty ($ migrationsInfo ["tableNames " ][$ index ])) {
96- $ choiceValue = $ this ->choice (
90+ $ value = $ this ->choice (
9791 "Choose the correct table instead ( {$ table }) " ,
9892 array_diff (
9993 $ this ->getTables (),
10094 array_merge ($ migrationsInfo ["tableNames " ], ["migrations " ])
10195 )
10296 );
10397
104- $ migrationsInfo = $ this ->checkMigrationFileExistence (
105- $ migrationsPath ,
106- $ choiceValue ,
107- $ migrationsInfo
108- );
98+ $ migrationsInfo = $ this ->getMigrationsInfo ($ migrationsPath , $ value );
10999 }
110100 }
111101
112102 return $ migrationsInfo ;
113103 }
114104
105+ /**
106+ * Get the migrations that match the given table.
107+ *
108+ * @param string $migrationsPath
109+ * @param string $table
110+ * @return array
111+ */
112+ protected function getMigrationsInfo (string $ migrationsPath , string $ table )
113+ {
114+ $ migrationsInfo = ["migrationNames " => [], "tableNames " => []];
115+
116+ if (!empty ($ migrationName = glob ("{$ migrationsPath }/*_create_ {$ table }_table.php " ))) {
117+ array_push ($ migrationsInfo ["tableNames " ], $ table );
118+ array_push ($ migrationsInfo ["migrationNames " ], basename ($ migrationName [0 ]));
119+ } elseif (!empty ($ migrationName = glob ("{$ migrationsPath }/*_create_ {$ table }.php " ))) {
120+ array_push ($ migrationsInfo ["tableNames " ], $ table );
121+ array_push ($ migrationsInfo ["migrationNames " ], basename ($ migrationName [0 ]));
122+ }
123+
124+ if (!empty ($ migrationName = glob ("{$ migrationsPath }/*_to_ {$ table }_table.php " ))) {
125+ array_push ($ migrationsInfo ["migrationNames " ], basename ($ migrationName [0 ]));
126+ }
127+
128+ if (!empty ($ migrationName = glob ("{$ migrationsPath }/*_from_ {$ table }_table.php " ))) {
129+ array_push ($ migrationsInfo ["migrationNames " ], basename ($ migrationName [0 ]));
130+ }
131+
132+ if (!empty ($ migrationName = glob ("{$ migrationsPath }/*_in_ {$ table }_table.php " ))) {
133+ array_push ($ migrationsInfo ["migrationNames " ], basename ($ migrationName [0 ]));
134+ }
135+
136+ return $ migrationsInfo ;
137+ }
138+
115139 /**
116140 * Drop all tables except the given array of table names.
117141 *
@@ -128,9 +152,8 @@ protected function dropTables(array $tableNames, array $migrationNames)
128152 ->insert (["migration " => substr_replace ($ migration , "" , -4 ), "batch " => 1 ]);
129153 }
130154
131- $ droppedTables = array_map ("current " , DB ::select ("SHOW TABLES " ));
132155 $ droppedTables = array_diff (
133- $ droppedTables ,
156+ $ this -> getTables () ,
134157 array_merge (array_filter ($ tableNames ), ["migrations " ])
135158 );
136159
@@ -140,55 +163,4 @@ protected function dropTables(array $tableNames, array $migrationNames)
140163 Schema::dropIfExists ($ table );
141164 }
142165 }
143-
144- /**
145- * Check if the given migration file exists.
146- *
147- * @param string $migrationsPath
148- * @param string $table
149- * @param array $migrationsInfo
150- * @return array
151- */
152- protected function checkMigrationFileExistence (
153- string $ migrationsPath ,
154- string $ table ,
155- array $ migrationsInfo
156- ) {
157- if (!empty ($ migrationFileName = glob ("{$ migrationsPath }/*_create_ {$ table }_table.php " ))) {
158- array_push ($ migrationsInfo ["tableNames " ], $ table );
159- array_push (
160- $ migrationsInfo ["migrationNames " ],
161- basename ($ migrationFileName [0 ])
162- );
163- } elseif (!empty ($ migrationFileName = glob ("{$ migrationsPath }/*_create_ {$ table }.php " ))) {
164- array_push ($ migrationsInfo ["tableNames " ], $ table );
165- array_push (
166- $ migrationsInfo ["migrationNames " ],
167- basename ($ migrationFileName [0 ])
168- );
169- }
170-
171- if (!empty ($ migrationFileName = glob ("{$ migrationsPath }/*_to_ {$ table }_table.php " ))) {
172- array_push (
173- $ migrationsInfo ["migrationNames " ],
174- basename ($ migrationFileName [0 ])
175- );
176- }
177-
178- if (!empty ($ migrationFileName = glob ("{$ migrationsPath }/*_from_ {$ table }_table.php " ))) {
179- array_push (
180- $ migrationsInfo ["migrationNames " ],
181- basename ($ migrationFileName [0 ])
182- );
183- }
184-
185- if (!empty ($ migrationFileName = glob ("{$ migrationsPath }/*_in_ {$ table }_table.php " ))) {
186- array_push (
187- $ migrationsInfo ["migrationNames " ],
188- basename ($ migrationFileName [0 ])
189- );
190- }
191-
192- return $ migrationsInfo ;
193- }
194166}
0 commit comments