3838
3939use CodeIgniter \Config \BaseConfig ;
4040use CodeIgniter \ConfigException ;
41+ use Config \Autoload ;
4142
4243/**
4344 * Class MigrationRunner
@@ -115,7 +116,7 @@ public function __construct(BaseConfig $config, ConnectionInterface $db = null)
115116 $ this ->type = $ config ->type ?? 'timestamp ' ;
116117 $ this ->table = $ config ->table ?? 'migrations ' ;
117118 $ this ->currentVersion = $ config ->currentVersion ?? 0 ;
118- $ this ->path = $ config ->path ?? APPPATH . 'Database/Migrations/ ' ;
119+ $ this ->path = $ config ->path ?? 'Database/Migrations/ ' ;
119120
120121 $ this ->path = rtrim ($ this ->path , '/ ' ).'/ ' ;
121122
@@ -166,15 +167,6 @@ public function version(string $targetVersion, $group='default')
166167 // Note: We use strings, so that timestamp versions work on 32-bit systems
167168 $ currentVersion = $ this ->getVersion ($ group );
168169
169- if ($ this ->type === 'sequential ' )
170- {
171- $ targetVersion = sprintf ('%03d ' , $ targetVersion );
172- }
173- else
174- {
175- $ targetVersion = (string )$ targetVersion ;
176- }
177-
178170 $ migrations = $ this ->findMigrations ();
179171
180172 if ($ targetVersion > 0 && ! isset ($ migrations [$ targetVersion ]))
@@ -263,11 +255,11 @@ public function latest()
263255 throw new \RuntimeException (lang ('Migrations.migNotFound ' ));
264256 }
265257
266- $ lastMigration = basename (end ($ migrations ));
258+ $ lastMigration = basename (end ($ migrations ), ' .php ' );
267259
268260 // Calculate the last migration step from existing migration
269261 // filenames and proceed to the standard version migration
270- return $ this ->version ($ this -> getMigrationNumber ( $ lastMigration) );
262+ return $ this ->version ($ lastMigration );
271263 }
272264
273265 //--------------------------------------------------------------------
@@ -293,25 +285,31 @@ public function findMigrations()
293285 {
294286 $ migrations = [];
295287
296- // Load all *_*.php files in the migrations path
297- foreach (glob ($ this ->path .'*_*.php ' ) as $ file )
298- {
299- $ name = basename ($ file , '.php ' );
300-
301- // Filter out non-migration files
302- if (preg_match ($ this ->regex , $ name ))
303- {
304- $ number = $ this ->getMigrationNumber ($ name );
305-
306- // There cannot be duplicate migration numbers
307- if (isset ($ migrations [$ number ]))
308- {
309- throw new \RuntimeException (lang ('Migrations.migMultiple ' ).$ number );
310- }
311-
312- $ migrations [$ number ] = $ file ;
313- }
314- }
288+ $ config = new Autoload ();
289+
290+ // Loop through all of our namespaced folders
291+ // searching for migration directories.
292+ foreach ($ config ->psr4 as $ namespace => $ dir )
293+ {
294+ $ dir = rtrim ($ dir , '/ ' ).'/ ' .$ this ->path ;
295+
296+ if (! is_dir ($ dir ))
297+ {
298+ continue ;
299+ }
300+
301+ // Load all *_*.php files in the migrations path
302+ foreach (glob ($ dir .'*_*.php ' ) as $ file )
303+ {
304+ $ name = basename ($ file , '.php ' );
305+
306+ // Filter out non-migration files
307+ if (preg_match ($ this ->regex , $ name ))
308+ {
309+ $ migrations [$ name ] = $ file ;
310+ }
311+ }
312+ }
315313
316314 ksort ($ migrations );
317315
0 commit comments