简体   繁体   中英

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

I'm using Liquibase for data migration.

I have a table named Document that already contains values.

My table Document contains columns(id, name, dueDate). The dueDate column is of type Date and i want to change his type from DATE to DATETIME.

I have adopted the following strategy

1- create a new column duedatenew of type DATETIME

2- copy values from column duedate to duedatenew

3- delete column duedate

4- rename column 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...

But, according to this , your best option is to use the sql Tag to execute the update as your want... Example:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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