Java 如何使用 Flyway 配置来处理多个数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23545657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Flyway configuration to handle multiple databases
提问by Federico Piazza
we have a java application configured with maven that uses multiple databases. It is one app - many schemas.
我们有一个配置了 maven 的 java 应用程序,它使用多个数据库。它是一个应用程序 - 许多模式。
I've configured flyway, tested and it works well but my config is only for one database.
我已经配置了 flyway,经过测试并且运行良好,但我的配置仅适用于一个数据库。
Here is my pom.xml tested with one schema:
这是我用一种模式测试的 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>
Update: by using the answer provided now I have the following pom.xml configured with 2 schemas.
更新:通过使用现在提供的答案,我使用 2 个模式配置了以下 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>
<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>
I execute flyway operations but none worked, here is the error I got:
我执行飞行操作但没有工作,这是我得到的错误:
[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!
The database configuration is ok. Also I checked the schemas are ok What I am missing?
数据库配置没问题。我还检查了模式是否正常我缺少什么?
UPDATE: I removed from the command line flyway: and it worked well. Thanks Jk1
更新:我从命令行 flyway 中删除:并且它运行良好。谢谢Jk1
采纳答案by Jk1
You can specify multiple executions for a single plugin with different configurations:
您可以为具有不同配置的单个插件指定多个执行:
<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>
You can use 'location' property to define migrations you want to run for each schema, just like it's done in example above. The location type is determined by its prefix. Unprefixed locations or locations starting with classpath: point to a package on the classpath and may contain both sql and java-based migrations. Locations starting with filesystem: point to a directory on the filesystem and may only contain sql migrations.
您可以使用 'location' 属性来定义要为每个架构运行的迁移,就像在上面的示例中所做的那样。位置类型由其前缀决定。不带前缀的位置或以 classpath 开头的位置:指向 classpath 上的包,并且可能包含基于 sql 和 java 的迁移。以文件系统开头的位置:指向文件系统上的一个目录,并且可能只包含 sql 迁移。