Running databasemigration with Grails IntegrationTest run - is it possible

26 Views Asked by At

So this is the problem I'm trying to work around:

I'm working with an old grails project using PosgreSQL, including GORM. The domain classes are as minimal as GORM wants them to be - so ID management is abstracted away into the default stuff and isn't spec'd in the domain classes at all.

The issue with this is that dbCreate (create-drop, FWIW) doesn't generate the hibernate_sequence sequence that it uses by default.

This has been fixed/bodged/compensated for with a liquibase changelog that creates it in other scenarios, but I'm trying to get H2 set up for IT tests. I can create a persisted H2 db and manually add hibernate_sequence to get everything to work, but I'm struggling to find a way to get that to work when the IT tests are run.

I had tried to add (in application.yml):

grails:
  plugin:
    databasemigration:
      updateOnStart: true
      updateOnStartFileNames: ['changelog.groovy']
      autoMigrateScripts: ['TestApp']

changelog.groovy exists in grails-app/migrations, and grails-app/migrations is configured as a sourceSet in build.gradle. All other aspects are correct and in place (dependencies etc.)

To check whether liquibase is actually running, I have set the changelog to execute trash SQL - which should cause an error - but it's instead failing on Sequence "HIBERNATE_SEQUENCE" not found, so is skimming over the issue.

Is what I'm attempting possible, or is there another way to make dbCreate generate the sequences itself? I can't risk changing how it has been run for fear of changing how prod works, and I've attempted all sorts of hibernate config in application.yml, but I've run into a brick wall.

1

There are 1 best solutions below

0
practical programmer On

So, sometimes the simplest solutions may be the best. Would adding data.sql file to src/integration-test/resources work for you? This file is automatically recognized by Spring and executed after or before hibernate ddl is called - depending on your Spring Boot version and additional configuration options. It can contain any SQL script to be executed, in your case sequence creation.

More details are available here: https://www.baeldung.com/spring-boot-data-sql-and-schema-sql https://docs.spring.io/spring-boot/docs/current/reference/html/data.html