Spring Boot with Flyway Placeholders
1 min readJun 2, 2020
Play around with multiple schemas in your flyway scripts by using Flyway placeholders.
Requirement
In each environment, we need to support two Database schemas. The schema should not be hardcoded and take the environment name as a suffix.
application.yaml
Our configuration:
spring:
datasource:
url: jdbc:postgresql://${database_name}/${service_database_name}
username: ${database_user}
password: ${database_password}
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
default_schema: test_${spring.profiles.active}
flyway:
schemas: test_${spring.profiles.active}
out-of-order: true
baseline-on-migrate: true
placeholder-prefix: $$$
placeholder-suffix: $$$
placeholders:
raw_schema: test_raw_${spring.profiles.active}
Default Flyway Schema: This is were flyway scripts will be executed.
flyway:
schemas: ${database_schema_name}
Second Schema: This is the second schema which we will use in our flyway scripts.
The default prefix is ‘${‘, which is not suitable if you want to use spring properties. So I have updated the placeholder prefix and suffix as we need to use spring properties.
spring:
flyway:
placeholder-prefix: $$$
placeholder-suffix: $$$
placeholders:
raw_schema: test_raw_${spring.profiles.active}
Flyway Script
Now you can use the placeholder like this:
create schema if not exists $$$raw_schema$$$;