Search code examples
mysqlsqlliquibasechangeset

Is there a way to combine changeset in liquibase?


Let's say that it is needed to add 2 column to a table and modify 1. From pure design view I need to create 3 changesets with 3 different preconditions: 2 column exist, 1 that column has wrong type.

This is clear design approach, but it leads to 3 separate alter table, which can be very expensive. For example MySql copy or rebuild entire table when column is added or modified. But it allows to combine several modified statement into one. But one altering changeset needs to combine preconditions which is impossible - should I perform big alter when one of them true or all?

So is there a way to look at several preconditions and construct alter table from successful preconditions only?


Solution

  • For MySQL, if you have an addColumn tag with multiple nested columns, it will generate a single ALTER TABLE statement that adds all the columns at once.

    You are able to nest multiple preconditions in "and" and "or" blocks, so you could have one changeSet that checks if column1 is missing AND column2 is missing and if both are, then run the addColumns with both columns listed. Then the next changeSet can check for missing column1 and the next for missing column2 and the next for the wrong data type.

    You will want the preconditions onFail attribute set to "MARK_RAN" so they are just marked at ran and are not continually re-evaluated on each update.