使用 Maven + Flyway 迁移/清理/等多个数据库

Posted

技术标签:

【中文标题】使用 Maven + Flyway 迁移/清理/等多个数据库【英文标题】:Using Maven + Flyway to Migrate/Clean/etc multiple databases 【发布时间】:2021-09-30 19:52:24 【问题描述】:

我有一个包含多个数据库的 maven 项目,我们正在使用 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>

我希望能够从命令行使用mvn flyway:cleanflyway:migrateflyway:info 命令,但是它们不起作用:

[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!

我怀疑这是因为我没有在用于配置的 application.properties 文件中使用标准 flyway 属性,但这也向我表明,如果我要使用这些标准属性,它只会适用于单一数据库。有没有一种方法可以设置我的 maven 文件来清理每个数据库,甚至可以根据执行的 ID(wave、tracks、osm)进行清理,以便它们使用这些属性?

更新:我想我基本上可以重复我用于迁移的清理过程,方法是再制作 3 个在 clean 阶段工作的执行块,如下所示:

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

...然后重复其他两个。但是,现在我收到此错误:

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

【问题讨论】:

【参考方案1】:

我在问题中发布的更新走在正确的轨道上,但我没有意识到我没有阅读干净生命周期的属性,所以我不得不在我的 properties-maven-plugin 块中添加一个执行:

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

这读取了配置文件并摆脱了错误,现在mvn clean按预期清理了所有数据库!

【讨论】:

以上是关于使用 Maven + Flyway 迁移/清理/等多个数据库的主要内容,如果未能解决你的问题,请参考以下文章

如何手动清理针对数据库运行的最后一次 flyway 迁移

Flyway 在 Maven 多模块项目中找不到迁移

Flyway:如何支持清理具有相同生命周期的多个模式?

flyway 后的 Flyway 迁移错误:基线

SpringBoot整合Flyway

SpringBoot整合Flyway