首页 > 解决方案 > 使用 H2 和 Spring 引导进行 Liquibase 迁移

问题描述

我使用 Spring Boot 和 H2 DB。我想在我的应用程序中包含 Liquibase。我有多个模块应用程序。有entity、dao、service、controller等模块。我在 Dao 模块中创建了 Liquibase 文件。这是 application.property:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=password
spring.liquibase.change-log=classpath:db/migration/master.xml

这是 liquibase.property:

classpath=
changeLogFile=db/migration/master.xml
url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
username=sa
password=password
driver=org.h2.Driver

这是master.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

    <include file="v-1-0-0/changelog-project-v-1.0.0-cumulative.xml"/>
    <include file="v-1-0-1/changelog-project-v-1.0.1-cumulative.xml"/>
    
</databaseChangeLog>

我有一个错误:

Invocation of init method failed; nested exception is liquibase.exception.ChangeLogParseException: liquibase.exception.SetupException: The file v-1-0-0/changelog-project-v-1.0.0-cumulative.xml was not found in
    - Spring resources

但是,我认为我不需要在属性中指定来自 master.xml 的文件。请帮忙。 在此处输入图像描述

标签: springspring-bootliquibase

解决方案


看起来路径changelog-project-v-1.0.0-cumulative.xml不正确。尝试使用relativeToChangelogFile="true"

输入master.xml以下内容:

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

    <include file="v-1-0-0/changelog-project-v-1.0.0-cumulative.xml" relativeToChangelogFile="true"/>
    <include file="v-1-0-1/changelog-project-v-1.0.1-cumulative.xml" relativeToChangelogFile="true"/>
    
</databaseChangeLog>

推荐阅读