In the previous post, I showed you how to use database migration with Liquibase in Spring MVC. So what about Spring Boot? In this tutorial, I will show you how to do this! It’s really simple!
First, I will create a new Spring Boot project:
with Liquibase Migration, Spring Data JDBC and PostgreSQL dependencies as follows:
The purpose I use Spring Data JDBC is to configure the dataSource for Liquibase and I will use PostgreSQL for this tutorial.
Result:
If you pay attention, you will see that the Spring Starter Project in the Spring Tool Suite has generated the src/main/resources/db/changelog directory. This is the directory containing the changelog file of Liquibase.
As you all know, for Liquibase to work, we need to declare the dataSource and the path to the changelog file, so now we will define them one by one.
We will define the dataSource information for Liquibase in the application.properties file.
1 2 3 |
spring.datasource.url=jdbc:postgresql://localhost:5432/liquibase_example spring.datasource.username=khanh spring.datasource.password=1 |
If you run this application now, you will see the following error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.5) 2021-05-13 05:54:48.936 INFO 79548 --- [ main] c.h.s.SpringBootLiquibaseApplication : Starting SpringBootLiquibaseApplication using Java 15.0.1 on Khanhs-MacBook-Pro.local with PID 79548 (/Users/khanh/Documents/workspace-spring-tool-suite-4-4.9.0.RELEASE/spring-boot-liquibase/target/classes started by khanh in /Users/khanh/Documents/workspace-spring-tool-suite-4-4.9.0.RELEASE/spring-boot-liquibase) 2021-05-13 05:54:48.938 INFO 79548 --- [ main] c.h.s.SpringBootLiquibaseApplication : No active profile set, falling back to default profiles: default 2021-05-13 05:54:49.238 INFO 79548 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode. 2021-05-13 05:54:49.244 INFO 79548 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 4 ms. Found 0 JDBC repository interfaces. 2021-05-13 05:54:49.457 INFO 79548 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2021-05-13 05:54:49.607 INFO 79548 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2021-05-13 05:54:50.904 INFO 79548 --- [ main] l.lockservice.StandardLockService : Successfully acquired change log lock 2021-05-13 05:54:50.932 INFO 79548 --- [ main] l.lockservice.StandardLockService : Successfully released change log lock 2021-05-13 05:54:50.935 WARN 79548 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.ChangeLogParseException: Error parsing classpath:/db/changelog/db.changelog-master.yaml 2021-05-13 05:54:50.935 INFO 79548 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2021-05-13 05:54:50.939 INFO 79548 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 2021-05-13 05:54:50.949 INFO 79548 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-05-13 05:54:50.964 ERROR 79548 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Liquibase failed to start because no changelog could be found at 'classpath:/db/changelog/db.changelog-master.yaml'. Action: Make sure a Liquibase changelog is present at the configured path. |
By default, Spring Boot will read the Liquibase changelog file defined in the YAML file, in the src/main/resouces/db/changelog/ directory, named db.changelog-master.yaml. You can change this default configuration using property spring.liquibase.change-log.
I will define a changelog file with XML format in the directory src/main/resources/db/changelog with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.0.xsd"> <changeSet id="1" author="khanh"> <preConditions onFail="MARK_RAN"> <not> <tableExists tableName="clazz" /> </not> </preConditions> <createTable tableName="clazz"> <column name="id" type="BIGINT" autoIncrement="true"> <constraints primaryKey="true" nullable="false" /> </column> <column name="name" type="VARCHAR(50)" /> </createTable> </changeSet> </databaseChangeLog> |
Configure the spring.liquibase.change-log property in the application.properties file:
1 |
spring.liquibase.change-log=classpath:db/changelog/db-changelog.xml |
Now, run the application again, check the database, you will see the following results: