在 spring 中使用 liquibase 创建数据库模式不起作用

问题描述 投票:0回答:1

在低于 2.5.0 的 spring boot 版本中,我在 application.properties 中有这个配置:

spring.datasource.initialization-mode=always
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb?currentSchema=myschema
spring.datasource.username=root
spring.datasource.password=pass
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
spring.liquibase.default-schema=myschema
spring.liquibase.liquibase-schema=myschema

另外,在 schema.sql 文件中,我有这样一个查询来在数据库中创建一个模式:

CREATE SCHEMA IF NOT EXISTS myschema;

在 2.5.0 的 spring boot 中,配置字段:

spring.datasource.initialization-mode=always

应替换为:

spring.sql.init.mode=always

不幸的是,应用 2.5.0 版的这些更改后,liquibase 解决方案无法按预期工作,并且未创建数据库模式。是什么原因造成的或如何处理?

出现的错误是:

2022-10-13 15:34:18.741  INFO 28767 --- [           main] liquibase.database                       : Set default schema name to null
2022-10-13 15:34:18.748  WARN 28767 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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.LockException: liquibase.exception.DatabaseException: ERROR: no schema has been selected to create in
  Pozycja: 14 [Failed SQL: (0) CREATE TABLE databasechangeloglock (ID INTEGER NOT NULL, LOCKED BOOLEAN NOT NULL, LOCKGRANTED TIMESTAMP WITHOUT TIME ZONE, LOCKEDBY VARCHAR(255), CONSTRAINT databasechangeloglock_pkey PRIMARY KEY (ID))]

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.LockException: liquibase.exception.DatabaseException: ERROR: schema "myschema" does not exist
  Pozycja: 14 [Failed SQL: (0) CREATE TABLE myschema.databasechangeloglock (ID INTEGER NOT NULL, LOCKED BOOLEAN NOT NULL, LOCKGRANTED TIMESTAMP WITHOUT TIME ZONE, LOCKEDBY VARCHAR(255), CONSTRAINT databasechangeloglock_pkey PRIMARY KEY (ID))]

我想到的唯一解决方案是在应用程序第一次启动时关闭liqibase,然后正常创建模式,但我想获得帮助,我该如何解决?

java database postgresql spring-boot liquibase
1个回答
-3
投票

您找到该错误的解决方案了吗?

© www.soinside.com 2019 - 2024. All rights reserved.