How do placeholders work in Flyway?

I am evaluating Flyway for use in my project. Our current SQL scripts contain placeholders for things like URLs that will have different domain names depending on the environment (dev, qa, prod).

In particular, we may have INSERT statements, such as

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

$ {env.token} must be replaced with "dev", "qa" or "prod".

We have about 50 different properties that may require replacement in SQL scripts. All properties are in one or two property files.

Is there a way to complete the Flyway Ant migration task so that it pulls replacement tokens and values ​​from the properties file? Anything along the lines of the Ant filter task?

+10
source share
4 answers

flyway.placeholders.

, ${env.token} Ant: flyway.placeholders.env.token

, . .: -)

+10

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

script :

INSERT INTO FEED VALUES ('app.example.company.org/feed1', 'My Feed');
+7

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>
0

In my experience, it is much easier to use environment variables instead of the CLI or configuration file (especially when using docker and k8s).

You can use environment variables in the following format -

export FLYWAY_PLACEHOLDERS_USER=${USER}

Then in your SQL statement, use this variable as follows:

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

Learn more about environment variables here.

0
source

All Articles