微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Flyway配置来处理多个数据库

我们有一个使用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

解决方法

您可以为具有不同配置的单个插件指定多个执行:
<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的迁移.以filesystem开头的位置:指向文件系统上的目录,可能只包含sql迁移.

原文地址:https://www.jb51.cc/mssql/80003.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐