VB_
VB_

Reputation: 45692

Run Liquibase changesets by context name

Is there any way to run only group of changesets with Liquibase API?

The following code initialize the whole schema before starting integration test class:

liquibase = new Liquibase(
    LIQUIBASE_CHANGELOG_PATH,
    new FileSystemResourceAccessor(),
    new JdbcConnection(embeddedTestDatabase.getConnection())
);
liquibase.dropAll();
liquibase.update(""); // PROBLEM: for some reason this launch all changesets including changesets with name `test`

Now I want to do something like DBUnit's @DatabaseSetup before specific test method - means execute only changesets with test context:

liquibase.update("test"); // PROBLEM : this also run all changesets

<changeSet author="me" id="some_id" logicalFilePath="some_path" context="test">
    <sql>
        INSERT INTO COMPANY (ID, CREATED_AT, CREATED_BY, NAME) VALUES (1, '2017-09-15 16:55:57.558', 'My company');
    </sql>
    <rollback>
        DELETE FROM COMPANY;
    </rollback>
</changeSet>

Upvotes: 2

Views: 1825

Answers (1)

Rich
Rich

Reputation: 15457

Yes, the "contexts" feature in Liquibase does what you need here.

The example given in the docs matches your requirements.

The docs explain:

When you run the migrator though any of the available methods, you can pass in a set of contexts to run. Only changeSets marked with the passed contexts will be run.

If you don’t assign a context to a changeSet, it will run all the time, regardless of what contexts you pass in to the migrator.

So I think you need to add a context other than "test" to the rest of your migrations, if you don't want them run here. Maybe "main"?

Upvotes: 4

Related Questions