14

Is there a way to write a liquibase addColumn changeset so it generates sql like

ALTER TABLE xxx ADD COLUMN yyy AFTER zzz;

I mean, is there a way to add an equivalent of "after column zzz" in liquibase jargon?

PapaFreud
  • 3,636
  • 4
  • 34
  • 45

2 Answers2

17

With Liquibase 3.1 there are new "afterColumn", "beforeColumn" and "position" attributes on the column tag.

The documentation at http://www.liquibase.org/documentation/column.html was just updated to include them.

Nathan Voxland
  • 15,453
  • 2
  • 48
  • 64
  • 2
    These column attributes aren't working yet. Relevant bug report: https://liquibase.jira.com/browse/CORE-1745 – David Keen Feb 10 '14 at 14:58
15

Until this is fixed, you can use modifySql.

<changeSet id="1-1" author="david">
    <comment>Add column field to example table</comment>
    <addColumn tableName="example">
        <column name="name" type="VARCHAR(50)" defaultValue="">
            <constraints nullable="false"/>
        </column>
    </addColumn>

    <!-- Use modifySql so we can insert it in the desired position -->
    <modifySql>
        <append value=" AFTER some_column"/>
    </modifySql>
</changeSet>
David Keen
  • 613
  • 7
  • 11
  • 1
    BTW `append` fails on H2 when field has unique constraint (because in this case 2 `ALTER TABLE` commands will be generated). – Slava Semushin Jun 12 '14 at 19:12