Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert int to value when changing datatype in column

Tags:

liquibase

Is it possible with liquibase to do some kind of mapping when doing a modifyDataType?

Considering the following example:

<changeSet id="1" author="me">
    <createTable tableName="person">
        <column name="id" type="int">
            <constraints primaryKey="true" nullable="false" />
        </column>
        <column name="firstName" type="varchar(100)"></column>
        <column name="lastName" type="varchar(100)"></column>
        <column name="marital_status" type="int">
            <constraints nullable="false" />
        </column>
    </createTable>
</changeSet>
<changeSet id="2" author="me">
    <modifyDataType tableName="person" columnName="type"
        newDataType="varchar(36)" />
</changeSet>

I would like in my column 'type' that the following mapping occurs:

0->single
1->married
etc..

Is this possible with liquibase? I'm using it via the command line.

like image 514
phury Avatar asked Mar 18 '13 14:03

phury


1 Answers

I think this is not possible directly via some kind of mapping or refactoring.

One way to do this is using sql and put this into another changeset which is run right after the one that changes the data type:

<changeSet id="3" author="me">
    <sql>
        update person set martial_status = 'single' where martial_status = 0;
        update person set martial_status = 'married' where martial_status = 1;
    </sql>
</changeSet>

I just wrote this down as an idea. It is untested.

You could add a precondition to execute this changeset only if changeset with id 2 ran successful.

like image 51
Jens Avatar answered Oct 24 '22 00:10

Jens