web-dev-qa-db-ja.com

Flyway構成を使用して複数のデータベースを処理する方法

複数のデータベースを使用するMavenで構成されたJavaアプリケーションがあります。これは1つのアプリであり、多くのスキーマがあります。

フライウェイを構成してテストしましたが、正常に機能しますが、構成は1つのデータベースのみを対象としています。

これが1つのスキーマでテストされた私の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>

フライウェイ操作を実行しましたが、何も機能しませんでした。次のエラーが発生しました。

[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!

データベースの構成は問題ありません。また、スキーマに問題がないことを確認しました。何が欠けていますか?

更新:コマンドラインフライウェイから削除しました:そしてそれはうまく機能しました。ありがとうJk1

13
Federico Piazza

構成が異なる単一のプラグインに対して複数の実行を指定できます。

 <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移行のみを含めることができます。

16
Jk1