Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Flyway migration placeholder

Can someone show the right format to use to use application.properties config in Flyway migrations.

I'd like to use the username for the datasource config in my application.properties file to grant permissions on a database table (db migration using Flyway, username will ultimately vary between environments), however I can't find an example of the syntax.

Example application.properties:

#  Database
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/example_db
spring.datasource.username=example_db_application
spring.datasource.password=examplePassword1

Migration:

CREATE TABLE token
(
  id TEXT,
  value TEXT,
);

GRANT SELECT, INSERT, UPDATE, DELETE ON token TO ${spring.datasource.username};

I've tried various iterations (flyway.placeholders.spring.datasource.username, tried specifying no prefix: spring.flyway.placeholder-prefix=) but no luck.

like image 516
user1016765 Avatar asked Mar 07 '18 21:03

user1016765


People also ask

What is Flyway placeholder?

Default placeholders ${flyway:user} = The user Flyway will use to connect to the database. ${flyway:database} = The name of the database from the connection url. ${flyway:timestamp} = The time that Flyway parsed the script, formatted as 'yyyy-MM-dd HH:mm:ss' ${flyway:filename} = The filename of the current script.

What is Flyway migration in spring boot?

Spring boot flyway is application software of database migration which was used to migrate, validate, undo, clean, repair, and baseline commands of SQL. Flyway framework is basically used to update the database version using migration tools.

Where are Flyway migrations stored?

By default Flyway will look for migrations on the classpath under db/migration, which on a Maven project means src/main/resources/db/migration. You can however also use a location starting with the filesystem: prefix that can be anywhere on your disk.

How do you skip Flyway migration?

The hotfix migration can be deployed with Flyway with skipExecutingMigrations=true . The schema history table will be updated with the new migration, but the script itself won't be executed again. skipExecutingMigrations can be used with with cherryPick to skip specific migrations.


1 Answers

Spring-Boot provides a common application-property for flyway migration placeholder-values under the path spring.flyway.placeholders.*=

Usage

# application.properties
# -> placeholder value `user`
spring.flyway.placeholders.user=joe

-- db/migration/V3__Migration_With_Placeholder.sql`:

CREATE TABLE ${user} (
   ...
)
like image 120
fateddy Avatar answered Oct 24 '22 09:10

fateddy