Flyway migration and database evolving library with useful configurations cross different environments on Spring-boot.
SpringBoot with
- Flyway like migration library used cross environments (See down)
- Freemarker template engine on frontend
- Spring data like ORM for data
- SpringBoot app configured for different environments
Flyway is configured via flyway-maven-plugin to simply switch between different environments. In this example we have 3 environments dev, test and preprod. All of them have separate configurations. So in theory they can have different data structure, different data, different db drivers.
More obvious is to have same data structures cross environments but different data. Same like in this Spring Boot application example.
For each environment we create maven profile. Profiles will just set properties for main plugin build.
In whole solution i am using vagrant-mysql database.Feel free to use your own.
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>4.0.3</version>
<configuration>
<driver>${db.driver}</driver>
<url>${db.url}</url>
<user>${db.user}</user>
<password>${db.password}</password>
<locations>
<location>${flyway.location}</location>
</locations>
</configuration>
</plugin>
<profile>
<id>DEV-environment</id>
<properties>
<db.driver>com.mysql.jdbc.Driver</db.driver>
<db.url>jdbc:mysql://33.33.33.1/flywaydemodev</db.url>
<db.user>root</db.user>
<db.password>root</db.password>
<flyway.location>filesystem:src/main/resources/db/migration/dev</flyway.location>
</properties>
</profile>
mvn flyway:migrate -P DEV-environment
After that is database prepared for application run. Database has schema and inserted data.
For app run you need just change in application.properties following
environment=dev
After that you can see application with dev database and data in it.
<profile>
<id>TEST-environment</id>
<properties>
<db.driver>com.mysql.jdbc.Driver</db.driver>
<db.url>jdbc:mysql://33.33.33.1/flywaydemotest</db.url>
<db.user>root</db.user>
<db.password>root</db.password>
<flyway.location>filesystem:src/main/resources/db/migration/test</flyway.location>
</properties>
</profile>
mvn flyway:migrate -P TEST-environment
Before this step the database is empty. After that is database prepared for application run. Database has schema and inserted data.
For app run you need just change in application.properties following
environment=test
Before this step the database is empty. After that you can see application with test database and data in it.
<profile>
<id>PREPROD-environment</id>
<properties>
<db.driver>com.mysql.jdbc.Driver</db.driver>
<db.url>jdbc:mysql://33.33.33.1/flywaydemopreprod</db.url>
<db.user>root</db.user>
<db.password>root</db.password>
<flyway.location>filesystem:src/main/resources/db/migration/preprod</flyway.location>
</properties>
</profile>
mvn flyway:migrate -P PREPROD-environment
After that is database prepared for application run. Database has schema and inserted data.
For app run you need just change in application.properties following
environment=preprod