英文:
Best practices on liquibase preConditions
问题
我正在寻找在Liquibase的changeSet
中何时使用preConditions
的最佳做法。
我理解这样做有助于检查现有的数据库状态,然后应用更改。
如果我从头开始使用Liquibase,并且所有的更改都将通过Liquibase进行,那么changeSet
是否足以检查/验证现有状态呢?在这种情况下,编写preConditions
似乎对我来说是多余的。我还没有找到任何关于这个问题的好文档。
在我的用例中,我将使用Liquibase来进行数据库模式更改+在几个表中添加元数据。
我看到了一些关于数据库模式更改查询的示例,比如添加表、列等,其中使用了preConditions
。
但在常规的插入、更新、删除查询周围并没有看到太多类似的内容。对于这样的数据操作查询,编写preConditions
是一个好的做法吗?是否有关于这方面的好文档?
英文:
I am looking for best practices on when to use preConditions
in Liquibase changeSet
.
I understand the fact that it helps in checking the existing state of the db and then applies the change.
If I am going to use Liquibase from the beginning and all the changes will be done via Liquibase should not changeSet
be enough to check/validate the existing state? Writing preConditions
seems to me more redundant in such case. I haven't been able to find any good document on this.
In my use case I will be using Liquibase for db schema change + adding metadata in couple of tables.
I see some examples for db schema change queries like adding table, column etc where preConditions
have been used.
But not seeing much around normal insert, update, delete queries. Is it good a practice to write preConditions
for such data manipulation queries as well? Is there any good documentation on this?
答案1
得分: 11
是 - 你应该编写 preConditions。在每个 changeSet 上都要写。总是如此。你的 changeSet 应该是原子的,因此为它们编写 preConditions 不应该很难。这只需要一点自我控制。
不是 - 仅通过 changeSet 的 id 来检查/验证现有状态是不够的。在理想世界中可能足够,那里一切运行得非常顺利,没有错误,也没有人用他们“肮脏”的手搞乱数据库。或者有人可以在你的 databaseChangeLog 中插入一些其他的 changeSet,这会打破仅基于 changeSets 的顺序和标识的理想流程。
但我们的世界并不理想,所以不,仅仅 changeSet 的 ID 是不够的。你需要 preConditions。
此外,如果你想了解 changeSet 的标识是如何确定的,可以查看这个 问题。
要回答你关于在执行插入、更新和删除查询之前如何检查数据的第二部分问题:
你总是可以使用 <sqlCheck>
标签。
<changeSet id="foo" author="bar">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(*) FROM user WHERE full_name='John Doe';
</sqlCheck>
</preConditions>
<sql>
<!-- 这里放置你的自定义 SQL 查询,它会修改数据 -->
</sql>
</changeSet>
英文:
Yes - you should write preConditions. On each and every changeSet. Always. Your changeSets should be atomic, so writing preConditions for them should not be hard at all. It just takes a little self-control.
No - changeSet id is not enough to check/validate the existing state. It could be enough in the ideal world, where everything's running extremely smooth, there're no errors and no one is messing with the database with their <s>dirty</s> hands. Or someone can insert some other changeSet in the middle of your databaseChangeLog and the ideal flow, based only on the sequence and identity of changeSets will be broken.
But our world is not ideal, so No, changeSet's ID is not enough. You need preConditions.
Also, check out this question if you want to know more about how the identity of the changeSet is determined.
To answer the second part of your question about checking the data before executing insert, update and delete queries:
You can always use <sqlCheck>
tag.
<changeSet id="foo" author="bar">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(*) FROM user WHERE full_name='John Doe';
</sqlCheck>
</preConditions>
<sql>
<!-- your custom SQL query here which modifies data somehow -->
</sql>
</changeSet>
答案2
得分: 4
因为“最佳实践”类型的问题很少有一个单一正确的答案,我会添加我的见解。
我的公司运行着一个单一的巨型应用程序,已经使用 liquibase 进行变更管理已有 5 年以上。我们有一个由 4 名开发人员组成的团队,并在 100 多台服务器上部署了我们的应用程序。我们从不使用前置条件,我也不记得曾经遇到过 liquibase 的问题,前置条件会有所帮助。另一方面,我们非常努力地确保除了通过 liquibase 进行数据库更改外,不进行任何其他更改,正是因为我们依赖于这个假设。
只要确保每个人都在相同的页面上,你应该没问题。
注意:我发现这个问题,因为我正在调查前置条件,因为我们正在编写另一个将与巨型应用程序的一部分共享架构的应用程序。由于变更日志随后会在多个独立演化的项目之间逻辑分布,我们的假设可能不再成立。如果你计划在多个项目之间共享数据库,请牢记这一点。
英文:
Since "Best Practice" type questions rarely have a single correct answer, I'll add my own.
My company runs a single monolithic app and has been using liquibase for change management for 5+ years. We're a team of 4 developers and have deployed our app on 100+ servers. We never use preconditions, and I don't recall ever having a liquibase problem where a precondition would have helped. On the other hand, we're pretty diligent about not making db changes except through liquibase EXACTLY BECAUSE we rely on this assumption.
Just make sure everyone is on the same page, and you should be fine.
Note: I found this question as I'm investigating preconditions because we're writing another app that's going to share part of the monolith's schema. Because the changelog would then be logically distributed amongst multiple projects being evolved independently, our assumptions may no longer hold. Keep this in mind if you're planning to share the database across multiple projects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论