How do placeholders work in Flyway?

后端 未结 4 650
日久生厌
日久生厌 2020-12-09 11:00

I\'m evaluating Flyway for use in my project. Our current SQL scripts contain placeholders for things like URLs which will have a different domain names depending on the env

相关标签:
4条回答
  • 2020-12-09 11:32

    Currently when supplying placeholders as properties, the property name should be prefixed with flyway.placeholders.

    For example the ${env.token} placeholder can be specified directly as this Ant property: flyway.placeholders.env.token

    There is currently no support for passing a property file directly, without using prefixes for the property names. Feel free to raise an issue in the Issue Tracker. :-)

    0 讨论(0)
  • 2020-12-09 11:49

    Maven version:

    <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>
    
    0 讨论(0)
  • 2020-12-09 11:50

    From my experience, it's much easier to use environment variables instead of CLI or config file (especialy when using docker and k8s).

    You can use environment variables in the following format -

    export FLYWAY_PLACEHOLDERS_USER=${USER}
    

    Then in you sql statement, use this variable like this -

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

    read more about environment variables here

    0 讨论(0)
  • 2020-12-09 11:57

    If the token was subdomain:

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

    The values in flyway.conf:

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

    Or command line:

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

    Would run the script as:

    INSERT INTO FEED VALUES ('app.example.company.org/feed1', 'My Feed');
    
    0 讨论(0)
提交回复
热议问题