liip / LiipTestFixturesBundle

This bundles enables efficient loading of Doctrine fixtures in functional test-cases for Symfony applications
https://liip.ch
MIT License
166 stars 45 forks source link

SQLSTATE[HY000]: General error: 1 table messenger_messages already exists #103

Open lsv opened 3 years ago

lsv commented 3 years ago

After an upgrade from symfony 4.4 to symfony 5.1 with all the doctrine dependencies, and also an remove of liip/functional-test-bundle and instead install of liip/test-fixtures-bundle

Some dependencies that have been upgraded is

doctrine/doctrine-migrations-bundle
doctrine/doctrine-fixtures-bundle
symfony/messenger

My tests began to output a lot of SQLSTATE[HY000]: General error: 1 table messenger_messages already exists all over the place.

Now for my migrations to not add messenger_messages table I have the following doctrine config

# config/packages/doctrine.yaml
doctrine:
    dbal:
        connections:
            default:
                schema_filter: ~^(?!messenger_messages)~

So to fix my table already exists I had to add the following to my doctrine test yaml file

# config/packages/test/doctrine.yaml

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                schema_filter: ~

And now my tests are running fine.

Im not actually sure this is a bug in either doctrine or liip, but just wanted to add it somewhere, so it can be googled - because I had no relevant google results coming up.

alexislefebvre commented 3 years ago

You can use that setting to disable the automatic creation of tables:

liip_test_fixtures:
    keep_database_and_schema: true
lsv commented 3 years ago

I actually thinks this is the reason. https://github.com/symfony/symfony/pull/40336

Ill try and test it at a later point.

alexislefebvre commented 3 years ago

@lsv Did the fix in Symfony solved your problem?

kolotov commented 2 years ago

@alexislefebvre I have same problem if I use automatic creation of tables.

alexislefebvre commented 2 years ago

@kolotov I don't have any other solution right one than using keep_database_and_schema: true.

WubbleWobble commented 2 years ago

Here's my research into this.

I have a set of tests, the first test runs fine, and then the next test blows up trying to create messenger_messages, which already exists.

Ultimate cause:

ORMSqliteDatabaseTool - lines 92-94 - dropDatabase and createSchema are not symmetrical (createSchema has a "create table messenger_messages", but dropDatabase doesn't have a corresponding drop table line)

Problem overview

Cause

Looks like these SchemaSubscribers were added to symfony/doctrine-bridge in 5.1 (SchemaListener directory appears at 5.1) https://github.com/symfony/doctrine-bridge/tree/4.4 https://github.com/symfony/doctrine-bridge/tree/5.1

Thoughts

Conclusion

Not much I can do to workaround - this is caused by code in symfony/doctrine-bridge, and behaviour could be adjusted if I could hook into SchemaTool, but that's in doctrine/orm.

I'm going to have to do the whole keep_databases_and_schema thing, right? :/

alexislefebvre commented 2 years ago

@WubbleWobble thanks for the detailed report!

Calling setExcludedDoctrineTables(['messenger_messages']) may help here, please see the doc: https://github.com/liip/LiipTestFixturesBundle/blob/2.x/doc/database.md#exclude-some-tables-

Update: actually it looks like it's only used when purging the tables, it may not help here.

WubbleWobble commented 2 years ago

@alexislefebvre Thanks - I found that method, but as you say - it didn't actually help :)

I would open a bug report with doctrine and/or symfony, but honestly I'm quite confused by many aspects of it, and where the "blame" (for lack of a better word) lies. It's going to take some more investigation I think!

I ended up using keep_databases_and_schema: true, and used a phpunit bootstrap file to run the various Symfony console commands needed to set up the test database.

Ceriusz commented 1 year ago

You can use that setting to disable the automatic creation of tables:

liip_test_fixtures:
    keep_database_and_schema: true

Sorry but I'm not sure where should I put this yaml configuration - should I create a new file config/packages/liip.yaml? Because I don't see any yaml regarding liip configuration under config/packages.

alexislefebvre commented 1 year ago

@Ceriusz yes, there is an example of path here: https://github.com/liip/LiipTestFixturesBundle/blob/2.x/doc/configuration.md

glaplace commented 1 year ago

Hello,

I have the same problem.

As a workaround, I deleted the global transport configuration and added it only to the dev and prod environments.

For testing I used test://.

example

## Configuration messenger
## Commun environment configuration
framework:
  messenger:
    routing:
      'App\Message\AsyncMessageInterface': async
    buses:
      command_bus:
        middleware:
          - router_context
    transports:
      async:
        options:
          queue_name: aqueuename

# prod environment
when@prod:
  framework:
    messenger:
      transports:
        async: 'doctrine://default'

# Test environment
when@test:
  framework:
    messenger:
      transports:
        async: 'test://'

I hope this can help someone.