Search code examples
mavenflyway

Using Maven + Flyway to Migrate/Clean/etc multiple databases


I have a maven project with multiple databases that we are using flyway to version:

        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>6.0.8</version>
            <dependencies>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>42.2.5</version>
                </dependency>
            </dependencies>
            <configuration>
                <configFiles>/etc/hG/application.properties</configFiles>
            </configuration>
            <executions>
                <execution>
                    <id>wave</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                        <goal>info</goal>
                    </goals>
                    <configuration>
                        <url>${wave.flyway.url}</url>
                        <user>${db.aws.user}</user>
                        <password>${db.aws.pass}</password>
                        <driver>${flyway.driver}</driver>
                        <locations>
                            <location>
                                filesystem:src/main/resources/wave/migration
                            </location>
                        </locations>
                    </configuration>
                </execution>
                <execution>
                    <id>tracks</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                        <goal>info</goal>
                    </goals>
                    <configuration>
                        <url>${tracks.flyway.url}</url>
                        <user>${db.timescale.tracks.user}</user>
                        <password>${db.timescale.tracks.pass}</password>
                        <driver>${flyway.driver}</driver>
                        <locations>
                            <location>
                                filesystem:src/main/resources/tracks/migration
                            </location>
                        </locations>
                    </configuration>
                </execution>
                <execution>
                    <id>osm</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                        <goal>info</goal>
                    </goals>
                    <configuration>
                        <url>${osm.flyway.url}</url>
                        <user>${db.osm.user}</user>
                        <password>${db.osm.pass}</password>
                        <driver>${flyway.driver}</driver>
                        <locations>
                            <location>
                                filesystem:src/main/resources/osm/migration
                            </location>
                        </locations>
                    </configuration>
                </execution>
            </executions>
        </plugin>

I want to be able to use the mvn flyway:clean, flyway:migrate, flyway:info commands from the command line, however they are not working:

[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:6.0.8:clean (default-cli) on project DataServices: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!

I suspect this is because I am not using the standard flyway properties in the application.properties file I am using for configuration, however that also suggests to me that if I were to use those standard properties, it would only apply to a single database. Is there a way I can set up my maven file to clean each database, or even do them based on the ID of the execution (wave, tracks, osm) so they use those properties?

UPDATE: I was thinking I could essentially repeat the process for cleaning that I am using for migrations by making 3 more execution blocks that work on the clean phase like so:

                <execution>
                    <id>wave-clean</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                    <configuration>
                        <url>${wave.flyway.url}</url>
                        <user>${db.aws.user}</user>
                        <password>${db.aws.pass}</password>
                        <driver>${flyway.driver}</driver>
                        <locations>
                            <location>
                                filesystem:src/main/resources/wave/migration
                            </location>
                        </locations>
                    </configuration>
                </execution>

...and repeat for the other two. However, now I am getting this error:

[INFO] --- flyway-maven-plugin:7.11.4:clean (wave-clean) @ DataServices ---
[WARNING] Discarding INCOMPLETE dataSource configuration! flyway.url must be set.

Solution

  • I was on the right track with the update posted in the question, but I didn't realize that I wasn't reading in the properties for the clean lifecycle, so I had to add an execution to my properties-maven-plugin block:

                    <execution>
                        <id>pre-clean-read-proj-props</id>
                        <phase>pre-clean</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>/etc/hG/application.properties</file>
                            </files>
                        </configuration>
                    </execution>
    

    This read the configuration file and got rid of the error, and now mvn clean cleans all databases as expected!