简体   繁体   English

Liquibase将列类型从Date更改为DateTime而不删除包含的值

[英]Liquibase change column type from Date to DateTime without deleting contained values

I'm using Liquibase for data migration. 我正在使用Liquibase进行数据迁移。

I have a table named Document that already contains values. 我有一个名为Document的表已经包含值。

My table Document contains columns(id, name, dueDate). 我的表文档包含列(id,name,dueDate)。 The dueDate column is of type Date and i want to change his type from DATE to DATETIME. dueDate列的类型为Date,我想将其类型从DATE更改为DATETIME。

I have adopted the following strategy 我采用了以下策略

1- create a new column duedatenew of type DATETIME 1-创建一个DATETIME类型的新列duedatenew

2- copy values from column duedate to duedatenew 从列duedate到duedatenew的2-拷贝值

3- delete column duedate 3-删除列duedate

4- rename column duedatenew to duedate 4-重命名列duedatenew to duedate

as described in the following changeset 如以下变更集中所述

  <changeSet id="task-99" author="blaise">
        <addColumn tableName="document">
            <column name="duedatenew" type="DATETIME" />
        </addColumn>
        <update tableName="document">
            <column name="duedatenew" valueComputed="(SELECT duedate FROM document)" />
        </update>
        <dropColumn tableName="document" columnName="duedate" />
        <renameColumn tableName="document" oldColumnName="duedatenew"
        newColumnName="duedate" />
</changeSet>

but the execution of changeset always fails during the second step. 但是在第二步中变更集的执行总是失败。 the copy of data always fails. 数据副本总是失败。

How can i solve this please? 我该怎么解决这个问题?

I was watching the column docs... It seems that valueComputed should point to a sql function, so a select query will not work... 我正在观看文档...似乎valueComputed应该指向一个sql函数,所以select查询将无效...

But, according to this , your best option is to use the sql Tag to execute the update as your want... Example: 但是,根据这个 ,你最好的选择是使用sql标签来执行更新,如你所愿...示例:

<changeSet id="task-99" author="blaise">
    <addColumn tableName="document">
        <column name="duedatenew" type="DATETIME" />
    </addColumn>
    <sql>update document set duedatenew = duedate</sql>
    <dropColumn tableName="document" columnName="duedate" />
    <renameColumn tableName="document" oldColumnName="duedatenew"
    newColumnName="duedate" />
</changeSet>

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

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