When should I use context and label attributes?

Context and label attributes are powerful tools that help you control how Liquibase deploys changesets. Because labels and contexts share many similarities and you can combine them on a changeset, it's sometimes difficult to decide whether context, labels or a combination of both will help you achieve your goals. However, it becomes easier to make a choice when you think about it from the perspective of who needs to understand or control which changesets to execute, the changeset author or the deployment manager.

What are contexts and labels?

Contexts are added to changesets to control which changesets execute in a particular migration run. They describe/tag the changeset with the environment. The changeset author decides which environments the changeset should run in.

Labels group and classify changesets. They are frequently used to control if complex logic is determined at runtime.

Context use case example

In this scenario, contexts describe/tag the environment, and the changeset author decides which environments the changeset should run in.

A changeset author is creating test data for a particular version. The data is tagged with context="!prod AND v.1.1".

At runtime, Liquibase commands specify the environment and version with a context expression of --contexts="test,v.1.1".

No runtime decision determines which changesets execute at runtime. Automation may be in place to always pass environment and artifact versions to the runtime context expression.

Label use case example

Labels describe/tag the changeset, and the deployment manager decides which labeled changesets to run.

The changeset author creates changesets for upcoming shopping cart features, but the decision about when these features will go to market has yet to be made.

The changeset author tags the changesets with labels="shopping-cart,feature-A".

Once the feature is ready to go live, a deployment engineer issues the Liquibase commands with a label expression of --labels="shopping-cart AND feature-A".

Related links