Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using spring boot profiles with liquibase changeset context attribute to manage changset scope

I am trying to do a proof of concept application with spring boot and liquibase. I basically want to create a spring boot app that can manage liquibase changesets by utilizing the changeset attribute called context, so that changesets with no context can be applied to any spring boot profile, whereas changesets with specific context (e.g context="dev" ) will only be applied if spring boot profiles of that type, is active (e.g spring.profiles.active=dev).

In my app, i have the following spring profiles -> dev, prod (each specified correctly in the application yaml file with the relavant profile db credentials also specified under the profile). What do I need to do to make this work. below is my application.yaml

spring:
  application:
    name: liquibase-spring-jpa-postgres-example
liquibase:
  change-log: db.changelog/db.changelog-master.xml

spring:
  profiles: dev
  datasource:
    url: jdbc:postgresql://localhost:5432/dev
    username: postgres
    password: password
    driver-class-name: org.postgresql.Driver

spring:
  profiles: ci
  datasource:
      url: jdbc:postgresql://localhost:5432/ci
      username: postgres
      password: password
      driver-class-name: org.postgresql.Driver

spring:
  profiles: qa
  datasource:
      url: jdbc:postgresql://localhost:5432/qa
      username: postgres
      password: password
      driver-class-name: org.postgresql.Driver

spring:
  profiles: production
  datasource:
      url: jdbc:postgresql://localhost:5432/prod
      username: postgres
      password: password
      driver-class-name: org.postgresql.Driver

and below is the current databaseChangeLog file (the second change set shouldn't run if my spring profile is prod).

<changeSet id="20161016_my_first_change" author="fike" context="dev, qa, ci, production">
    <sql>
        CREATE TABLE customer
        (
        id BIGSERIAL PRIMARY KEY,
        firstname character varying NOT NULL,
        lastname character varying NOT NULL
        );
    </sql>
    <rollback>
        drop table customer;
    </rollback>
</changeSet>

<changeSet id="20161016_my_first_change2" author="krudland" context="dev">
    <sql>
        insert into customer (firstname, lastname) values ('Franklin','Ike');
    </sql>
    <rollback>
        delete from customer where firstname = 'Franklin' and lastname = 'Ike';
    </rollback>
</changeSet>

I basically need to be able to manage my liquibase context, using my spring profile. Is this possible?

like image 531
franklini Avatar asked Oct 17 '16 14:10

franklini


People also ask

What are Liquibase contexts?

October 27, 2021. Liquibase labels and contexts allow you to choose a subset of changesets to execute at runtime, enabling many different use cases.

What is a changeset in Liquibase?

The changeset tag is a unit of change that Liquibase executes on a database and which is used to group database Liquibase Change Types together. A list of changes created by multiple changesets are tracked in a changelog.


1 Answers

You need to define 'liquibase.contexts' property into your yaml file. Something like below.

spring:
  profiles: dev
  datasource:
    url: jdbc:postgresql://localhost:5432/dev
    username: postgres
    password: password
    driver-class-name: org.postgresql.Driver
liquibase:
   contexts: dev

After adding this the below change set will only execute when your local profile is 'dev' (i.e. spring-boot:run -Dspring.profiles.active=dev)

<changeSet id="20161016_my_first_change2" author="krudland" context="dev">
    <sql>
        insert into customer (firstname, lastname) values ('Franklin','Ike');
    </sql>
    <rollback>
        delete from customer where firstname = 'Franklin' and lastname = 'Ike';
    </rollback>
</changeSet>
like image 92
mehtaa Avatar answered Oct 09 '22 09:10

mehtaa