grails / grails-database-migration

Grails® framework Database Migration Plugin
Apache License 2.0
98 stars 115 forks source link

Unable to load classpath resources for sqlFile changesets #169

Open acanby opened 4 years ago

acanby commented 4 years ago

I'm trying to integrate Spring Batch into my Grails application. The Spring Batch jar supplies per dbms schemas for the required tables, and I have been trying to add this via dbm/liquibase. It seems like something that liquibase supports, and I have seen examples for vanilla Spring Boot/Liquibase that seem to work.

I have a changelog as follows:

databaseChangeLog = {
    changeSet(author: "user", id: "spring-batch-schema-4.2.1.RELEASE", dbms: 'mssql') {
        sqlFile(encoding: "utf8",
                splitStatements: true,
                stripComments: true,
                relativeToChangelogFile: false,
                path: 'classpath:/org/springframework/batch/core/schema-sqlserver.sql'
        )
    }
}

This seems like it should work - but I get the below exception:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-31 13:05:50.125 ERROR --- [  restartedMain] <> o.s.boot.SpringApplication               : Application run failed

liquibase.exception.UnexpectedLiquibaseException: java.io.IOException: File does not exist: 'classpath:/org/springframework/batch/core/schema-sqlserver.sql'
        at liquibase.change.AbstractSQLChange.generateCheckSum(AbstractSQLChange.java:189)
        at liquibase.changelog.ChangeSet.generateCheckSum(ChangeSet.java:255)
        at liquibase.changelog.ChangeSet.toString(ChangeSet.java:807)
        at liquibase.changelog.ChangeLogIterator.createKey(ChangeLogIterator.java:105)
        at liquibase.changelog.ChangeLogIterator.alreadySaw(ChangeLogIterator.java:113)
        at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:77)
        at liquibase.changelog.DatabaseChangeLog.validate(DatabaseChangeLog.java:269)
        at liquibase.Liquibase.update(Liquibase.java:198)
        at liquibase.Liquibase.update(Liquibase.java:179)
        at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:317)
        at org.grails.plugins.databasemigration.liquibase.GrailsLiquibase.performUpdate(GrailsLiquibase.groovy:83)

So I did some digging into the exception. A change was added in https://github.com/grails-plugins/grails-database-migration/commit/7e46268f22f72e91d2bf60a577a5c29ebc28e48a#diff-9735b2073ff7adabaa25c8f0085a81f2 that overrode the createLiquibase method from the default SpringLiquibase one. It changes the resourceAccessor to the parent of the one used by SpringLiquibase (i.e. removing all Spring awareness).

I'd like to propose changing the resourceAccessor back to the Spring one. I've made the code changes: https://github.com/grails-plugins/grails-database-migration/commit/b5ca95257efad2482598a8ebf8e5765ad3adcafa. Using this code I can now load the file from the Spring Batch Jar.

If this change was made for a specific reason, I can understand the hesitation to change the implementation for everyone. I'd like to have the option of creating my own GrailsLiquibase instances, but the plugin specifically uses new. Perhaps this could be exposed via a bean factory? Code here: https://github.com/acanby/grails-database-migration/commit/3a1e05174153b2e3ec2c9e5d16702142e806a575.

andrewcanby-finocomp commented 3 years ago

Any thoughts on this one @puneetbehl?