Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When creating a column using liquibase, how do I specify a value for that column based on an existing column?

Tags:

liquibase

I have an existing mysql table with two columns a and b.

I now want to add a column c to that table.

c should be nullable, should have a default value of NULL, except in those rows where column b has the value 10. Where b has the value 10, c should have a value X.

I understand that it is fairly simple to do this using SQL, but I want to do this using liquibase, since liquibase is what we use for our schema migrations.

like image 416
Aml Avatar asked Jun 14 '12 22:06

Aml


People also ask

How do I change the value of a column in Liquibase?

Step 1: Add the update Change Type to your changeset with the needed attributes as it is shown in the examples. Step 2: Deploy your changeset by running the update command. Now, you should see an updated table. Note: You can use the update Change Type in combination with loadData and loadUpdateData Change Types.

What is a change set in Liquibase?

The changeset tag is a unit of change that Liquibase executes on a database and which is used to group database Liquibase Change Types together. A list of changes created by multiple changesets are tracked in a changelog.


2 Answers

Have you already tried something like this?

<addColumn tableName="SGW_PRODOTTI_INFO_ATTRIBUTE">
    <column name="AlternativeListPrice" type="double" defaultValue="0.0">
    <constraints nullable="true"/>
    </column>
</addColumn>
like image 153
Walter Traspadini Avatar answered Oct 09 '22 03:10

Walter Traspadini


I think the best solution without using plain sql is following:

  • Use addColumn change, as Walter wrote;

  • Use update change.

You can choose to use both changes within a changeset, but a good practice is to separate each one by a separated changeset for liquibase transaction/rollback purposes.

like image 6
rodrigocprates Avatar answered Oct 09 '22 03:10

rodrigocprates