Java 如何将Liquibase与Spring Boot JPA和Maven结合使用来管理来自JPA实体的迁移
我一直试图在SpringJPA项目上使用来管理迁移,就像Python/Django在Java 如何将Liquibase与Spring Boot JPA和Maven结合使用来管理来自JPA实体的迁移,java,spring-boot,hibernate,jpa,liquibase-hibernate,Java,Spring Boot,Hibernate,Jpa,Liquibase Hibernate,我一直试图在SpringJPA项目上使用来管理迁移,就像Python/Django在makemigrations中所做的那样。Liquibase非常令人困惑,我试着按照我在网上找到的一些SA答案和教程进行操作,但都没有效果 以下是我想做的: 通过JPA注释类定义实体 生成迁移以在MySQL数据库上反映这些实体 在数据库上应用所述迁移。(最初为空) 到目前为止,我能够生成一个“diff”changeLog文件,并设法让liquibase在空数据库上创建databasechangelog和datab
makemigrations
中所做的那样。Liquibase非常令人困惑,我试着按照我在网上找到的一些SA答案和教程进行操作,但都没有效果
以下是我想做的:
databasechangelog
和databasechangeloglock
表。但是,我无法使用diff更新master.xml
文件,也无法将diff应用于数据库
以下是我目前的配置(基本spring jpa项目,只有一个实体用于测试):
Pom.xml(缩写)
我还添加了一个空的changelog-master.xml文件,其中包含(不包括名称空间):
但是现在,我该如何应用这个呢?如何使diff命令将这个特定的diff“附加”到主变更日志中?运行mvn liquibase:update
不会执行任何操作。此外,数据库上的changelog表仍然为空
编辑:
我尝试手动将差异添加到主文件,如下所示:
<databaseChangeLog>
<include file="classpath:db/diff-changelog.xml" />
</databaseChangeLog>
在运行changelogSync
和update
之后,我设法在databasechangelog
表中获得了一条记录。但是,未创建新表
谢谢你的帮助,
也许如果你想让你感到困惑的话,你应该考虑使用另一个迁移工具。我个人使用flyway,它非常直观且易于使用。Flyway只使用SQL,这似乎非常适合MySQL请查看。您可以查看我编写的演示: 在这里,我使用纯liquibase核心库和.xml方法(我个人更喜欢它而不是.yml,因为它对我来说更可读) 如何运行:
.sql文件的文档:根据数据库变更日志,Liquibase将应用尚未应用的变更日志。如果您现在添加了变更日志,则必须通过运行
liquibas:update
来应用更改。如果我从干净的数据库运行update,它只会生成内部liquibase表。我应该运行的命令的确切顺序是什么?也许liquibase不知道这个主文件?您必须以某种方式配置它的位置。Flyway无法从JPA实体生成迁移文件。你必须用手去做,这违背了目的。
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=none
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/my-db?useSSL=false
spring.datasource.username = user
spring.datasource.password = pass
spring.liquibase.enabled=true
spring.liquibase.url=jdbc:mysql://localhost:3306/my-db?useSSL=false
spring.liquibase.user=user
spring.liquibase.password=pass
spring.liquibase.change-log=classpath:db/changelog-master.xml
logging.level.org.springframework=INFO
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.generate_statistics=true
<databaseChangeLog />
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog>
<changeSet author="tiago (generated)" id="1618352585193-1">
<createTable tableName="chunk">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true" primaryKeyName="chunkPK"/>
</column>
<column name="x_origin" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="y_origin" type="BIGINT">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
<databaseChangeLog>
<include file="classpath:db/diff-changelog.xml" />
</databaseChangeLog>