Checking context

I am seeing that if no contexts switch is present in the calling command, all changesets with any context are run. I would like to

  1. enforce that a context has been submitted to the liquibase command
  2. print what context was submitted

Any ideas?

@JamesContractor How are you calling Liquibase?

hey james

Are you familiar with liquibase quality checks or liquibase flow?

the first question is really just using the ChangesetContextCheck QC and setting the severity/return code to whatever level stops your automation job.

ideally you would run QCs as part of a flow, so the checks are enforced every time, but you could run QCs by themselves too.

as for number 2, there isnt a liquibase way to print out all the contexts. they ARE stored in teh DBCL, but there isnt a liquibase native “print contexts from DBCL” or something like that.

that said, since you can run shell scripts from flow, you could whip one up that queries the DBCL (or the text of the changelog) for that data. it would be an exercise left to the reader to wrangle permissions for DB access, or to parse through the changelog text to match for contexts, etc.

hope this leads you closer to where you need to be!
cheers,
mario

As a side note… The documentation page for using contexts does not have the proper syntax.

“contextFilter” does not seem to work:

–changeset bob:1 contextFilter:test

“context” does work:

–changeset bob:1 context:test

Thanks for your suggestions. If we had Pro we would have used flow. We ended up storing the context in the database so we could refer to it for certain configurations, and then using a preCondition to check that only one is set, like so:

databaseChangeLog:

  • changeSet:
    id: base-1101
    author: baseline
    changes:
    • sql:
      sql: create table if not exists drms.environment(
      context varchar(20) not null,
      bucket_account varchar(20)
      );
  • changeSet:
    id: base-1101a
    author: baseline
    changes:
    • sql:
      sql: delete from drms.environment;
  • changeSet:
    id: base-1101c
    author: baseline
    context: dev
    changes:
    • sql:
      sql: insert into drms.environment(context, bucket_account) values (‘dev’, ‘bclwr’);
    • output:
      message: “Context: DEV”
      target: STDOUT
  • changeSet:
    id: base-1101d
    author: baseline
    context: stage
    changes:
    • sql:
      sql: insert into drms.environment(context, bucket_account) values (‘stage’, ‘bclwr’);
    • output:
      message: “Context: STAGE”
      target: STDOUT
      … for other contexts
  • changeSet:
    preConditions:
    • onFail: HALT
    • onFailMessage: You must specify one and only one context!
      With maven, use a java system variable, e.g. java --Dliquibase.contexts=prod…
      With Liquibase CLI, pass --contexts=prod after the liquibase command.
      Note that the variable is contexts with an ‘s’.
      You need to remove incorrect entries from the drms.environment table.
    • sqlCheck:
      expectedResult: 1
      sql: select count(*) from drms.environment;
      id: base-1101
      author: noop
      runAlways: true