Skip to content

Commit da7aca7

Browse files
alquercithePanz
authored andcommitted
fix: Database Migration does set current version in DB #98
1 parent 0df2b8b commit da7aca7

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

lib/Doctrine/Migration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,4 @@ protected function _createMigrationTable()
559559
return false;
560560
}
561561
}
562-
}
562+
}

tests/DoctrineTest/Doctrine_UnitTestCase.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,14 @@ public function init()
141141

142142
if (count($e) > 3) {
143143
$driver = $e[2];
144-
switch($e[2]) {
144+
145+
switch($driver) {
145146
case 'Mysql':
146147
case 'Mssql':
147148
case 'Oracle':
148149
case 'Pgsql':
149150
case 'Sqlite':
150-
$this->driverName = $e[2];
151+
$this->driverName = $driver;
151152
break;
152153
}
153154
}
@@ -226,7 +227,14 @@ public function prepareTables() {
226227

227228
}
228229
}
229-
$this->conn->export->exportClasses($this->tables);
230+
231+
foreach ($this->tables as $table) {
232+
try {
233+
$this->conn->export->exportClasses(array($table));
234+
} catch (Doctrine_Export_Exception $e) {
235+
}
236+
}
237+
230238
$this->objTable = $this->connection->getTable('User');
231239
}
232240
public function prepareData()
@@ -306,6 +314,13 @@ public function getDeclaration($type)
306314
return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
307315
}
308316

317+
protected function openAndBindMysqlConnection()
318+
{
319+
$this->dbh = new PDO('mysql:host=localhost;dbname=test', 'root');
320+
321+
$this->conn = $this->connection = $this->openAdditionalConnection($this->dbh);
322+
}
323+
309324
protected function openAdditionalConnection($adapter = null, $name = null)
310325
{
311326
$connection = $this->manager->openConnection($adapter, $name);
@@ -320,5 +335,7 @@ private function closeAdditionalConnections()
320335
foreach ($this->additionalConnections as $connection) {
321336
$this->manager->closeConnection($connection);
322337
}
338+
339+
$this->conn = $this->connection = Doctrine_Manager::getInstance()->getCurrentConnection();
323340
}
324341
}

tests/MigrationTestCase.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,37 @@ public function testMigrationClassNameInflected()
127127
$this->assertTrue($code);
128128
}
129129
}
130+
131+
public function test_afterSuccessfullMigration_willSetMigratedVersionAsCurrentVersionInMysqlDB()
132+
{
133+
$this->openAndBindMysqlConnection();
134+
135+
parent::prepareTables();
136+
137+
$migration = new Doctrine_Migration('migration_classes');
138+
$migration->setCurrentVersion(3);
139+
140+
$migration->migrate(4);
141+
$this->assertEqual(4, $migration->getCurrentVersion());
142+
}
143+
144+
public function test_afterFailedMigration_willKeepCurrentVersionInMysqlDB()
145+
{
146+
$this->openAndBindMysqlConnection();
147+
148+
parent::prepareTables();
149+
150+
$migration = new Doctrine_Migration('migration_classes');
151+
$migration->setCurrentVersion(0);
152+
153+
try {
154+
$migration->migrate(1);
155+
156+
$this->fail('migration must fail');
157+
} catch (Doctrine_Migration_Exception $e) {
158+
$this->assertEqual(0, $migration->getCurrentVersion());
159+
}
160+
}
130161
}
131162

132163
class MigrationPhonenumber extends Doctrine_Record

0 commit comments

Comments
 (0)