How to handle DCL in Flyway migration scripts?

666 views Asked by At

I have a postgres database that has users and roles already defined. There are multiple schemas in this database that are all controlled via different projects/flyway scripts. I am working on adding Flyway integration into a new project where we will use an embedded Postgres instance for testing.

Since none of these users/roles will exist on this instance, they need to be created in a migration script. However, since these users/roles will already exist in my operational databases, the migrations will fail when they attempt to create the roles.

I already considered writing a function for this, but then the function would have to be included in any project that uses the embedded Postgres and would have to be maintained across multiple code bases. This seems very sloppy. Can anyone recommend a way for me to handle these DCL operations using Flyway that will work with the embedded approach as well as my operational databases?

1

There are 1 answers

3
Florian E. On

In a previous project we use for this approach a set of additional Flyway migration scripts. These scripts we add to the test environment classpath. We used this for a Flyway version before the feature of Callback and repeatable migrations were added.

Add a callback configuration for your Test environment and you add in the before or after migration phase your user and roles.

Third solution use repeatable migration scripts for your user and roles setup see https://flywaydb.org/documentation/migration/repeatable. Use this scripts in production and test. But in this case your sql must done correct and repeatable otherwise you will break your production environment.