gpt4 book ai didi

mysql - Liquibase 部分提交变更集

转载 作者:可可西里 更新时间:2023-11-01 07:07:37 27 4
gpt4 key购买 nike

在 MySQL 上使用 liquibase v3.6.3。如果我理解正确,默认情况下每个 CHANGESET 都会运行到一个 SQL 事务中。但是,在我看来,交易是在 CHANGE 的基础上进行的。运行此脚本时

databaseChangeLog:
- changeSet:
id: changeset-
changes:
- renameTable:
oldTableName: old_table
newTableName: new_table
- addColumn:
columns:
- column:
name: test_column_name
type: varchar(255)
tableName: other_table

如果 addColumn 标记因为某些 SQL 异常(即约束检查或其他)而失败,那么数据库变更日志表将不会更新,我不希望这样做,因为变更集失败了。但是,第一个语句确实通过了,我的表现在称为 new_table

当然,如果我纠正导致第二个更新失败的问题并重试更新,它将失败,因为 old_table 不再存在。

我在 liquibase 文档中知道这一段

Liquibase attempts to execute each changeSet in a transaction that is committed at the end, or rolled back if there is an error. Some databases will auto-commit statements which interferes with this transaction setup and could lead to an unexpected database state. Therefore, it is usually best to have just one change per changeSet unless there is a group of non-auto-committing changes that you want applied as a transaction such as inserting data.

https://www.liquibase.org/documentation/changeset.html

但是我不是很懂。自动提交意味着自动提交一个事务。如果所有的变更集都包含在一个事务中,为什么只有一些变更通过了? liquibase 应该回滚整个事务吗?

对此有什么最佳做法吗?我们不能在liquibase中手动设置交易吗?

最佳答案

部分提交变更集的不是 Liquibase。我使用过许多数据库,我使用的所有数据库都有一个基本概念,即事务仅结合数据修改 (DML)。

DDL 永远不是事务的一部分。它总是立即执行,并且在执行之前自动提交打开的事务。这是因为数据库的回滚命令只能处理数据修改。它无法处理 DDL。如果回滚不再可能,那么保持事务打开就变得毫无用处。

因此,Liquibase 确实创建了一个事务并在最后按照文档说明提交所有更改。但这只有在变更集只包含 DML 而没有 DDL 时才有效。

因此,DDL 和 DML 永远不应该混合在一个变更集中,每个 DDL 语句都应该在一个单独的变更集中。否则,Liquibase 无法阻止变更集部分成功并在尝试回滚时造成麻烦。

关于mysql - Liquibase 部分提交变更集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56391050/

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