【发布时间】:2019-06-15 18:15:14
【问题描述】:
我有一个现有的 spring boot 项目和一个相同的数据库。现在,我想添加 liquibase 来处理进一步的数据库迁移。执行此操作的正确步骤是什么?
我已关注this article 添加 liquibase 并生成更改日志。我发现的大多数文章都在谈论从头开始在项目中使用 liquibase,或者对实现没有太详细的描述。到目前为止,我已经完成了以下工作:
在 pom.xml 中添加了依赖和插件
<dependencies>
//..other dependencies
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
</plugin>
</plugins>
</build>
在 src/main/resources 下添加 liquibase.properties 文件
url=jdbc:mysql://localhost:3306/demodb
username=root
password=root
driver=com.mysql.jdbc.Driver
outputChangeLogFile=src/main/resources/db/changelog/changes/demodb-changelog.xml
更新了 src/main/resources 下的 application.properties 文件以处理变更日志
#Hibernate
spring.datasource.url=jdbc:mysql://localhost:3306/demodb
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
#Jpa
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#Liquibase
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
在 src/main/resources/db/changelog 下创建了db.changelog-master.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
</databaseChangeLog>
运行 spring boot 应用程序,以便在数据库中创建两个新表 - DATABASECHANGELOG(此时为空)和 DATABASECHANGELOGLOCK(此时只有一个空条目)
从终端生成demodb-changelog.xml 文件以创建数据库当前状态的更改日志
mvn liquibase:generateChangeLog
然后在执行时同步当前的更改日志,在liquibase.properties 中添加:
changeLogFile=src/main/resources/db/changelog/changes/demodb-changelog.xml
然后从终端跑:
mvn liquibase:changelogSync
现在,DATABASECHANGELOG 表包含执行的变更日志条目。
接下来在db.changelog-master.xml文件中,添加生成的文件:
<include file="db/changelog/changes/demodb-changelog.xml"/>
现在,当我运行应用程序时,我得到了异常:
Caused by: liquibase.exception.MigrationFailedException:
Migration failed for change set db/changelog/changes/demodb-changelog.xml
Reason: liquibase.exception.DatabaseException: Table 'abc' already exists
因此,这是尝试再次运行更改日志文件。如何配置为仅运行那些尚未运行的变更集?我以为DATABASECHANGELOG的作用是处理已经执行的changeset,但我想我这里错了。
我可以在没有db.changelog-master.xml 中的include 标记的情况下运行该应用程序,但我想所有变更日志文件都需要在此处列出,因为如果我要在另一台机器上运行此应用程序,我将需要所有变更日志文件从头开始创建整个数据库。
那么如何配置 liquibase 只运行尚未执行的变更日志?
【问题讨论】:
-
您是否尝试将生成的文件包含在 changelog-master 中,然后运行 changeLogSync?
标签: spring-boot database-migration liquibase