@@ -25,7 +25,6 @@ class Migration extends Command
2525 public function __construct (Context $ context , Stdio $ stdio , CI_Controller $ ci )
2626 {
2727 parent ::__construct ($ context , $ stdio , $ ci );
28- $ this ->load ->library ('migration ' );
2928 $ this ->load ->config ('migration ' );
3029 }
3130
@@ -49,15 +48,26 @@ public function __invoke($type, $classname)
4948 $ migration_type = $ this ->config ->item ('migration_type ' );
5049
5150 if ($ migration_type === 'sequential ' ) {
52- $ files = $ this ->migration ->find_migrations ();
53- if (empty ($ files )) {
54- $ version = 0 ;
55- } else {
56- end ($ files );
57- $ version = key ($ files );
51+
52+ $ migrations = array ();
53+
54+ // fax max version
55+ foreach (glob ($ migration_path . '*_*.php ' ) as $ file ) {
56+ $ name = basename ($ file , '.php ' );
57+
58+ if (preg_match ('/^\d{3}_(\w+)$/ ' , $ name )) {
59+ $ number = sscanf ($ name , '%[0-9]+ ' , $ number ) ? $ number : '0 ' ;
60+ $ migrations [] = $ number ;
61+ }
62+ }
63+
64+ $ version = 0 ;
65+
66+ if ($ migrations !== []) {
67+ $ version = max ($ migrations );
5868
5969 // check max version
60- if ($ version == 999 ) {
70+ if (( int ) $ version = == 999 ) {
6171 $ this ->stdio ->errln (
6272 "<<red>>The version number is out of range<<reset>> "
6373 );
@@ -69,8 +79,6 @@ public function __invoke($type, $classname)
6979 $ file_path = $ migration_path . date ('YmdHis ' ) . '_ ' . $ classname . '.php ' ;
7080 }
7181
72- $ file_path = $ migration_path . date ('YmdHis ' ) . '_ ' . $ classname . '.php ' ;
73-
7482 // check file exist
7583 if (file_exists ($ file_path )) {
7684 $ this ->stdio ->errln (
@@ -83,8 +91,7 @@ public function __invoke($type, $classname)
8391 foreach (glob ($ migration_path . '*_*.php ' ) as $ file ) {
8492 $ name = basename ($ file , '.php ' );
8593
86- // use date('YmdHis') so...
87- if (preg_match ('/^\d{14}_(\w+)$/ ' , $ name , $ match )) {
94+ if (preg_match ($ migration_type === 'timestamp ' ? '/^\d{14}_(\w+)$/ ' : '/^\d{3}_(\w+)$/ ' , $ name , $ match )) {
8895 if (strtolower ($ match [1 ]) === strtolower ($ classname )) {
8996 $ this ->stdio ->errln (
9097 "<<red>>The Class \"$ classname \" already exists<<reset>> "
0 commit comments