jooq-modelator icon indicating copy to clipboard operation
jooq-modelator copied to clipboard

Leftover schema definitions in Postgres after Liquibase migration

Open romasch opened this issue 7 years ago • 2 comments

When running migrations with Liquibase, Postgres and multiple schemas, the database is not properly cleaned up after each run.

Reproduction steps:

  1. Set up example project with the following Liquibase databaseChangeLog.yml:
databaseChangeLog:
    - changeSet:
        id: 1
        author: test
        changes: [sql: {sql: "create schema test"}]
#    - changeSet:
#        id: 2
#        author: test
#        changes: [empty: {}]
  1. Run liquibase migration.
  2. Uncomment the second changeset and run again. => Change Set scripts/databaseChangeLog.yml::1::test failed. Error: ERROR: schema "test" already exists [Failed SQL: create schema test]

The problem is the statement liquibase.dropAll(), which only deletes the default (public) schema.

There are multiple solutions to this:

  1. Never reuse a container, instead create a new one every time (and delete it afterwards).
  2. Add plugin configuration options for schemas to be deleted in Liquibase.
  3. Never delete the database in an existing container, but instead add a task to delete the container from Gradle.

Personally I think the first solution is the easiest with least risk of future headache. There might be a slight performance impact, but since the plugin only runs when there are actual changes to the changelog this might not be such an issue.

romasch avatar Oct 15 '18 20:10 romasch

Thanks for you feedback, and your suggestions!

I will take some time, and try to incorporate support for schemas. If it proves too difficult, option nr. 1 seems the way to go.

ayedo avatar Oct 16 '18 17:10 ayedo

Follow up: I spent some time trying to implement the schema migrations. It seems to be trickier that just passing along another parameter to the Flyway, and Liquibase instances.. (see here. I think I will implement forced container recreation as soon as I find the time to do so.

ayedo avatar Oct 17 '18 08:10 ayedo