@@ -182,16 +182,16 @@ private static function scanDirectoryFiles($directory)
182182 array_walk ($ files , function (&$ value , $ index ) use ($ directory ) {
183183 $ value = rtrim ($ directory , '/ ' ) . "/ {$ value }" ;
184184 });
185-
186- // sort alphabetically (ensures ads comes before ads_data, users before users_address, etc.)
187- // sort($files);
188-
189- // self::orderMigrationsByFK($files)
190185
191186 return self ::sortParentFiles ($ files );
192- return $ files ;
193187 }
194188
189+ /**
190+ * Sort parent files according to their names
191+ *
192+ * @param array $files
193+ * @return void
194+ */
195195 private static function sortParentFiles (array $ files )
196196 {
197197 // Custom sort: parent tables before child tables
@@ -216,72 +216,6 @@ private static function sortParentFiles(array $files)
216216 return $ files ;
217217 }
218218
219- // Very compact dependency-based ordering
220- private static function orderMigrationsByFK (array $ files ): array
221- {
222- $ tableOf = [];
223- $ deps = []; // table => set of referenced tables
224-
225- $ getFile = function (string $ f ): string {
226- return \Tamedevelopers \Support \Capsule \File::get ($ f ) ?? '' ;
227- };
228-
229- // 1) map file -> table and dependencies
230- foreach ($ files as $ f ) {
231- $ src = $ getFile ($ f );
232- if (!preg_match ("/Schema::create \\([' \"]([a-zA-Z0-9_]+)[' \"]/i " , $ src , $ m )) continue ;
233- $ table = $ m [1 ];
234- $ tableOf [$ table ] = $ f ;
235-
236- preg_match_all ("/->constrained \\([' \"]([a-zA-Z0-9_]+)[' \"]/i " , $ src , $ mm );
237- $ refs = array_unique ($ mm [1 ] ?? []);
238- $ deps [$ table ] = $ refs ;
239- }
240-
241- // 2) Kahn's topological sort (by tables)
242- $ inDeg = [];
243- foreach ($ deps as $ t => $ rs ) {
244- $ inDeg [$ t ] ??= 0 ;
245- foreach ($ rs as $ r ) {
246- // only count if referenced table is being created in this set
247- if (isset ($ deps [$ r ])) {
248- $ inDeg [$ t ] += 1 ;
249- }
250- }
251- }
252-
253- $ q = [];
254- foreach ($ inDeg as $ t => $ d ) if ($ d === 0 ) $ q [] = $ t ;
255-
256- $ ordered = [];
257- while ($ q ) {
258- $ t = array_shift ($ q );
259- $ ordered [] = $ t ;
260- foreach ($ deps as $ u => $ rs ) {
261- if (in_array ($ t , $ rs , true )) {
262- $ inDeg [$ u ] -= 1 ;
263- if ($ inDeg [$ u ] === 0 ) $ q [] = $ u ;
264- }
265- }
266- }
267-
268- // Append any tables not captured (no FKs or parsing edge cases)
269- foreach (array_keys ($ tableOf ) as $ t ) {
270- if (!in_array ($ t , $ ordered , true )) $ ordered [] = $ t ;
271- }
272-
273- // Return files in dependency order
274- $ out = [];
275- foreach ($ ordered as $ t ) {
276- if (isset ($ tableOf [$ t ])) $ out [] = $ tableOf [$ t ];
277- }
278- // Preserve non-create migration files (if any)
279- foreach ($ files as $ f ) if (!in_array ($ f , $ out , true )) $ out [] = $ f ;
280-
281- return $ out ;
282- }
283-
284-
285219 /**
286220 * Create API Response
287221 * @return \Tamedevelopers\Support\Collections\Collection
0 commit comments