Liquibase不会从changeSet内部的类路径加载* .sql

时间:2019-02-13 15:26:13

标签: java sql-server spring-data-jpa liquibase

我需要配置执行从jar加载的sql的changeSet。

我有一个内部项目changeSet,可以正常运行

<changeSet id="1" author="sergii" dbms="h2">
  <sqlFile
    encoding="utf8"
    path="schema-ms-sql.0.0.1.sql"
    relativeToChangelogFile="true"
    splitStatements="true"
    stripComments="true"/>
</changeSet>

某些脚本是从不同的库提供的(在我的情况下是spring-boot-starter-batch),例如:

classpath:/org/springframework/batch/core/schema-h2.sql

请注意,jar在项目中并且可以访问(构建\测试\运行时间)。 结果,我还需要在changeSet中注册一个,尝试:

<changeSet id="2" author="sergii" dbms="h2">
  <sqlFile
    encoding="utf8"
    path="classpath*:/org/springframework/batch/core/schema-h2.sql"
    relativeToChangelogFile="true"
    splitStatements="true"
    stripComments="true"/>
</changeSet>

,由于以下原因,它不适用于任何配置(例如"classpath:/org/springframework/batch/core/schema-h2.sql""/org/springframework/batch/core/schema-h2.sql""org/springframework/batch/core/schema-h2.sql""classpath*:/org/springframework/batch/core/schema-h2.sql"等)

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.UnexpectedLiquibaseException: java.io.IOException: File does not exist: 'classpath*:/org/springframework/batch/core/schema-h2.sql'

我知道使用spring可以使用自动配置,但是我对liquibase审核感兴趣...

是否有任何想法如何使打包的脚本通过changeSet工作或纳入liquibase审核?

2 个答案:

答案 0 :(得分:1)

解决方案是更改sqlFile标签的属性:

relativeToChangelogFile="false"

下面的结果changeSet

<changeSet id="2" author="sergii" dbms="h2">
  <sqlFile
    encoding="utf8"
    path="classpath:/org/springframework/batch/core/schema-h2.sql"
    relativeToChangelogFile="false"
    splitStatements="true"
    stripComments="true"/>
</changeSet>

答案 1 :(得分:1)

具有相对路径的单独 changelog.yaml 文件的示例:

databaseChangeLog:
  - changeSet:
      id: my_script_1
      changes:
          - sqlFile:
                dbms: mysql
                encoding: utf8
                path: db/changelog/0_0_1/my_script_1.sql    
                relativeToChangelogFile: false
                splitStatements: true
                stripComments: true
      rollback:
          - sqlFile:
                dbms: mysql
                encoding: utf8
                path: db/changelog/0_0_1/my_script_1_rollback.sql
                relativeToChangelogFile: false
                splitStatements: true
                stripComments: true
  - changeSet:
      id: my_script_2
      changes:
        - sqlFile:
            dbms: mysql
            encoding: utf8
            path: db/changelog/0_0_1/my_script_2.sql
            relativeToChangelogFile: false
            splitStatements: true
            stripComments: true
      rollback:
        - sqlFile:
            dbms: mysql
            encoding: utf8
            path: db/changelog/0_0_1/my_script_2_rollback.sql
            relativeToChangelogFile: false
            splitStatements: true
            stripComments: true