将 Flyway 与 MongoDB 和 Spring Boot 结合使用

问题描述 投票:0回答:1

我正在尝试将 Flyway 与 MongoDB 和 Spring Boot 结合使用。在其他项目中,我成功地将 Flyway 与 SQL DB 结合使用,并且 Spring 自动运行迁移。然而,对于 MongoDB,Spring Boot 自动配置似乎并未像平常那样初始化

FlywayMigrationInitializer

我按照Redgate的文档将依赖项添加到我的

pom.xml
https://documentation.red-gate.com/flyway/flyway-cli-and-api/supported-databases/mongodb

对于 Spring Boot 部分,我尝试从这些 Github 问题中拼凑出一些示例:

这是我的

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>mongo-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mongo-app</name>
    <description>mongo-app</description>

    <properties>
        <java.version>17</java.version>
        <flyway.version>10.21.0</flyway.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>${flyway.version}</version>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-database-mongodb</artifactId>
            <version>${flyway.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.github.kornilova203</groupId>
                    <artifactId>mongo-jdbc-driver</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.kornilova203</groupId>
            <artifactId>mongo-jdbc-driver</artifactId>
            <version>1.19</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/mongo-jdbc-standalone-1.19.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这是我的

application.yml

spring:
  application:
    name: mongo-app
  data:
    mongodb:
      uri: mongodb://localhost:27017
      database: example
  datasource:
    url: jdbc:mongodb://localhost:27017/example
    driver-class-name: com.dbschema.MongoJdbcDriver
  flyway:
    url: jdbc:mongodb://localhost:27017/example
    driver-class-name: com.dbschema.MongoJdbcDriver
    environment: "mongodb"
    fail-on-missing-locations: true
    locations: classpath:db/migration
    sqlMigrationSuffixes:
      - ".js"
    enabled: true

为了让 Flyway 迁移自动运行,我缺少什么?

可以在此处找到该应用程序的完整源代码:https://github.com/cfinlinson-incomm/mongodb-app

mongodb spring-boot maven flyway
1个回答
0
投票

我通过对我的

pom.xml
进行一些额外的更改来实现此功能。

首先,如果

FlywayConfiguration
位于类路径中,则 Spring Boot 才会激活
JdbcUtils
类。为了解决这个问题,我将此依赖项添加到我的
pom.xml
:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

其次,正如 M. Deinum 在评论中所解释的那样,具有范围

system
的依赖项不包含在最终的 jar 中,因此虽然这在通过 IntelliJ 本地运行时对我有用,但当我将其部署到测试环境时它失败了。为了解决这个问题,我修改了
spring-boot-maven-plugin
,如下所示:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <includeSystemScope>true</includeSystemScope>
  </configuration>
</plugin>
© www.soinside.com 2019 - 2024. All rights reserved.