【问题标题】:Spring Boot Flyway migration placeholderSpring Boot Flyway 迁移占位符
【发布时间】:2018-08-16 02:56:10
【问题描述】:

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

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

示例 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

迁移:

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


    【解决方案1】:

    Spring-Boot为路径spring.flyway.placeholders.*=下的flyway迁移占位符值提供了一个通用的application-property

    用法

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

    -- db/migration/V3__Migration_With_Placeholder.sql`:
    
    CREATE TABLE ${user} (
       ...
    )
    

    【讨论】:

    • 行得通 - 例如spring.flyway.placeholders.spring.datasource.username=XYZ 但是考虑到我已经在别处指定了 spring.datasource.username=XYZ ,这非常可怕 - 有没有办法只使用现有属性?
    • spring.datasource.hikari.username=myuser spring.flyway.placeholders.user=${spring.datasource.hikari.username}
    猜你喜欢
    • 2017-10-30
    • 2020-12-31
    • 2019-08-07
    • 2018-10-07
    • 1970-01-01
    • 2018-12-11
    • 2021-07-10
    • 2018-07-29
    • 2014-07-16
    相关资源
    最近更新 更多