14

我正在评估在我的项目中使用的 Flyway。我们当前的 SQL 脚本包含诸如 URL 之类的占位符,这些占位符将根据环境(dev、qa、prod)具有不同的域名。

具体来说,我们可能有 INSERT 语句,例如

INSERT INTO FEED VALUES ('app.${env.token}.company.org/feed1', 'My Feed');

${env.token} 需要替换为“dev”、“qa”或“prod”。

我们有大约 50 个不同的属性,它们可能需要在 SQL 脚本中替换。这些属性都驻留在一个或两个属性文件中。

有没有办法运行 Flyway Ant 迁移任务,以便从属性文件中提取替换标记和值?类似于 Ant 过滤器任务的东西?

4

4 回答 4

16

目前,当提供占位符作为属性时,属性名称应以flyway.placeholders 为前缀。

例如,${env.token}占位符可以直接指定为这个 Ant 属性:flyway.placeholders.env.token

目前不支持直接传递属性文件,而不使用属性名称的前缀。随时在问题跟踪器中提出问题。:-)

于 2012-02-23T20:24:22.110 回答
14

如果令牌是subdomain

INSERT INTO FEED VALUES ('app.${subdomain}.company.org/feed1', 'My Feed');

flyway.conf 中的值:

flyway.url=jdbc:mydb://db
flyway.user=root
flyway.schemas=schema1
flyway.placeholders.subdomain=example

或命令行:

flyway -url=jdbc:mydb://db -user=root -schemas=schema1 -placeholders.subdomain=example migrate

将脚本运行为:

INSERT INTO FEED VALUES ('app.example.company.org/feed1', 'My Feed');
于 2017-03-03T18:03:29.177 回答
1

根据我的经验,使用环境变量而不是 CLI 或配置文件要容易得多(尤其是在使用 docker 和 k8s 时)。

您可以使用以下格式的环境变量 -

export FLYWAY_PLACEHOLDERS_USER=${USER}

然后在你的sql语句中,像这样使用这个变量 -

INSERT INTO tmptable (user)
VALUES ('${user}')

在此处阅读有关环境变量的更多信息

于 2019-06-03T11:13:11.603 回答
0

Maven版本:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <configuration>
                <url>jdbc:mysql://localhost/cloud</url>
                <user>root</user>
                <password>root</password>
                <placeholderReplacement>false</placeholderReplacement>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql.version}</version>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
于 2019-05-06T00:57:00.223 回答