Spring Boot Flyway 迁移占位符

问题描述 投票:0回答:2

有人可以展示在 Flyway 迁移中使用 application.properties 配置的正确格式吗?

我想在 application.properties 文件中使用数据源配置的用户名来授予数据库表的权限(使用 Flyway 进行数据库迁移,用户名最终会因环境而异),但是我找不到示例句法。

示例应用程序.属性:

#  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

迁移:

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

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

我尝试过各种迭代(flyway.placeholders.spring.datasource.username,尝试不指定前缀:spring.flyway.placeholder-prefix =)但没有运气。

spring spring-boot flyway application.properties
2个回答
12
投票

Spring-Boot 为路径下的 Flyway 迁移占位符值提供了一个通用的应用程序属性

spring.flyway.placeholders.*=

使用方法

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

-- db/migration/V3__Migration_With_Placeholder.sql`:

CREATE TABLE ${user} (
   ...
)

0
投票

如果您的配置位于 yaml 中,则示例为:

spring:
  flyway:
    placeholders:
      APP_DB_USER: ${DB/User}
      APP_DB_PASSWORD: ${DB/Password}
    

您的 V1_0__create_role.sql 可能是:

 CREATE ROLE "${APP_DB_USER}" LOGIN
 PASSWORD '${APP_DB_PASSWORD}' NOSUPERUSER INHERIT CREATEDB NOCREATEROLE;

您还可以影响占位符格式:

 spring:
   flyway:
     placeholder-prefix: $$$
     placeholder-suffix: $$$
© www.soinside.com 2019 - 2024. All rights reserved.