I'm trying to use Flyway with MongoDB and Spring Boot. In other projects, I've successfully used Flyway with SQL DBs, and Spring automatically runs the migrations. With MongoDB, however, it seems that the that Spring Boot autoconfiguration isn't initializing the FlywayMigrationInitializer
as it normally does.
I followed Redgate's documentation to add the dependencies to my pom.xml
: https://documentation.red-gate.com/flyway/flyway-cli-and-api/supported-databases/mongodb.
And for the Spring Boot part, I've tried to piece together examples from these Github issues:
Here's my 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>
Here's my 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
What am I missing to get Flyway migrations to run automatically?
Full source code for the app can be found here: https://github.com/cfinlinson-incomm/mongodb-app.
I got this working by making some additional changes to my pom.xml
.
First, the FlywayConfiguration
class only gets activated by Spring Boot if JdbcUtils
is in the classpath. To resolve that, I added this dependency to my pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Second, as M. Deinum explained in a comment, dependencies with scope system
are not included in the final jar, so while this worked for me when running locally via IntelliJ, it failed when I deployed it to a test environment. To resolve that, I modified the spring-boot-maven-plugin
like so:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>