We have designed the Headstart framework to be database-agnostic to support major database products with the help of Flyway. To achieve this database agnosticism, Headstart uses Spring Data/Hibernate but relies on migration scripts for creating database tables. We run these scripts using Flyway via Maven. Therefore, when there are changes to the database, we craft new migration scripts and apply the changes using Flyway.
Choose Headstart Framework Database Using Maven Profiles
The Headstart framework supports specific versions of MariaDB, MySQL, Microsoft SQL, Oracle, and PostgreSQL. To choose which database to use, we use a Maven profile corresponding to the RDBS we want our application to use. Consider the following snippet from the pom.xml file.
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 | <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <!-- Omitted for brevity --> <profiles> <!-- Start: PostgreSQL --> <profile> <id>db-postgresql</id> <properties> <flyway.configFiles>src/main/config/database/flyway/pg/flyway.conf</flyway.configFiles> </properties> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> </dependencies> </profile> <!-- End: PostgreSQL --> <!-- Omitted for brevity --> </profiles> </project> |
Each Maven profile has a dependency on a supported database and a location to a Flyway configuration for the Headstart Framework to use. The Maven profile allows us to build and run our application with the database of our choice. Meanwhile, Flyway configuration allows Flyway to successfully run the appropriate migration scripts for the following Maven commands.
1 2 | mvn flyway:migrate mvn flyway:clean |
Application Properties for Headstart Framework
When we decide and configure which database to use, we start up the Headstart application using a set of .properties files for a specific environment and Spring profiles. These .properties files reside within a particular codebase directory but outside the resources directory, while the Spring profiles allow us to use additional .applications files.
Consider the following subdirectories under the src/main/config/env in the Headstart Framework codebase.
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 | +---database ¦ +---flyway ¦ ¦ +---mariadb ¦ ¦ ¦ flyway.conf ¦ ¦ ¦ ¦ ¦ +---mssql ¦ ¦ ¦ flyway.conf *** Removed for brevity *** ¦ ¦ ¦ ¦ ¦ +---oracle ¦ ¦ ¦ flyway.conf ¦ ¦ ¦ ¦ ¦ +---pg ¦ ¦ flyway.conf ¦ ¦ ¦ +---migration *** Removed for brevity *** ¦ ¦ ¦ +---pg ¦ V1_0_0__create_core_tables.sql ¦ V2_0_0__seed_role_permission_tables.sql ¦ +---env +---dev ¦ application-dbmariadb.properties ¦ application-dbmssql.properties ¦ application-dbpg.properties ¦ application-dev.properties ¦ application.properties ¦ +---prod *** Removed for brevity *** ¦ +---qa *** Removed for brevity *** |
For the dev environment, we have application.properties, application-dev.properties, and .properties file for a specific database. To run Headstart using PostgreSQL, we use the following VM options.
1 | -Dspring.config.location=<PATH_TO_PROJECT>/src/main/config/env/dev/ -Dspring.profiles.active=dev,dbpg |
Then, Headstart uses the files application.properties, application-dev.properties, and application-dbpg.properties in that order, and Spring Boot loads the values accordingly.