Search code examples
liquibase

Why are all contexts executed when non specified on update?


I'm using Liquibase 3.3.5 to update my database. Having contexts is a nice way to only execute specific parts of the changelog. But I don't understand, why ALL changesets are executed, when no context is provided on update. Consider the following example:

  • changeset A: context=test
  • changeset B: no context
  • changeset C: context=prod

So

  • executing update with context=test, will execute changeset A+B.
  • executing update with context=prod, will execute changeset B+C.
  • executing update with no context, will execute changeset A+B+C.

For me, this doesn't make sense at all :).

I would expect, that only changeset B will be executed, since it doesn't define a specific context.

In the Liquibase contexts example: http://www.liquibase.org/documentation/contexts.html ("Using Contexts for Test Data") they say, that one should mark the changesets for testing with "test", and executing them with giving the context "test" to apply testdata. Fine - make sense. But

"When it comes time to migrate your production database, don’t include the “test" context, and your test data not be included. "

So, if I wouldn't specify "test" context when executing production update, it would execute the "test" changesets as well, since I didn't specify a context at all.

Again, I would expect that leaving out test on update execution, would only perform the regular changesets without the test changesets.

Or I'm missing something here :)?


Solution

  • This is just how Liquibase works - if you do an update and don't specify a context, then all changesets are considered as applicable to that update operation.

    There were a couple of ways that this could have been implemented, and the development team had to pick one.

    1. if you don't specify a context during an update operation, then no changesets are considered.
    2. if you don't specify a context, then all changesets are considered.
    3. if you don't specify a context, then only changesets that have no context are considered.
    4. if you don't specify a context and none of the changesets have contexts on them, then all changesets are considered, but if some of the changesets do have contexts, go to option 1, 2, or 3 above.

    The team could have gone with option 3 (which matches your expectation) but decided long ago to go with option 2, as that seemed like the 'best' way at the time. I wasn't on the team at that time, so I don't know any more than that.