From da7aca7da40992a443ac93ce03f74e9b1531eb2b Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Thu, 14 Dec 2023 22:17:52 +0100 Subject: [PATCH] fix: Database Migration does set current version in DB #98 --- lib/Doctrine/Migration.php | 2 +- tests/DoctrineTest/Doctrine_UnitTestCase.php | 23 +++++++++++++-- tests/MigrationTestCase.php | 31 ++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/Migration.php b/lib/Doctrine/Migration.php index 0208ce540..615c792ec 100644 --- a/lib/Doctrine/Migration.php +++ b/lib/Doctrine/Migration.php @@ -559,4 +559,4 @@ protected function _createMigrationTable() return false; } } -} \ No newline at end of file +} diff --git a/tests/DoctrineTest/Doctrine_UnitTestCase.php b/tests/DoctrineTest/Doctrine_UnitTestCase.php index a1f0fb2f7..c1e20f935 100644 --- a/tests/DoctrineTest/Doctrine_UnitTestCase.php +++ b/tests/DoctrineTest/Doctrine_UnitTestCase.php @@ -141,13 +141,14 @@ public function init() if (count($e) > 3) { $driver = $e[2]; - switch($e[2]) { + + switch($driver) { case 'Mysql': case 'Mssql': case 'Oracle': case 'Pgsql': case 'Sqlite': - $this->driverName = $e[2]; + $this->driverName = $driver; break; } } @@ -226,7 +227,14 @@ public function prepareTables() { } } - $this->conn->export->exportClasses($this->tables); + + foreach ($this->tables as $table) { + try { + $this->conn->export->exportClasses(array($table)); + } catch (Doctrine_Export_Exception $e) { + } + } + $this->objTable = $this->connection->getTable('User'); } public function prepareData() @@ -306,6 +314,13 @@ public function getDeclaration($type) return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true)); } + protected function openAndBindMysqlConnection() + { + $this->dbh = new PDO('mysql:host=localhost;dbname=test', 'root'); + + $this->conn = $this->connection = $this->openAdditionalConnection($this->dbh); + } + protected function openAdditionalConnection($adapter = null, $name = null) { $connection = $this->manager->openConnection($adapter, $name); @@ -320,5 +335,7 @@ private function closeAdditionalConnections() foreach ($this->additionalConnections as $connection) { $this->manager->closeConnection($connection); } + + $this->conn = $this->connection = Doctrine_Manager::getInstance()->getCurrentConnection(); } } diff --git a/tests/MigrationTestCase.php b/tests/MigrationTestCase.php index 59b2b336c..4bb3dbe91 100644 --- a/tests/MigrationTestCase.php +++ b/tests/MigrationTestCase.php @@ -127,6 +127,37 @@ public function testMigrationClassNameInflected() $this->assertTrue($code); } } + + public function test_afterSuccessfullMigration_willSetMigratedVersionAsCurrentVersionInMysqlDB() + { + $this->openAndBindMysqlConnection(); + + parent::prepareTables(); + + $migration = new Doctrine_Migration('migration_classes'); + $migration->setCurrentVersion(3); + + $migration->migrate(4); + $this->assertEqual(4, $migration->getCurrentVersion()); + } + + public function test_afterFailedMigration_willKeepCurrentVersionInMysqlDB() + { + $this->openAndBindMysqlConnection(); + + parent::prepareTables(); + + $migration = new Doctrine_Migration('migration_classes'); + $migration->setCurrentVersion(0); + + try { + $migration->migrate(1); + + $this->fail('migration must fail'); + } catch (Doctrine_Migration_Exception $e) { + $this->assertEqual(0, $migration->getCurrentVersion()); + } + } } class MigrationPhonenumber extends Doctrine_Record