0

Let us take an example : In the given liquibase xml file , I have multiple changesets with different ids.

changeSet id="1"
changeSet id="2"
changeSet id="3"
changeSet id="4"
liquibase.bat --changeLogFile=..\MasterChangelog.xml --driver==com.microsoft.sqlserver.jdbc.SQLServerDriver --classpath=sqljdbc4-3.0.jar --url="jdbc:sqlserver://;databaseName=MyDB;user=MyUser;password=MyPassword;" --logLevel=INFO update

All the above changesets will get executed as expected.

Now my requirement is, i should have a feasibility to ignore/should not consider the changeset whose id is "3"

Reason is , I have different DBs and don't want some specific changesets should be not be executed.

do we have any commands to get this done?

Please advise.

  • Why do you need the chageset id-"3" in the changelog if it needs to be ignored? Or Is it that you need to skip based on any condition? – JITHIN_PATHROSE Jul 24 '19 at 06:44
  • Yes JITHIN. i need to skip the changeset conditionally. i found some concept called 'context' in liquibase. Now i am trying investigating whether this best suits my requirement or not. – Raj Kumar Uppala Jul 24 '19 at 09:20
  • In that case, you can use preConditions inside a changeset. Refer https://stackoverflow.com/a/57096840/4229716 – JITHIN_PATHROSE Jul 24 '19 at 09:55
  • 1
    But here in my case, preconditions will not suit as i dont have any conditions rather than information of DBs. So Context seems to be best suits me. Thanks for your time in suggesting me different possibilities – Raj Kumar Uppala Jul 24 '19 at 11:35

1 Answers1

0

The standard way to do this is using liquibase contexts and labels. There is a good blog post describing their use at https://www.liquibase.org/2014/11/contexts-vs-labels.html

I've copied most of that here for easier reference and to protect against link rot.

Contexts

Contexts in Liquibase have been available for quite a while, and they started out primarily as a way of “tagging” changeSets so they can be chosen at runtime. One common use is to mark changeSets that insert test data as context=”test” so that in your development and QA environments you you can run liquibase with –contexts=test to get the test data and in production you run with –contexts=prod to not have test data. Contexts are also helpful for marking changeSets based on feature sets to include (context=”shoppingCart”) or bundle (context=”pro”) or even customer (context=”acme_inc”). For complex cases, multiple contexts can be applied to a changeSet such as context=”acme_inc, pro” and multiple contexs can be chosen at runtime such as –contexts=free,qa.

With Liquibase 3.2, support was added to for context expressions in changeSets. Now, when you are defining your changeSet you can specify complex logic such as context=”!test” or context=”qa or (acme_inc and dev)”. The context logic can only be specified in your changeSet definition, however. When running Liquibase, you can still specify multiple contexts, but you are just listing out all the contexts that apply to the current Liquibase run.

Labels

Labels were added in Liquibase 3.3 to work like contexts, but “backwards” in who can specify logical expressions. In your changeSet you can only specify a simple list of “labels” that apply to the changeSet but at runtime you can write a complex expression to chose the labels you want to execute. This allows you to specify a changeSet with labels=”qa, acme_inc” and then at runtime use expressions such as –labels=”!acme_inc” or –labels=”pro or (free and beta)”.

Which is right for you?

Whether you should use contexts or labels comes down to whether the changeSet writer or the Liquibase executor best understands and/or needs the most control over which changeSets to execute.

If the changeSet author needs to be able to specify complex logic based on the kind of environment that Liquibase will run in, use contexts. If the person executing Liquibase needs to specify complex logic to chose changeSets to run, use labels. If you do not use complex expressions, there is no functional difference between them.

Remember: you can use both.

SteveDonie
  • 8,700
  • 3
  • 43
  • 43
  • Many thanks Steve for your quick suggestion on this. I will go through your suggestion and get back to you with more questions :) – Raj Kumar Uppala Jul 24 '19 at 06:09
  • In my scenario, I need to take decision on only one changeset and rest should get consider by default as i have hundreds of changesets and cannot implement context concept now.. any suggestion on this query? – Raj Kumar Uppala Jul 24 '19 at 07:11
  • 1
    Hello Steve, based on your suggestion I have investigated further and seems be i got some clue on this from https://dba-presents.com/index.php/liquibase/188-environments-in-liquibase-changesets . i will try this and get back to you in case of any queries on this – Raj Kumar Uppala Jul 24 '19 at 07:41