gpt4 book ai didi

grails - Grails 3数据库迁移

转载 作者:行者123 更新时间:2023-12-02 14:59:51 25 4
gpt4 key购买 nike

我将应用程序更新为grails 3.1.9,但数据库迁移插件遇到问题。

我的生产application.yml看起来像这样:

   production:
dataSource:
driverClassName: org.postgresql.Driver
dialect: org.hibernate.dialect.PostgreSQLDialect
dbCreate: none
url: jdbc:postgresql://localhost:5432/something
username: postgres
password: postgres
properties:

我的build.gradle看起来像这样:
buildscript {
dependencies {
classpath 'org.grails.plugins:database-migration:2.0.0.RC4'
}
}

dependencies {
compile 'org.liquibase:liquibase-core:3.4.1'
compile 'org.grails.plugins:database-migration:2.0.0.RC4'
}

这是变更日志的开始:
databaseChangeLog = {

changeSet(author: "michal (generated)", id: "1472650791344-1") {
createTable(tableName: "appointment") {
column(autoIncrement: "true", name: "id", type: "BIGINT") {
constraints(primaryKey: "true", primaryKeyName: "appointmentPK")
}

column(name: "version", type: "BIGINT")

column(name: "customer_id", type: "BIGINT")

column(name: "duration", type: "BLOB(255)")

column(name: "note", type: "CLOB")

column(defaultValueComputed: "0", name: "personal_available", type: "BOOLEAN")

column(defaultValueComputed: "0", name: "personal_booked", type: "BOOLEAN")

column(name: "provider_id", type: "BIGINT")

column(name: "start_time", type: "BLOB(255)")

column(name: "url", type: "VARCHAR(255)")
}
}

在生产模式下运行应用程序时,出现此错误。
SEVERE 8/31/16 3:41 PM: liquibase: changelog.groovy: changelog.groovy::1472650791344-1::michal (generated): Change Set changelog.groovy::1472650791344-1::michal (generated) failed.  Error: ERROR: relation "appointment" already exists [Failed SQL: CREATE TABLE public.appointment (id BIGSERIAL NOT NULL, version BIGINT, customer_id BIGINT, duration BYTEA, note TEXT, personal_available BOOLEAN DEFAULT 0, personal_booked BOOLEAN DEFAULT 0, provider_id BIGINT, start_time BYTEA, url VARCHAR(255), CONSTRAINT "appointmentPK" PRIMARY KEY (id))]
liquibase.exception.DatabaseException: ERROR: relation "appointment" already exists [Failed SQL: CREATE TABLE public.appointment (id BIGSERIAL NOT NULL, version BIGINT, customer_id BIGINT, duration BYTEA, note TEXT, personal_available BOOLEAN DEFAULT 0, personal_booked BOOLEAN DEFAULT 0, provider_id BIGINT, start_time BYTEA, url VARCHAR(255), CONSTRAINT "appointmentPK" PRIMARY KEY (id))]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:316)

...
Caused by: org.postgresql.util.PSQLException: ERROR: relation "appointment" already exists

我将不胜感激任何想法,为什么它不起作用。

谢谢格雷格·佩特金。设置dbcreate进行验证确实可以帮助我解决此错误。

最佳答案

问题在于您的dbCreate子句以及行为的非常不便的更改,导致无效的dbCreate值被忽略。如果您不希望Hibernate弄乱数据库并干扰数据库迁移,请使用dbCreate: validate

插件可以为Grails应用程序提供值,因此它们可以提供合理的默认值,并且可以从头开始工作:首先一个接一个地插入插件,最后application.yml可以覆盖它们设置的任何内容。为了便于开发和测试,插件也是独立的Grails应用程序,因此其中许多插件都具有dbCreate设置。

发生的不便之处在于,现在无效的dbCreate选项将被覆盖,然后覆盖由随机选择的最新插件留下的值,从而有可能有助于配置(该插件也可以使用create-drop,这可能是即使在开发环境中也非常不方便-实际上,较新的Grails版本将忽略插件中的dbCreate设置,因为当前行为是如此危险)。

因此,当数据库迁移运行时,Hibernate已经使用updatecreate-drop设置来创建表,因此数据库迁移失败。将值配置为validate应该为覆盖插件设置提供了有效的替代。

关于grails - Grails 3数据库迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39251406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com