GareGarble
GareGarble

Reputation: 305

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

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?

Upvotes: 21

Views: 20600

Answers (2)

Vasily Konyaev
Vasily Konyaev

Reputation: 229

this is possible too:

<changeSet id="1" author="your_name">
    <addColumn tableName="your_table">
        <column name="b" type="varchar(255)"/>
    </addColumn>
</changeSet>

<changeSet id="2" author="your_name">
    <update tableName="your_table">
        <column name="b" valueComputed="a"/>
    </update>
</changeSet>

Upvotes: 22

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521093

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>

Upvotes: 16

Related Questions