From 179df9eb39d20e4c53ed452ed2f4fb19fc278d2a Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Thu, 14 Dec 2023 22:17:52 +0100 Subject: [PATCH 01/12] 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 3fa7bcbcd..5715cceb8 100644 --- a/tests/DoctrineTest/Doctrine_UnitTestCase.php +++ b/tests/DoctrineTest/Doctrine_UnitTestCase.php @@ -142,13 +142,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; } } @@ -227,7 +228,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() @@ -307,6 +315,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(getenv('MYSQL_DSN')); + + $this->conn = $this->connection = $this->openAdditionalConnection($this->dbh); + } + protected function openAdditionalConnection($adapter = null, $name = null) { $connection = $this->manager->openConnection($adapter, $name); @@ -321,5 +336,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 7b718ba54..d5d286461 100644 --- a/tests/MigrationTestCase.php +++ b/tests/MigrationTestCase.php @@ -127,4 +127,35 @@ 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()); + } + } } From 884868ee231231d28379aa85bd7dbb43314bd198 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Sat, 13 Apr 2024 16:50:14 +0200 Subject: [PATCH 02/12] fixup! fix: Database Migration does set current version in DB #98 --- tests/DoctrineTest/Doctrine_UnitTestCase.php | 13 +++++++------ tests/MigrationTestCase.php | 12 +++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/DoctrineTest/Doctrine_UnitTestCase.php b/tests/DoctrineTest/Doctrine_UnitTestCase.php index 5715cceb8..c24abf976 100644 --- a/tests/DoctrineTest/Doctrine_UnitTestCase.php +++ b/tests/DoctrineTest/Doctrine_UnitTestCase.php @@ -229,12 +229,7 @@ public function prepareTables() { } } - foreach ($this->tables as $table) { - try { - $this->conn->export->exportClasses(array($table)); - } catch (Doctrine_Export_Exception $e) { - } - } + $this->conn->export->exportClasses($this->tables); $this->objTable = $this->connection->getTable('User'); } @@ -320,6 +315,12 @@ protected function openAndBindMysqlConnection() $this->dbh = new PDO(getenv('MYSQL_DSN')); $this->conn = $this->connection = $this->openAdditionalConnection($this->dbh); + + $this->exc = new Doctrine_Connection_Mysql_Exception(); + + $this->unitOfWork = $this->connection->unitOfWork; + $this->connection->setListener(new Doctrine_EventListener()); + $this->query = new Doctrine_Query($this->connection); } protected function openAdditionalConnection($adapter = null, $name = null) diff --git a/tests/MigrationTestCase.php b/tests/MigrationTestCase.php index d5d286461..689487087 100644 --- a/tests/MigrationTestCase.php +++ b/tests/MigrationTestCase.php @@ -32,13 +32,11 @@ */ class Doctrine_Migration_TestCase extends Doctrine_UnitTestCase { - public function prepareTables() - { - $this->tables[] = 'MigrationPhonenumber'; - $this->tables[] = 'MigrationUser'; - $this->tables[] = 'MigrationProfile'; - parent::prepareTables(); - } + protected $tables = array( + 'MigrationPhonenumber', + 'MigrationUser', + 'MigrationProfile', + ); public function testMigration() { From 3d88ef74dd03bdf2fbecbddb35083f55732debb5 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Sat, 13 Apr 2024 17:39:53 +0200 Subject: [PATCH 03/12] fixup! fix: Database Migration does set current version in DB #98 --- tests/DoctrineTest/Doctrine_UnitTestCase.php | 33 ++++++++++---------- tests/MigrationTestCase.php | 22 +++++++++---- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/tests/DoctrineTest/Doctrine_UnitTestCase.php b/tests/DoctrineTest/Doctrine_UnitTestCase.php index c24abf976..19427d370 100644 --- a/tests/DoctrineTest/Doctrine_UnitTestCase.php +++ b/tests/DoctrineTest/Doctrine_UnitTestCase.php @@ -217,22 +217,29 @@ public function init() } } } + public function prepareTables() { - foreach($this->tables as $name) { + $this->resetTablesOnConnection($this->tables, $this->connection); + + $this->objTable = $this->connection->getTable('User'); + } + + protected function resetTablesOnConnection(array $tables, Doctrine_Connection $connection) + { + foreach($tables as $name) { $name = ucwords($name); - $table = $this->connection->getTable($name); + $table = $connection->getTable($name); $query = 'DROP TABLE ' . $table->getTableName(); + try { - $this->conn->exec($query); + $connection->exec($query); } catch(Doctrine_Connection_Exception $e) { - } } - $this->conn->export->exportClasses($this->tables); - - $this->objTable = $this->connection->getTable('User'); + $connection->export->exportClasses($tables); } + public function prepareData() { $groups = new Doctrine_Collection($this->connection->getTable('Group')); @@ -310,17 +317,11 @@ public function getDeclaration($type) return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true)); } - protected function openAndBindMysqlConnection() + protected function openMysqlAdditionalConnection() { - $this->dbh = new PDO(getenv('MYSQL_DSN')); - - $this->conn = $this->connection = $this->openAdditionalConnection($this->dbh); + $dbh = new PDO(getenv('MYSQL_DSN')); - $this->exc = new Doctrine_Connection_Mysql_Exception(); - - $this->unitOfWork = $this->connection->unitOfWork; - $this->connection->setListener(new Doctrine_EventListener()); - $this->query = new Doctrine_Query($this->connection); + return $this->openAdditionalConnection($dbh); } protected function openAdditionalConnection($adapter = null, $name = null) diff --git a/tests/MigrationTestCase.php b/tests/MigrationTestCase.php index 689487087..2642f3ad5 100644 --- a/tests/MigrationTestCase.php +++ b/tests/MigrationTestCase.php @@ -128,11 +128,16 @@ public function testMigrationClassNameInflected() public function test_afterSuccessfullMigration_willSetMigratedVersionAsCurrentVersionInMysqlDB() { - $this->openAndBindMysqlConnection(); + $tables = array( + 'MigrationPhonenumber', + 'MigrationUser', + 'MigrationProfile', + ); - parent::prepareTables(); + $connection = $this->openMysqlAdditionalConnection(); + $this->resetTablesOnConnection($tables, $connection); - $migration = new Doctrine_Migration('migration_classes'); + $migration = new Doctrine_Migration('migration_classes', $connection); $migration->setCurrentVersion(3); $migration->migrate(4); @@ -141,11 +146,16 @@ public function test_afterSuccessfullMigration_willSetMigratedVersionAsCurrentVe public function test_afterFailedMigration_willKeepCurrentVersionInMysqlDB() { - $this->openAndBindMysqlConnection(); + $tables = array( + 'MigrationPhonenumber', + 'MigrationUser', + 'MigrationProfile', + ); - parent::prepareTables(); + $connection = $this->openMysqlAdditionalConnection(); + $this->resetTablesOnConnection($tables, $connection); - $migration = new Doctrine_Migration('migration_classes'); + $migration = new Doctrine_Migration('migration_classes', $connection); $migration->setCurrentVersion(0); try { From 18f8d35f32176de053754aa6c6ac36d6f393bb29 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Sat, 13 Apr 2024 17:45:18 +0200 Subject: [PATCH 04/12] fixup! fix: Database Migration does set current version in DB #98 --- tests/MigrationTestCase.php | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/tests/MigrationTestCase.php b/tests/MigrationTestCase.php index 2642f3ad5..310fa237a 100644 --- a/tests/MigrationTestCase.php +++ b/tests/MigrationTestCase.php @@ -32,12 +32,14 @@ */ class Doctrine_Migration_TestCase extends Doctrine_UnitTestCase { - protected $tables = array( + const TABLES = array( 'MigrationPhonenumber', 'MigrationUser', 'MigrationProfile', ); + protected $tables = self::TABLES; + public function testMigration() { $migration = new Doctrine_Migration('migration_classes'); @@ -128,14 +130,8 @@ public function testMigrationClassNameInflected() public function test_afterSuccessfullMigration_willSetMigratedVersionAsCurrentVersionInMysqlDB() { - $tables = array( - 'MigrationPhonenumber', - 'MigrationUser', - 'MigrationProfile', - ); - $connection = $this->openMysqlAdditionalConnection(); - $this->resetTablesOnConnection($tables, $connection); + $this->resetTablesOnConnection(self::TABLES, $connection); $migration = new Doctrine_Migration('migration_classes', $connection); $migration->setCurrentVersion(3); @@ -146,14 +142,8 @@ public function test_afterSuccessfullMigration_willSetMigratedVersionAsCurrentVe public function test_afterFailedMigration_willKeepCurrentVersionInMysqlDB() { - $tables = array( - 'MigrationPhonenumber', - 'MigrationUser', - 'MigrationProfile', - ); - $connection = $this->openMysqlAdditionalConnection(); - $this->resetTablesOnConnection($tables, $connection); + $this->resetTablesOnConnection(self::TABLES, $connection); $migration = new Doctrine_Migration('migration_classes', $connection); $migration->setCurrentVersion(0); From 88cfed5de4a98d7f0760bee31b8fa6e58073c4a0 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Sat, 13 Apr 2024 17:48:55 +0200 Subject: [PATCH 05/12] fixup! fix: Database Migration does set current version in DB #98 --- tests/DoctrineTest/Doctrine_UnitTestCase.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/DoctrineTest/Doctrine_UnitTestCase.php b/tests/DoctrineTest/Doctrine_UnitTestCase.php index 19427d370..15deba9c0 100644 --- a/tests/DoctrineTest/Doctrine_UnitTestCase.php +++ b/tests/DoctrineTest/Doctrine_UnitTestCase.php @@ -338,7 +338,5 @@ private function closeAdditionalConnections() foreach ($this->additionalConnections as $connection) { $this->manager->closeConnection($connection); } - - $this->conn = $this->connection = Doctrine_Manager::getInstance()->getCurrentConnection(); } } From 5897f6d1dda7cc0bfa9f5de7ec437dfba60fa8d1 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Sat, 13 Apr 2024 18:07:48 +0200 Subject: [PATCH 06/12] fixup! fix: Database Migration does set current version in DB #98 --- tests/MigrationTestCase.php | 43 ++++++------------------------------- tests/run.php | 1 + 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/tests/MigrationTestCase.php b/tests/MigrationTestCase.php index 310fa237a..7b718ba54 100644 --- a/tests/MigrationTestCase.php +++ b/tests/MigrationTestCase.php @@ -32,13 +32,13 @@ */ class Doctrine_Migration_TestCase extends Doctrine_UnitTestCase { - const TABLES = array( - 'MigrationPhonenumber', - 'MigrationUser', - 'MigrationProfile', - ); - - protected $tables = self::TABLES; + public function prepareTables() + { + $this->tables[] = 'MigrationPhonenumber'; + $this->tables[] = 'MigrationUser'; + $this->tables[] = 'MigrationProfile'; + parent::prepareTables(); + } public function testMigration() { @@ -127,33 +127,4 @@ public function testMigrationClassNameInflected() $this->assertTrue($code); } } - - public function test_afterSuccessfullMigration_willSetMigratedVersionAsCurrentVersionInMysqlDB() - { - $connection = $this->openMysqlAdditionalConnection(); - $this->resetTablesOnConnection(self::TABLES, $connection); - - $migration = new Doctrine_Migration('migration_classes', $connection); - $migration->setCurrentVersion(3); - - $migration->migrate(4); - $this->assertEqual(4, $migration->getCurrentVersion()); - } - - public function test_afterFailedMigration_willKeepCurrentVersionInMysqlDB() - { - $connection = $this->openMysqlAdditionalConnection(); - $this->resetTablesOnConnection(self::TABLES, $connection); - - $migration = new Doctrine_Migration('migration_classes', $connection); - $migration->setCurrentVersion(0); - - try { - $migration->migrate(1); - - $this->fail('migration must fail'); - } catch (Doctrine_Migration_Exception $e) { - $this->assertEqual(0, $migration->getCurrentVersion()); - } - } } diff --git a/tests/run.php b/tests/run.php index d1dd2c7ee..b458896b6 100644 --- a/tests/run.php +++ b/tests/run.php @@ -279,6 +279,7 @@ // Migration Tests $migration = new GroupTest('Migration Tests', 'migration'); $migration->addTestCase(new Doctrine_Migration_TestCase()); +$migration->addTestCase(new Doctrine_Migration_Mysql_TestCase()); $migration->addTestCase(new Doctrine_Migration_Base_TestCase()); $migration->addTestCase(new Doctrine_Migration_Diff_TestCase()); $test->addTestCase($migration); From e43ec5a06a670b4c707dc57f586f3dce4f0e7ab9 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Sat, 13 Apr 2024 18:21:37 +0200 Subject: [PATCH 07/12] fixup! fix: Database Migration does set current version in DB #98 --- .github/workflows/continuous-integration.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 26b4f3b23..0904617d2 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -36,6 +36,15 @@ jobs: - "8.2" - "8.3" + services: + mysql: + image: mysql:5.5.62 + env: + MYSQL_DATABASE: test + MYSQL_ROOT_PASSWORD: testrootpass + ports: + - 3306:3306 + steps: - name: Checkout uses: actions/checkout@v3 @@ -62,4 +71,6 @@ jobs: run: composer install --prefer-dist - name: Run Tests + env: + MYSQL_DSN: 'mysql:host=127.0.0.1;dbname=test;user=root;pass=testrootpass' run: cd tests && php run.php From efc5ad130a7186b935e887d30d2dcebff01a2b05 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Sat, 13 Apr 2024 18:29:43 +0200 Subject: [PATCH 08/12] fixup! fix: Database Migration does set current version in DB #98 --- lib/Doctrine/Migration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/Migration.php b/lib/Doctrine/Migration.php index 615c792ec..0208ce540 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 From 612966a988ef8718c95a4af62b84ea3fc5b193be Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Fri, 24 May 2024 19:23:09 +0200 Subject: [PATCH 09/12] fixup! fix: Database Migration does set current version in DB #98 --- tests/Migration/MysqlTestCase.php | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/Migration/MysqlTestCase.php diff --git a/tests/Migration/MysqlTestCase.php b/tests/Migration/MysqlTestCase.php new file mode 100644 index 000000000..955382bbd --- /dev/null +++ b/tests/Migration/MysqlTestCase.php @@ -0,0 +1,76 @@ +. + */ + +/** + * Doctrine_Migration_TestCase + * + * @package Doctrine + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.doctrine-project.org + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Migration_Mysql_TestCase extends Doctrine_UnitTestCase +{ + private $migration; + + const TABLES = array( + 'MigrationPhonenumber', + 'MigrationUser', + 'MigrationProfile', + ); + + protected $tables = self::TABLES; + + public function setUp() + { + parent::setUp(); + + $connection = $this->openMysqlAdditionalConnection(); + $this->resetTablesOnConnection(self::TABLES, $connection); + + $this->migration = new Doctrine_Migration('migration_classes', $connection); + } + + public function test_afterSuccessfullMigration_willSetMigratedVersionAsCurrentVersionInMysqlDB() + { + $this->migration->setCurrentVersion(3); + + $this->migration->migrate(4); + + $this->assertEqual(4, $this->migration->getCurrentVersion()); + } + + public function test_afterFailedMigration_willKeepCurrentVersionInMysqlDB() + { + $this->migration->setCurrentVersion(0); + + try { + $this->migration->migrate(1); + + $this->fail('migration must fail'); + } catch (Doctrine_Migration_Exception $e) { + $this->assertEqual(0, $this->migration->getCurrentVersion()); + } + } +} From da5a25d3f801577c8983ccaddc55d09636e6c5af Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Fri, 24 May 2024 19:36:34 +0200 Subject: [PATCH 10/12] fixup! fix: Database Migration does set current version in DB #98 --- .github/workflows/continuous-integration.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 0904617d2..768de7701 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -72,5 +72,5 @@ jobs: - name: Run Tests env: - MYSQL_DSN: 'mysql:host=127.0.0.1;dbname=test;user=root;pass=testrootpass' + MYSQL_DSN: 'mysql:host=127.0.0.1;dbname=test;user=root;password=testrootpass' run: cd tests && php run.php From ae17cc61c977f5691f6280207ee9f81b1141dc53 Mon Sep 17 00:00:00 2001 From: Kyle McGrogan Date: Fri, 5 Apr 2024 11:34:31 -0400 Subject: [PATCH 11/12] Update the export command to use a PHP8-safe PDO transaction commit path --- lib/Doctrine/Export.php | 2 +- .../Transaction/TransactionHelper.php | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 lib/Doctrine/Transaction/TransactionHelper.php diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index ab56d6620..e14be577e 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -1221,7 +1221,7 @@ public function exportClasses(array $classes) } } - $connection->commit(); + Doctrine_Transaction_Helper::commitIfInTransaction($connection); } } diff --git a/lib/Doctrine/Transaction/TransactionHelper.php b/lib/Doctrine/Transaction/TransactionHelper.php new file mode 100644 index 000000000..d88e6ec67 --- /dev/null +++ b/lib/Doctrine/Transaction/TransactionHelper.php @@ -0,0 +1,47 @@ +. + */ + +/** + * Doctrine_Transaction_Helper + * + * @package Doctrine + * @subpackage Transaction + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 1.4 + * @version $Revision$ + * @author Kyle McGrogan + */ +final class Doctrine_Transaction_Helper +{ + public static function commitIfInTransaction(Doctrine_Connection $connection): void + { + $handler = $connection->getDbh(); + + // Attempt to commit while no transaction is running results in exception since PHP 8 + pdo_mysql combination + if ($handler instanceof PDO && !$handler->inTransaction()) { + return; + } + + $connection->commit(); + } +} \ No newline at end of file From ac331ed93394e8c3a3e9bc6dc2fd2e606c9aaab8 Mon Sep 17 00:00:00 2001 From: Kyle McGrogan Date: Mon, 8 Apr 2024 11:35:02 -0400 Subject: [PATCH 12/12] Relocate helper class so it's found by the autoloader --- lib/Doctrine/Export.php | 2 +- lib/Doctrine/{Transaction => }/TransactionHelper.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename lib/Doctrine/{Transaction => }/TransactionHelper.php (97%) diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index e14be577e..f09610451 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -1221,7 +1221,7 @@ public function exportClasses(array $classes) } } - Doctrine_Transaction_Helper::commitIfInTransaction($connection); + Doctrine_TransactionHelper::commitIfInTransaction($connection); } } diff --git a/lib/Doctrine/Transaction/TransactionHelper.php b/lib/Doctrine/TransactionHelper.php similarity index 97% rename from lib/Doctrine/Transaction/TransactionHelper.php rename to lib/Doctrine/TransactionHelper.php index d88e6ec67..6cc606606 100644 --- a/lib/Doctrine/Transaction/TransactionHelper.php +++ b/lib/Doctrine/TransactionHelper.php @@ -31,7 +31,7 @@ * @version $Revision$ * @author Kyle McGrogan */ -final class Doctrine_Transaction_Helper +final class Doctrine_TransactionHelper { public static function commitIfInTransaction(Doctrine_Connection $connection): void {