当前位置: 首页 > 知识库问答 >
问题:

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

闾丘德宇
2023-03-14

我们有一个用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配置了两种模式。

<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个答案

应煌
2023-03-14

您可以使用不同的配置为单个插件指定多个执行:

 <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”属性定义要为每个架构运行的迁移,就像上面的示例中所做的那样。位置类型由其前缀决定。非固定位置或以类路径开头的位置:指向类路径上的包,可能包含基于sql和java的迁移。以文件系统开头的位置:指向文件系统上的目录,并且可能只包含sql迁移。

 类似资料:
  • 问题内容: 我们有一个使用maven配置的Java应用程序,该应用程序使用多个数据库。这是一个应用程序-许多架构。 我已经配置了flyway,已经过测试,并且效果很好,但是我的配置仅适用于一个数据库。 这是我的pom.xml使用一种模式进行测试: 更新:通过使用现在提供的答案,我将以下pom.xml配置为2个模式。 我执行飞行操作,但没有成功,这是我收到的错误: 数据库配置正常。另外,我检查了架构

  • 我正在将Flyway集成到一个现有的遗留项目中,该项目由同一个应用程序的多个数据库组成。该项目使用Maven,我想使用maven-flyway-plugin与flyway集成。 到目前为止,我的工作配置如下所示: 有了这个,我可以像这样分别迁移每个数据库: 不幸的是,这不是非常友好的用户。我希望能够简单地执行并执行所有三个迁移配置。

  • 我对Flyway完全陌生,但我正在尝试使用https://github.com/flyway/flyway-docker描述的docker-compose flyway mysql安排来迁移许多相同的测试数据库 据我所知,< code>migrate命令可以在它的< code>-schemas参数中接受多个模式,但是它似乎只将实际的SQL迁移应用于列表中的第一个模式。 例如,当我使用< code>

  • 我正在使用Spring批处理设置一个作业服务器。我的JdbcCursorItemReader需要配置sql,该sql在每个作业运行的基础上进行更改。因为sql发生了变化,所以我希望阅读器具有@stepscope,这样我就不需要担心sql的状态性了。 所以我设置了这样一个类: 我在整个服务器上使用基于Java的配置。ItemReader的一个实例的bean如下所示: 启动服务器并运行Spring批处

  • 我的WebApp使用多个数据库,我尝试使用GlassFish连接池来管理连接,但我发现配置示例只使用一个数据库。 那么,我该怎么办?创建与我正在使用的数据库数量相同的连接池,或者是否有方法将一个池配置为多个数据库?