27

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.

Aml
  • 301
  • 1
  • 3
  • 6
  • 4
    You will need to use a `` tag in Liquibase. –  Jun 14 '12 at 22:33
  • 1
    Thank you, @a_horse_with_no_name; that worked quite nicely! – Aml Jun 15 '12 at 01:12
  • 1
    Possible duplicate of [How to add new column with default value from existing column in Liquibase](https://stackoverflow.com/questions/35172172/how-to-add-new-column-with-default-value-from-existing-column-in-liquibase) – axiopisty Apr 22 '19 at 16:41

3 Answers3

52

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

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

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.

rodrigocprates
  • 498
  • 6
  • 22
6

If you are adding column then

<changeSet author="your-name" id="your-id"> 
    <addColumn tableName="person" >
        <column name="is_active" type="varchar2(1)" defaultValue="Y" />  
    </addColumn>  
</changeSet>

add-column

if column is already added, and then you need to set default value

<changeSet author="your-name" id="your-id">
    <addDefaultValue columnDataType="varchar2(1)" columnName="is_active" defaultValue="Y" tableName="person"/>  
</changeSet>

add-default-value

Minnow
  • 495
  • 4
  • 6