首页 > 解决方案 > 如何从 Java 应用的变更日志中获取前提条件?

问题描述

如果表存在,我正在尝试使用 PreConditions 跳过 CreateTable 变更集和 MARK_RAN。由于 Liquibase 在尝试在 MYSQL(在 AWS Aurora 中)中创建现有表时记录了表存在错误,因此似乎忽略了前提条件。

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">

    <changeSet author="xxx" id="1231">
        <preConditions onFail="MARK_RAN">
            <not>
                <tableExists tableName="tenant"/>
            </not>
        </preConditions>
        <createTable tableName="tenant">
            <column autoIncrement="true" name="tenantId" type="int(11)">
                <constraints primaryKey="true" nullable="false"
                    unique="false" />
            </column>
         ...

正在使用以下 Java 代码应用更改日志:

            final Database dataBase = DatabaseFactory.getInstance()
                    .findCorrectDatabaseImplementation(new JdbcConnection(connection));
            final Liquibase liquiBase = new liquibase.Liquibase(CHANGE_LOG_FILE, RESOURCE_ACCESSOR, dataBase);
            liquiBase.update(CONTEXTS, LABEL_EXPRESSION);

错误:


Response: {"errorMessage":"Failed to connect to Liquibase due to Migration failed for change set db.changelog-1.1.xml::1231::xxx:\n     Reason: liquibase.exception.DatabaseException: Table 'tenant' already exists [Failed SQL: CREATE TABLE xxx_global.tenant (tenantId INT AUTO_INCREMENT NOT NULL, region VARCHAR(255) NULL, tenantName VARCHAR(255) NULL, CONSTRAINT PK_TENANT PRIMARY KEY (tenantId))]"}

我唯一的其他想法是尝试在 CreateTable 变更集上使用自定义上下文进行全新安装

标签: liquibase

解决方案


我发现了这个问题,这不是 Liquibase 问题。包含调用 Liquibase 的 Lambda 代码的 AWS 堆栈未更新。我删除并重新创建了堆栈,它开始工作。


推荐阅读