简体   繁体   English

Liquibase & MySQL 5.7 时间戳列修改问题

[英]Liquibase & MySQL 5.7 timestamp column modification problem

I have a column called updated_time it has a timestamp data type assigned:我有一个名为updated_time的列,它分配了timestamp数据类型:

`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 

The problem is that currently, this column supports a second precision.问题是目前,该列支持第二精度。 I need it to be able to store a millisecond precision.我需要它能够存储毫秒精度。

So, I have prepared a following Liquibase changeset:因此,我准备了以下 Liquibase 变更集:

{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "1",
        "changes": [
          {
            "modifyDataType": {
              "columnName": "updated_time",
              "newDataType": "timestamp(3)",
              "tableName": "mytable"
            },
            "addDefaultValue": {
              "columnDataType": "timestamp(3)",
              "columnName": "updated_time",
              "defaultValueComputed": "current_timestamp(3)",
              "tableName": "mytable"
            },
            "addNotNullConstraint": {
              "columnDataType": "timestamp(3)",
              "columnName": "updated_time",
              "tableName": "mytable"
            }
          }
        ]
      }
    }
  ]
}

Unfortunately, this changeset always fails.不幸的是,这个变更集总是失败。

Here is the reason:原因如下:

2019-11-08 00:57:28.624  WARN [] 99326 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set ...
     Reason: liquibase.exception.DatabaseException: Invalid default value for 'updated_time' [Failed SQL: (1067) ALTER TABLE maas.mytable MODIFY updated_time timestamp(3)]

I saw some posts regarding the fact that there were some changes to the timestamp data type in version 5.7.我看到了一些关于 5.7 版本中timestamp数据类型发生了一些变化的帖子。 That, by default, I cannot assign a non-zero (null) value to the timestamp column.也就是说,默认情况下,我不能为timestamp列分配非零(空)值。 And, in order, to get it fixed, I'd have to make some changes to the server config itself.而且,为了修复它,我必须对服务器配置本身进行一些更改。 Let's imagine that I have a limited access to the server and I can't simply go and modify the config, is there an elegant way of working around this issue in Liquibase?假设我对服务器的访问权限有限,我不能简单地 go 并修改配置,在 Liquibase 中是否有解决此问题的优雅方法?

If you want to convert timestamp to bigint , then you can do it the following way:如果要将timestamp转换为bigint ,则可以通过以下方式进行:

  • create a new column, eg bigint_date ;创建一个新列,例如bigint_date
  • populate it with bigint values from your current timestamp_date column;使用当前timestamp_date列中的 bigint 值填充它;
  • drop your timestamp_date column;删除您的timestamp_date列;

Liquibase changeSets may look like this: Liquibase 变更集可能如下所示:

<changeSet id="foo1" author="bar">
    <preConditions onFail="MARK_RAN">
        <not>
            <columnExists tableName="your_table" columnName="bigint_date"/>
        </not>
    </preConditions>
    <comment>Add new column</comment>
    <addColumn tableName="your_table">
        <column name="bigint_date" type="bigint(13)"/>
    </addColumn>
</changeSet>
<changeSet id="foo2" author="bar">
    <preConditions onFail="MARK_RAN">
        <columnExists tableName="your_table" columnName="bigint_date"/>
        <columnExists tableName="your_table" columnName="timestamp_date"/>
    </preConditions>
    <comment>Populate it with bigint values from your current "timestamp_date" column</comment>
    <update tableName="your_table">
        <column name="bigint_date" valueComputed="SELECT UNIX_TIMESTAMP(timestamp_date) FROM your_table"/>
    </update>
</changeSet>
<changeSet id="foo3" author="bar">
    <preConditions onFail="MARK_RAN">
        <columnExists tableName="your_table" columnName="timestamp_date"/>
    </preConditions>
    <comment>Drop your "timestamp_date" column</comment>
    <dropColumn tableName="your_table" columnName="timestamp_date"/>
</changeSet>
<changeSet id="foo4" author="bar">
    <preConditions onFail="MARK_RAN">
        <columnExists tableName="your_table" columnName="bigint_date"/>
    </preConditions>
    <comment>Update NULL values</comment>
    <update tableName="your_table">
        <column name="bigint_date" valueComputed="UNIX_TIMESTAMP()"/>
        <where>bigint_date IS NULL</where>
    </update>
</changeSet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM