Liquibase: Copy column data into a new column in the same table

sindhunk picture sindhunk · Mar 31, 2016 · Viewed 8.7k times · Source

I have a table with a column A. I am creating a new column B. B will have the same data as column A. How do I replicate the column in Liquibase? Is there some expression I can write to do the replication?

Answer

Tim Biegeleisen picture Tim Biegeleisen · Mar 31, 2016

Create a new changeset where you add a new column, and then update column B using the <sql> tag:

<changeSet author="yourName" id="example">
    <addColumn catalogName="db"
               schemaName="public"
               tableName="yourTable">
        <!-- replace varchar(255) with the actual type of column A -->
        <column name="B" type="varchar(255)"/>
    </addColumn>

    <sql>UPDATE yourTable SET B = A</sql>
</changeSet>