liquibase migration on spring boot startup (SpringLiquibase)
Prerequisites:
- mysql database available to be used. I’m using jdbc:mysql://localhost:3306/liquibase_test
- I’m using Netbeans. Other IDE also can be used.
Steps are…
- Create project from “Spring initialz” with 3 dependencies:
a. liquibase
b. mysql
c. jpa
- Open generated project in netbeans.
- Change application.properties to application.yml and paste the content below in it.
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/liquibase_test username: greenhorn password: greenhorn liquibase: check-change-log-location: true enabled: true
- Create and insert the below in “db.changelog-test.xml” file and place the file in src/main/resources/db/mysql/changelog (create if the directory doesn’t exist):
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> <changeSet author="generated" id="tbl-1"> <createTable tableName="test_tbl"> <column name="tblId" type="VARBINARY(36)"> <constraints nullable="false"/> </column> <column name="createdDate" type="datetime"> <constraints nullable="false"/> </column> <column name="updatedDate" type="datetime"/> <column name="fk_createdBy" type="VARBINARY(36)"> <constraints nullable="false"/> </column> <column name="fk_updatedBy" type="VARBINARY(36)"/> <column name="active" type="BIT(1)"/> <column name="deleted" type="BIT(1)"/> <column name="content" type="VARCHAR(255)"> <constraints nullable="false"/> </column> <column name="version" type="INT"/> </createTable> </changeSet> <changeSet author="generated" id="tbl-2"> <addPrimaryKey columnNames="tblId" constraintName="PRIMARY" tableName="test_tbl"/> </changeSet> </databaseChangeLog>
- Create LiquibaseConfig.java file and paste the content below in it:
@Configuration public class LiquibaseConfig { @Autowired private DataSource dataSource; @Autowired private ResourceLoader resourceLoader; @Bean public SpringLiquibase liquibase() throws Exception { // Locate change log file String changelogFile = "classpath:db/mysql/changelog/db.changelog-test.xml"; Resource resource = resourceLoader.getResource(changelogFile); Assert.state(resource.exists(), "Unable to find file: " + resource.getFilename()); // Configure Liquibase SpringLiquibase liquibase = new SpringLiquibase(); liquibase.setChangeLog(changelogFile); liquibase.setDataSource(dataSource); liquibase.setDropFirst(true); liquibase.setShouldRun(true); // Verbose logging Map<String, String> params = new HashMap<>(); params.put("verbose", "true"); return liquibase; } }
- Execute the project using mvn spring-boot:run
- Examine the database in jdbc:mysql://localhost:3306/liquibase_test
- End.
NOTE The sample spring boot maven project used for this exercise can be downloaded from: |
---|
References: