如何使用Flyway配置来处理多个数据库 [英] How to use Flyway configuration to handle multiple databases

查看:1799
本文介绍了如何使用Flyway配置来处理多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个配置了使用多个数据库的maven的java应用程序。它是一个应用程序很多模式。

we have a java application configured with maven that uses multiple databases. It is one app - many schemas.

我已经配置flyway,测试,它的工作很好,但我的配置只是一个数据库。

I've configured flyway, tested and it works well but my config is only for one database.

这是我用一个模式测试的pom.xml:

Here is my pom.xml tested with one schema:

<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个模式。 / p>

Update: by using the answer provided now I have the following pom.xml configured with 2 schemas.

<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操作,但没有工作,这里是我得到的错误:

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?

更新:我从命令行flyway:和它工作得很好。感谢 Jk1

UPDATE: I removed from the command line flyway: and it worked well. Thanks 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>

您可以使用location属性来定义要为每个模式运行的迁移,在上面的示例中完成。位置类型由其前缀确定。未定义的位置或以classpath开头的位置:指向类路径上的包,并且可能包含基于sql和基于java的迁移。以文件系统开头的位置:指向文件系统上的目录,并且只能包含sql迁移。

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.

这篇关于如何使用Flyway配置来处理多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆