无法使用 liquibase 填充数据库

Cannot populate database with liquibase

我正在构建一个小型 spring-boot 应用程序。我正在使用 spring-data-jpa 创建数据库模式,并使用 liquibase 用测试数据填充它。

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/book-db
spring.datasource.driver-class-name=org.postgresql.Driver

spring.datasource.username=admin
spring.datasource.password=lTIDDYz3n3jD3BeYaAJz
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

根据文档,如果我在默认路径下有 gradle 依赖项和主 changeLog,则不需要配置 liquibase。

db.changelog-master.yaml:

databaseChangeLog:
      - changeSet:
            id: 1
            author: jb
            changes:
            - sqlFile:
                  path: db/migration/insert-books.sql

插入-books.sql:

--liquibase formatted sql
--changeset admin:1
delete from book;
insert into book (id, title)
values (nextval('seq'), 'Functional Programming for Mortals');
commit;

有无commit我都试过了。 tables databasechangelogdatabasechangelog 已成功创建并包含迁移(插入书籍)。

迁移完成,因为如果我添加无效插入(对某些 table 不存在),我会得到异常:

ERROR: relation "xxx" does not exist

如何使用 liquibase 在 insert-books.sql 脚本中用数据填充数据库?

不要同时使用 liquibase 和 JPA 来管理数据库结构。如果您想使用 liquibase,请将 JPA (Hibernate) 设置为仅验证模式并在 liquibase 中管理模式。

spring.jpa.hibernate.ddl-auto=validate

您的解决方案的问题在于操作顺序。当您的应用程序启动时,它首先运行插入数据的 liquibase,然后启动 JPA 并从头开始创建模式。

尝试在 运行 应用程序之前删除架构,我敢打赌迁移 (liquibase) 会失败。

Liquibase 必须负责模式,有一种方法可以将 liquibase 添加到现有数据库,但它再次使 liquibase 成为模式的所有者:

Using liquibase on the existing database