Pergunta

We have couple of data schemas and we investigate the migration to Liquibase. (One of data schemas is already migrated to Liquibase).

Important question for us is if Liquibase supports dry run:

  • We need to run database changes on all schemas without commit to ensure we do not have problems.
  • In case of success all database changes run once again with commit.

(The question similar to this SQL Server query dry run but related to Liquibase)

Added after the answer

I read documentation related to updateSQL and it is not answers the requirements of “dry run”. It just generates the SQL (in command line, in Ant task and in Maven plugin). I will clarify my question:

Does Liquibase support control on transactions?

I want to open transaction before executing of Liquibase changelog, and to rollback the transaction after the changelog execution. Of course, I need to verify the result of the execution.

Is it possible?

Added

Without control on transactions (or dry run) we can not migrate to Liquibase all our schemas.

Please help.

Foi útil?

Solução

You can try "updateSQL" mode, it will connect db (check you access rights), acquire db lock, generate / print SQL sentences to be applied (based on db state and you current liquibase change sets) also it will print chageset id's missing in current state of db and release db lock.

Outras dicas

Unfortunately, no.

By default, Liquibase commits the transaction executing all statements of a changeset. I assume that the migration paths you have in mind usually involve more than a single changeset.

The only way you can modify the transaction behavior is the runInTransaction attribute for the <changeset> tag, as documented here. By setting it to false, you effectively disable the transaction management, i.e. it enables auto-commit mode as you can see in ChangeSet.java.

I think that this feature could be a worthwhile addition to Liquibase, so I opened a feature request: CORE-1790.

I think your answer is "it does not support dry runs" but the problem is primarily with the database and not with liquibase.

Liquibase does run each changeSet in a transaction and commits it after inserting into the DATABASECHANGELOG table so in theory you could override liquibase logic to roll back that transaction instead of committing it, but you will run into the problem where most SQL ran by liquibase is auto-committing.

For example, if you had a changeSet of:

<changeSet>
  <createTable name="test">
   ...
   </createTable>
</changeSet>

What is ran is:

START TRANSACTION
CREATE TABLE NAME ...
INSERT INTO DATABASECHANGELOG...
COMMIT

but even if you changed the last command to ROLLBACK the create table call will auto-commit when it runs and the only thing that will actually roll back is the INSERT.

NOTE: there are some databases that will rollback DDL SQL such as postgresql, but the majority do not.

INSERT/UPDATE commands would run in a transaction and could be auto-rolled back at the end, but liquibase does not have a postCondition command to do the in-transaction check of the state that would be required. That would be a useful feature (https://liquibase.jira.com/browse/CORE-1793) but even it would not be usable if there are any auto-committing change tags in the changeset. If you added a postcondition to create table example above, the postcondition would fail and the update would fail, but the table would still be there.

If your Liquibase migration is sufficiently database agnostic, you can just run it on an in-memory H2 database (or some other "throwaway database") that you can spin up easily using a few lines of code.

var info = new Properties();
info.put("user", "sa");
info.put("password", "");

try (var con = new org.h2.Driver().connect("jdbc:h2:mem:db", info)) {
    var accessor = new FileSystemResourceAccessor();
    var jdbc = new JdbcConnection(con);
    var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbc);

    Liquibase liquibase = new Liquibase("/path/to/liquibase.xml", accessor, database);
    liquibase.update("");
}

I've blogged about this approach more in detail here.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top