Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"There is no active transaction" error after successfully performing migrations #393

Closed
bartfeenstra opened this issue Jan 4, 2021 · 14 comments

Comments

@bartfeenstra
Copy link
Contributor

bartfeenstra commented Jan 4, 2021

Problem

When performing migrations, the process ends with "There is no active transaction" thrown in Connection.php on line 1761. The migrations themselves are performed successfully: the database schema appears to be altered as expected, and when re-running migrations Doctrine is aware that all migrations have been run successfully previously already.

The problem occurred for the first time after the migration from PHP 7.4 to 8.0.0.

The problem occurs when performing migrations through the doctrine:migrations:migrate command, as well as when performed in code by dama/doctrine-test-bundle.

Update 2021-01-05

This may be a pdo_mysql issue. See https://twitter.com/derrabus/status/1346405883429924870

Package versions

  • doctrine/migrations 3.0.2
  • doctrine/doctrine-migrations-bundle 3.0.2
  • doctrine/dbal 2.12.1

Related issues

@dlgoodchild
Copy link

dlgoodchild commented Jan 7, 2021

I've just experienced this now myself on PHP v8.0.0.
I changed no composer dependencies and simply rolled the container back to 7.4.10 and the migrations worked.

-zsh

Executing migrations on PHP 8 currently does not work, however I believe it likely relates to doctrine/migrations as that's what's common between myself @bartfeenstra

Package versions

  • doctrine/orm 2.8.1
  • doctrine/migrations 3.0.2

@lukasluecke
Copy link

A temporary workaround would be to override isTransactional() to return false - if not having a transaction is acceptable for your migrations.

@greg0ire
Copy link
Member

greg0ire commented Jan 7, 2021

Duplicate of doctrine/migrations#1104

@greg0ire greg0ire marked this as a duplicate of doctrine/migrations#1104 Jan 7, 2021
@greg0ire greg0ire closed this as completed Jan 7, 2021
@yyaremenko
Copy link

yyaremenko commented Jun 2, 2021

IF YOU STILL GET THIS ERROR

if you use Doctrine Migrations Bundle
make sure to update the bundle to ^3.1.1 version (but first update the doctrine migrations library);
for both of these, run this single command

composer update doctrine/migrations && composer require doctrine/doctrine-migrations-bundle:^3.1.1

if you use Doctrine Migrations directly, without bundle, as prompted by @azjezz

If you are using doctrine migrations 2, run the following:

composer require doctrine/migrations:^2.3.3

If you are using doctrine migrations 3, run the following:

composer require doctrine/migrations:^3.1.1

@azjezz
Copy link

azjezz commented Jun 2, 2021

@yyaremenko the proposed solution works even if you are using the bundle, the bug is in the doctrine/migrations lib, not the bundle :)

@yyaremenko
Copy link

@azjezz yes, I agree and I understand, but the idea with the bundle is that you don't require the library directly in your composer.json; if I use your solution while using bundle, I get the following in my composer.json

...
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/migrations": "^3.1.1",
...

The things may get messy quite fast.

@garak
Copy link
Contributor

garak commented Jun 15, 2021

I started to get this error too (since I migrated to PHP 8), but I don't use doctrine migrations.
I just execute:

<?php
/** @var \Doctrine\DBAL\Connection $connection */
$connection->beginTransaction();
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
$connection->executeStatement($sql);
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
$connection->commit();

Is there a place in Doctrine where I can look for?

@greg0ire
Copy link
Member

I think you might be able to reproduce it in pure PDO too. If that's true, you will understand that there is not much we can do.

@garak
Copy link
Contributor

garak commented Jun 15, 2021

I think you might be able to reproduce it in pure PDO too. If that's true, you will understand that there is not much we can do.

I see. Is there any issue elsewhere I can follow?

@greg0ire
Copy link
Member

I don't think there is anything to follow, because there is nothing we should do IMO. You can get an in-depth explanation about implicit commits here though: https://github.com/doctrine/migrations/pull/1157/files

@garak
Copy link
Contributor

garak commented Jun 15, 2021

Thank @greg0ire. I solved by splitting my sql (one with table deletion/creation and one with everything else)

@ingfdoaguirre
Copy link

ingfdoaguirre commented Feb 13, 2022

I started to get this error too (since I migrated to PHP 8), but I don't use doctrine migrations. I just execute:

<?php
/** @var \Doctrine\DBAL\Connection $connection */
$connection->beginTransaction();
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
$connection->executeStatement($sql);
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
$connection->commit();

Is there a place in Doctrine where I can look for?

Hi, maybe is not the correct solution or the one that everyone wants, but you can use PDO directly with DDL and run exec() (AS A TEMPORARY SOLUTION).

use PDO;
use PDOException;

try{
    $dsn = 'mysql:host=testhost;dbname=testname;charset=testchartset';
    $pdo = new PDO($dsn, 'testuser' , 'testpass');
    $sql = 'CREATE TABLE ..............';
    $pdo->exec($sql);
} catch (PDOException $e){
    return $e->getMessage();
}

And there will be no "There is no active transaction" error code. I'm using php 8.1 and Symfony 6.0.3

You can outwit me or be upset and say "this is an antipattern", but it works.

@greg0ire
Copy link
Member

Well it does work if you have one statement, but as soon as you have 2 of them, is no longer does. As for the outwitting, if you use MySQL or Oracle, that means you cannot use DDL in a transaction, so… maybe, you know, just don't?

@Nek-
Copy link

Nek- commented Aug 31, 2023

@greg0ire would a PR adding the option transactional in the bundle config be accepted?

(Mb, I saw it's already done, just not compatible with dbal 2.x 🙈 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants