Uncle Bob (Robert C. Martin) gave us greate first 5 design principles for Object Oriented Programming known as S.O.L.I.D.
S.O.L.I.D STANDS FOR:
- S – Single-responsiblity principle
- O – Open-closed principle
- L – Liskov substitution principle
- I – Interface segregation principle
- D – Dependency Inversion Principle
Let’s go over all principles and understand them through example.
1 ) S – Single-responsiblity principle :
Class should have one and only one job.
2) O – Open-closed principle :
Objects or entities should be open for extension, but closed for modification.
3) L – Liskov substitution principle
Derived types must be completely substitable for its base/parent class.
4) I –Interface segregation principle
Client should be forced to implement unneccessary method/functions which they will not use.
5) D – Dependency Inversion Principle
Entities must depend on abstraction, not on concreation.
Recently while working on project, we came to a requirement where we need to create new database on mysql and then migrate data from script file.
Three options came to my picture – 1. Execute Sql Scripts using Spring JDBC template
2. Use Flyway to do migration
3.Use Liquibase to do migration
So We thought to get a try on Spring boot and Liquibase which will help us in data migration. Spring boot support liquibase by default if we provide liquibase properties in application.property file. Once you added property and changeLog files, Spring boot while application is coming up, it will run migration from Changelog file and apply it on database. But we were in situation where we want to do migration on demand not on when spring boot application is coming up.
Steps to create Spring Boot application with Liquibase (Assumpation you are using STS)
- Create New Spring Starter project
- Select JPA,Web,Mysql dependency and create Spring boot application
- Add liquibase dependency into gradle.build file
- Add database related properties into application property file
- exclude liquibaseautoconfiguration from spring auto configuration annotation
- Add changeLog file at resource->db->changelog>db.changelog-master.yaml
- add code to run liquibase migration on demand.
Project Structure should be as show below :-
Project sample code is availabe on github repo- GITHUB
Hope this is helpful for who is looking to do on demand data migration using liquibase and Spring boot.