Skip to content

Commit feb66c9

Browse files
Merge pull request #149 from PaulMcRoar/master
API migrate up issue resolution
2 parents 60fd20e + f19bc1e commit feb66c9

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

src/Phpmig/Api/PhpmigApplication.php

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

tests/Phpmig/Api/PhpmigApplicationTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,17 @@ public function testGetMigrations()
104104
);
105105

106106
// up
107-
$this->assertCount(3, $app->getMigrations(0, $this->next_version));
108-
$this->assertCount(3, $app->getMigrations(0, null));
107+
$this->assertCount(2, $app->getMigrations(0, $this->next_version));
108+
$this->assertCount(2, $app->getMigrations(0, null));
109109
$this->assertCount(2, $app->getMigrations($this->prev_version, $this->next_version));
110-
$this->assertCount(1, $app->getMigrations($this->current_version, $this->next_version));
110+
$this->assertCount(2, $app->getMigrations($this->current_version, $this->next_version));
111111
$this->assertCount(0, $app->getMigrations($this->next_version, $this->next_version));
112112

113113
// down
114-
$this->assertCount(1, $app->getMigrations($this->next_version, $this->current_version));
114+
$this->assertCount(0, $app->getMigrations($this->next_version, $this->current_version));
115115
$this->assertCount(1, $app->getMigrations($this->current_version, $this->prev_version));
116-
$this->assertCount(2, $app->getMigrations($this->next_version, $this->prev_version));
117-
$this->assertCount(3, $app->getMigrations($this->next_version, 0));
116+
$this->assertCount(1, $app->getMigrations($this->next_version, $this->prev_version));
117+
$this->assertCount(1, $app->getMigrations($this->next_version, 0));
118118
}
119119

120120
/**

0 commit comments

Comments
 (0)