@@ -28,6 +28,7 @@ class PhpmigApplication
2828 protected $ container ;
2929 protected $ output ;
3030 protected $ migrations ;
31+ protected $ adapter ;
3132
3233 public function __construct (\ArrayAccess $ container , OutputInterface $ output )
3334 {
@@ -50,6 +51,7 @@ public function __construct(\ArrayAccess $container, OutputInterface $output)
5051 }
5152
5253 $ this ->migrations = array_unique ($ migrations );
54+ $ this ->adapter = $ container ['phpmig.adapter ' ];
5355 }
5456
5557 /**
@@ -100,34 +102,63 @@ public function down($version = 0)
100102 */
101103 public function getMigrations ($ from , $ to = null )
102104 {
103- $ migrations = array ();
104-
105- if ($ to > $ from || $ to === null ) {
106- ksort ($ this ->migrations );
107- } else {
108- krsort ($ this ->migrations );
105+ $ to_run = array ();
106+
107+ $ migrations = $ this ->migrations ;
108+ $ versions = $ this ->adapter ->fetchAll ();
109+
110+ sort ($ versions );
111+
112+ $ direction = 'up ' ;
113+ if ($ to !== null ){
114+ $ direction = $ to > $ from ? 'up ' : 'down ' ;
109115 }
110-
111- foreach ($ this ->migrations as $ path ) {
112- preg_match ('/^[0-9]+/ ' , basename ($ path ), $ matches );
113- if (!array_key_exists (0 , $ matches )) {
114- continue ;
116+
117+
118+ if ($ direction == 'down ' ) {
119+ rsort ($ migrations );
120+
121+ foreach ($ migrations as $ path ) {
122+ preg_match ('/^[0-9]+/ ' , basename ($ path ), $ matches );
123+ if (!array_key_exists (0 , $ matches )) {
124+ continue ;
125+ }
126+
127+ $ version = $ matches [0 ];
128+
129+ if ($ version > $ from ) {
130+ continue ;
131+ }
132+ if ($ version <= $ to ) {
133+ continue ;
134+ }
135+
136+ if (in_array ($ version , $ versions )) {
137+ $ to_run [] = $ path ;
138+ }
115139 }
116-
117- $ version = $ matches [0 ];
118-
119- // up
120- if ($ to > $ from || $ to === null ) {
121- if ($ version > $ from && ($ version <= $ to || $ to === null )) {
122- $ migrations [] = $ path ;
140+ }else {
141+ sort ($ migrations );
142+ foreach ($ migrations as $ path ) {
143+ preg_match ('/^[0-9]+/ ' , basename ($ path ), $ matches );
144+ if (!array_key_exists (0 , $ matches )) {
145+ continue ;
146+ }
147+
148+ $ version = $ matches [0 ];
149+
150+ if ($ to !== null && ($ version > $ to )) {
151+ continue ;
152+ }
153+
154+ if (!in_array ($ version , $ versions )) {
155+ $ to_run [] = $ path ;
123156 }
124- // down
125- } elseif ($ to < $ from && $ version > $ to && $ version <= $ from ) {
126- $ migrations [] = $ path ;
127157 }
158+
128159 }
129-
130- return $ this ->loadMigrations ($ migrations );
160+
161+ return $ this ->loadMigrations ($ to_run );
131162 }
132163
133164 /**
0 commit comments