doctrine / migrations

Doctrine Database Migrations Library
https://www.doctrine-project.org/projects/migrations.html
MIT License
4.65k stars 387 forks source link

$plannedSql shared between commands execution. #1402

Open R3VoLuT1OneR opened 4 months ago

R3VoLuT1OneR commented 4 months ago

Bug Report

Used version 2.x now after migration to 3.x I am found this problem. I am using doctrine migration in my PHPUnit test suite.

On each setUp I am running running migration:

migrations:migrate

And then in each tearDown I have:

migrations:migrate first

In my migration up method I have some INSERT SQL queries. In my down I have only DROP TABLE.

Q A
BC Break yes
Version 3.7.2

Summary

Basically executed queries shared between commands, if we reuse the "Application".

It's happens because migration objects kept in memory and $plannedSql is not clear between commands execution. So migration executor appends new queries from down function to $plannedSql and then executes queries from prev up function call and later queries from down call.

Current behavior

On the doctrine:migrate first running in the tearDown the INSERT from the up method executed and after that the DROP TABLE exectued.

How to reproduce

$configuration = new Configuration();
$configuration->addMigrationsDirectory(
    'App\Migrations',
    __DIR__ . '/migrations'
);

$dependencyFactory = DependencyFactory::fromConnection(
    configurationLoader: new ExistingConfiguration($configuration),
    connectionLoader: new ExistingConnection(static::connection()),
);

$migrationsApplication = ConsoleRunner::createApplication(
    dependencyFactory: $dependencyFactory
);

$migrationsApplication->setAutoExit(false);

// Migration
$migrationsApplication->run(new StringInput('migrations:migrate --no-interaction'));

// Rollback
$migrationsApplication->run(new StringInput('migrations:migrate first --no-interaction'));

Expected behavior

No SQL quries from the first migrations executed on migrations:migrate first, only DROP TABLE no INSERT.