如何使用 Flyway 配置处理多个数据库
Posted
技术标签:
【中文标题】如何使用 Flyway 配置处理多个数据库【英文标题】:How to use Flyway configuration to handle multiple databases 【发布时间】:2014-06-26 01:47:39 【问题描述】:我们有一个配置了使用多个数据库的 maven 的 java 应用程序。它是一个应用程序 - 许多架构。
我已经配置了flyway,经过测试,效果很好,但我的配置只适用于一个数据库。
这是我使用一种模式测试的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<!-- Flyway plugin configuration -->
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.0</version>
<configuration>
<url>jdbc:mysql://localhost:3306/argentina</url>
<user>test</user>
<password>test</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<!-- alllll my dependency list -->
</dependency>
<!-- DB dependencies -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</project>
更新:通过使用现在提供的答案,我将以下 pom.xml 配置为 2 个模式。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<id>argentina</id>
<phase>compile</phase> <!--whatever phase you need-->
<goals>
<goal>migrate</goal>
</goals>
<configuration>
<url>jdbc:mysql://localhost:3306/argentina</url>
<user>test</user>
<password>test</password>
<locations>
<location>
filesystem:src/main/resources/db/migration
</location>
</locations>
</configuration>
</execution>
<execution>
<id>brazil</id>
<phase>compile</phase> <!--whatever phase you need-->
<goals>
<goal>migrate</goal>
</goals>
<configuration>
<url>jdbc:mysql://localhost:3306/brazil</url>
<user>test</user>
<password>test</password>
<locations>
<location>
filesystem:src/main/resources/test2/sql
</location>
</locations>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
...
</dependencies>
</project>
我执行了flyway操作,但没有成功,这是我得到的错误:
[INFO] Copying 5 resources
[INFO] [compiler:compile execution: default-compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [flyway:migrate execution: argentina]
[INFO] Database: jdbc:mysql://localhost:3306/argentina (MySQL 5.5)
[INFO] Validated 4 migrations (execution time 00:00.006s)
[INFO] Current version of schema `argentina`: 45678
[INFO] Schema `argentina` is up to date. No migration necessary.
[INFO] [flyway:migrate execution: brazil]
[INFO] Database: jdbc:mysql://localhost:3306/brazil (MySQL 5.5)
[INFO] Validated 1 migration (execution time 00:00.003s)
[INFO] Current version of schema `brazil`: 1
[INFO] Schema `brazil` is up to date. No migration necessary.
[INFO] [flyway:migrate execution: default-cli]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] org.flywaydb.core.api.FlywayException: DataSource not set! Check your configuration!
数据库配置没问题。我还检查了模式是否正常我缺少什么?
更新:我从命令行中删除了 flyway: 并且效果很好。谢谢Jk1
【问题讨论】:
【参考方案1】:您可以为具有不同配置的单个插件指定多个执行:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<id>first-execution</id>
<phase>compile</phase> <!--whatever phase you need-->
<goals>
<goal>migrate</goal>
</goals>
<configuration>
<url>jdbc:mysql://localhost:3306/schema2</url>
<user>root</user>
<password>root</password>
<locations>
<location>
filesystem:/path/to/migrations/folder
</location>
</locations>
</configuration>
</execution>
<execution>
<id>second-execution</id>
<phase>compile</phase> <!--whatever phase you need-->
<goals>
<goal>migrate</goal>
</goals>
<configuration>
<url>jdbc:mysql://localhost:3306/schema1</url>
<user>root</user>
<password>root</password>
<locations>
<location>
filesystem:/path/to/other/migrations/folder
</location>
</locations>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</plugin>
您可以使用“位置”属性来定义要为每个架构运行的迁移,就像在上面的示例中所做的那样。位置类型由其前缀决定。无前缀位置或以类路径开头的位置:指向类路径上的一个包,并且可能包含基于 sql 和 java 的迁移。以文件系统开头的位置:指向文件系统上的一个目录,并且可能只包含 sql 迁移。
【讨论】:
谢谢Jk1,我会尽快测试并确认 我们如何处理迁移?我的意思是如何设置迁移以在其特定架构上运行? 我试过你的conf,但没有奏效。我收到了这个 maven 错误:[INFO] 'run' is specified in an execution, but not found in the plugin 我的错,不小心在配置部分包含了额外的标签。请检查更新的答案。这个版本在我的机器上运行良好,还解决了您关于特定架构特定迁移的第一个问题。 要针对单个执行运行目标,您可以执行“mvn flyway:info@first-execution”之类的操作以上是关于如何使用 Flyway 配置处理多个数据库的主要内容,如果未能解决你的问题,请参考以下文章