Safely Evolving Database with Liquibase, Spring Data, and Spring Boot

Overview

This blog post explains the process to safely evolve Database with Liquibase, Spring Boot, and Spring Data JPA. Liquibase Maven plugin gives the ability to perform liquibase operations through maven commands and Liquibase Hibernate plugin helps to generate liquibase changesets based on JPA entities. Code uploaded to Github for reference

Technologies Used

  1. Spring Boot 2.x
  2. Spring Data JPA 2.x
  3. Java 11
  4. Liquibase 4.0.0
  5. Liquibase Hibernate plugin 3.10.1
  6. Liquibase Maven plugin 4.0.0

Instructions

  1. Clone the Github repository into local machine
  2. Add required dependencies for Spring Boot, Spring Data, Liquibase, etc. based on below mentioned pom.xml file
pom.xml
package com.liquibasedemo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import javax.persistence.*;


@Entity
@Table(name = "country")
@Data
public class Country
{
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;

@Column(name = "name")
private String name;

@Column(name = "code")
private String code;

@Column(name = "iso_code")
private String isoCode;


@ManyToOne
@JoinColumn(name = "region_id")
@JsonIgnore
private Region region;

public Country()
{
}

}

CountryRepository.java

package com.liquibasedemo.repo;

import com.liquibasedemo.model.Country;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CountryRepository extends JpaRepository<Country, Long>
{
}

CountryController.Java

package com.liquibasedemo.web;

import com.liquibasedemo.model.Country;
import com.liquibasedemo.repo.CountryRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/country")
public class CountryController
{
private final CountryRepository countryRepository;

public CountryController(CountryRepository countryRepository)
{
this.countryRepository = countryRepository;
}

@GetMapping(path = "/list")
public List<Country> findAllCountries()
{
return countryRepository.findAll();
}

@GetMapping(path = "/create")
public Country createCountry()
{
Country country=new Country();
country.setCode("USD");
country.setIsoCode("USD");
country.setName("United States Dollar");


return countryRepository.saveAndFlush(country);
}
}
liquibase.properties
jpa:
hibernate:
ddl-auto: update
Directory Structure
Local Database Changes
$ mvn clean package -DskipTests
Liquibase Diff Command
Liquibase Update Command

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavan Jadda

Pavan Jadda

Full Stack Developer(Java, Angular and React)