diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 26b4f3b23..768de7701 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;password=testrootpass' run: cd tests && php run.php diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index ab56d6620..f09610451 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -1221,7 +1221,7 @@ public function exportClasses(array $classes) } } - $connection->commit(); + Doctrine_TransactionHelper::commitIfInTransaction($connection); } } diff --git a/lib/Doctrine/TransactionHelper.php b/lib/Doctrine/TransactionHelper.php new file mode 100644 index 000000000..6cc606606 --- /dev/null +++ b/lib/Doctrine/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_TransactionHelper +{ + 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 diff --git a/tests/DoctrineTest/Doctrine_UnitTestCase.php b/tests/DoctrineTest/Doctrine_UnitTestCase.php index 3fa7bcbcd..15deba9c0 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; } } @@ -216,20 +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')); @@ -307,6 +317,13 @@ public function getDeclaration($type) return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true)); } + protected function openMysqlAdditionalConnection() + { + $dbh = new PDO(getenv('MYSQL_DSN')); + + return $this->openAdditionalConnection($dbh); + } + protected function openAdditionalConnection($adapter = null, $name = null) { $connection = $this->manager->openConnection($adapter, $name); 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()); + } + } +} 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);