04-23.eri-test Liquibase:请勿将ISO日期用作yml的变更集ID

\n

After upgrading a service from java 8 to java 11, our liquibase got crazy and tried to apply every change to the database from the beginning. But they were already applied and no changes were made to the database.
\nWe discovered that using ISO dates as identifiers of the changeset was a bad idea. The hard way.

\n

\n \n \n \xc2\xa0Liquibase Changesets\n

\n\n

Changesets in liquibase are, as their name implies, a set of changes that must be applied at once in a database (or not at all).

\n\n

In order that liquibase is able to distinguish when a changeset is already applied or not, it requires you to set a unique identifier.

\n\n

By convention, we started to use the ISO date when the change was written (format yyyy-mm-dd).

\n\n

For example:
\n

\n\n
databaseChangeLog:\n- changeSet:\n    id: 2019-06-06\n    author: sergiomoratilla\n    comments: Adding new value to know which client was used to book.\n    changes:\n    - modifyDataType:\n        tableName: reservation\n        columnName: client\n        newDataType: ENUM(\'WEB_DESKTOP\', \'WEB_MOBILE\', \'APP_ANDROID\', \'APP_IOS\', \'UNKNOWN\') NOT NULL\n
\n\n\n\n

Liquibase stores the applied changeset in a table called DATABASECHANGELOG within your schema. So that, we were expecting a row
\n

\n\n
"2019-06-06"    sergiomoratilla /liquibase/changelogs/this-example.yml ...\n
\n\n\n\n

\n \n \n It\'s a trap\n

\n\n

Liquibase\'s YML parser decides to get the changeset id as a Date, and then use toString() to generate the id. That smells because you don\'t have control on that format... and this is exactly what happened.

\n\n

Instead of storing "2019-06-06" we got "Wed Jun 06 00:00:00 GMT 2019".

\n\n

After upgrading to Java 11, the behaviour of toString() changed and now returns "Wed Jun 06 02:00:00 CEST 2019". It is exactly the same date so that this format is correct but it is a bit weak to trust your ids to default formatting.

\n\n

\n \n \n \xc2\xa0Solutions\n

\n\n

Don\'t use ISO date as ids (if you are using yml format to configure that). Probably, most of you didn\'t already do that.

\n\n

When we started to have several changes on the same date, we decided to change that format to yyyymmdd-n, where n is an incremental integer.

\n\n

What if you are already using them? I suggest you to replace the ids in your changelogs files by the ids you already have in your database. And change your convention for new files!

\n\n\n
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值