【问题标题】:Duplicate column name , In liquibase after commenting a changeset and uncommenting it列名重复,在 liquibase 中评论变更集并取消评论后
【发布时间】:2019-12-17 19:46:29
【问题描述】:

在我的 Java 本地开发环境中,我错误地注释了一些变更集并运行了应用程序。 现在,当我取消注释更改集时,Liquibase 崩溃并抱怨数据库中的重复列。

有什么方法可以清除 Liquibase 而不必删除整个数据库? 好像有校验和冲突什么的。

    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.MigrationFailedException: Migration failed for change set classpath:/db/changelog/init/33_20191217_sprint31.xml::more attributes for user:::
     Reason: liquibase.exception.DatabaseException: Duplicate column name 'option_cgu' [Failed SQL: ALTER TABLE ga_db.api_user ADD option_cgu BIT(1) NULL]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at com.groupama.gari.api.GariApiApplication.main(GariApiApplication.java:16) [classes/:na]
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set classpath:/db/changelog/init/33_20191217_sprint31.xml::more attributes for user::Moncef Fadal GSB850:
     Reason: liquibase.exception.DatabaseException: Duplicate column name 'option_cgu' [Failed SQL: ALTER TABLE ga_db.api_user ADD option_cgu BIT(1) NULL]
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:619) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:79) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.Liquibase.update(Liquibase.java:214) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.Liquibase.update(Liquibase.java:192) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388) ~[liquibase-core-3.5.3.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    ... 18 common frames omitted
Caused by: liquibase.exception.DatabaseException: Duplicate column name 'option_cgu' [Failed SQL: ALTER TABLE ga_db.api_user ADD option_cgu BIT(1) NULL]
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:309) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:113) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1277) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1259) ~[liquibase-core-3.5.3.jar:na]
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:582) ~[liquibase-core-3.5.3.jar:na]
    ... 26 common frames omitted

【问题讨论】:

    标签: liquibase


    【解决方案1】:

    看起来不像是校验和冲突。看起来其他 changeSet 已经添加了此列。

    但是,如果您想删除有关已执行 changeSet 的记录,请按照以下步骤操作(尽管这样做不是一个好习惯,但评论 changeSets 也不是):

    ID 列的databasechangelog 表中找到您需要的changeSet 的ID 并删除此行。

    如果没有这个记录,liquibase 将不会知道这个 changeSet 曾经被执行过,并且会在下一次应用程序启动时尝试执行它。

    我建议您尝试将<preConditions> 添加到changeSets 以避免此类错误。在这种情况下,它可能如下所示:

    <preConditions onFail="MARK_RUN">
        <not>
            <columnExists tableName="api_user" columnName="option_cgu"/>
        </not>
    </preConditions>
    

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 2015-08-17
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 2016-06-03
      • 2019-02-18
      • 2013-12-31
      • 2013-03-22
      相关资源
      最近更新 更多