我正在研究将 Liquibase 用于使用 Oracle 的新项目,我想知道如何确保我的变更集足够强大,可以在无需人工干预的情况下从任何类型的故障中恢复。理想情况下,我会使用 runInTransaction 属性,它允许 DDL 在失败时回滚,但 Oracle 会在 DDL 上自动提交。对于这种情况,文档建议:
因此,通常最好每个 changeSet 只进行一次更改,除非您希望将一组非自动提交更改应用为事务,例如插入数据。
每个 changeSet 有一个 DDL 会减少出现问题的机会,但不会消除它们。如果 DDL 成功,但对 DATABASECHANGELOG 的更新失败,从我的测试来看,Liquibase 似乎只是卡住了,需要手动干预。
有必要在每一步都使用前置条件来避免这个问题吗?这使得生成的变更集非常冗长。这是 Liquibase 示例表定义之一:
<changeSet author="jsmith" id="1">
<createTable tableName="departments"
remarks="The departments of this company. Does not include geographical divisions.">
<column name="id" type="number(4,0)">
<constraints nullable="false" primaryKey="true"
primaryKeyName="DPT_PK"/>
</column>
<column name="dname" type="varchar2(14)"
remarks="The official department name as registered on the internal website."/>
</createTable>
<addUniqueConstraint constraintName="departments_uk1"
columnNames="dname" tableName="departments"/>
<createSequence sequenceName="departments_seq"/>
</changeSet>
为了使它具有幂等性,我认为它必须更改为以下内容:
<changeSet author="jsmith" id="1">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="departments" />
</not>
</preConditions>
<createTable tableName="departments"
remarks="The departments of this company. Does not include geographical divisions.">
<column name="id" type="number(4,0)" / column>
<column name="dname" type="varchar2(14)"
remarks="The official department name as registered on the internal website." />
</createTable>
</changeSet>
<changeSet author="jsmith" id="2">
<preConditions onFail="MARK_RAN">
<not>
<primaryKeyExists primaryKeyName="pk_departments" />
</not>
</preConditions>
<addPrimaryKey tableName="departments" columnNames="id"
constraintName="pk_departments" />
</changeSet>
<changeSet author="jsmith" id="3">
<preConditions onFail="MARK_RAN">
<not>
<uniqueConstraintExists constraintName="departments_uk1" />
</not>
</preConditions>
<addUniqueConstraint constraintName="departments_uk1"
columnNames="dname" tableName="departments" />
</changeSet>
<changeSet author="jsmith" id="4">
<preConditions onFail="MARK_RAN">
<not>
<sequenceExists sequenceName="departments_seq" />
</not>
</preConditions>
<createSequence sequenceName="departments_seq" />
</changeSet>
有没有更简单的方法来实现这一点?我原以为 Liquibase 能够生成这些先决条件。
谢谢