Skip to content

Question: User creation in a Flyway SQL migration #276

Closed
@lobodpav

Description

@lobodpav

The background

In our application, we have a versioned Flyway migration script that creates a user in all our environments using

CREATE USER a_new_user PASSWORD 'a_new_password'

When we run our integration tests using

@AutoConfigureEmbeddedDatabase(provider = AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY, refresh = AutoConfigureEmbeddedDatabase.RefreshMode.AFTER_EACH_TEST_METHOD)

all tests after the first one fail on

14:39:02.278 [postgres:pid(18371)] INFO  i.z.t.d.p.embedded.EmbeddedPostgres - 2024-08-01 14:39:02.278 CEST [18428] ERROR:  role "a_new_user" already exists
14:39:02.279 [postgres:pid(18371)] INFO  i.z.t.d.p.embedded.EmbeddedPostgres - 	CREATE USER a_new_user PASSWORD 'a_new_user'
14:39:02.282 [Test worker] WARN  o.s.test.context.TestContextManager - Caught exception while invoking 'beforeTestMethod' callback TestExecutionListener [ourApp.test.spock.IntegrationSpecExecutionListener] for test method [public void ourApp.ControllerIntegrationSpec.$spock_feature_0_0()] and test instance [ourApp.ControllerIntegrationSpec@2c4748af]
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction
Caused by: io.zonky.test.db.provider.ProviderException: Unexpected error when prefetching a database
Caused by: org.postgresql.util.PSQLException: ERROR: role "a_new_user" already exists

When debugging the io.zonky.test.db.context.DefaultDatabaseContext#refreshDatabase, the testPreparers contain loads of queries, with the CREATE USER a_new_user PASSWORD 'a_new_password' being one of those.

The question

Is it possible to somehow exclude user creation from becoming part of the template that is restored?

I am still unsure what the root cause is. I thought that the user in the template was restored to a new database where it can't conflict. Obviously, this is not the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions