0
0

【Spring Boot】flyway-database-mongodbでMongoDBに対してマイグレーションを行う

Posted at

概要

Spring Bootでは、SpringBoot x Flywayで DBマイグレーションを行うの記事にある通り、Flywayでマイグレーションを行う設定ができるのですが、MongoDBに対してマイグレーションを行う際に少しハマったので内容をメモ書きします。

前提

  • 使用した言語はJavaになります。(Kotlinでもほぼ同様の設定で大丈夫と思いますが)
  • 使用したflyway-database-mongodbのバージョンは10.13.0になります。
  • ビルドツールはgradleを使用します。

設定内容

設定の概要等は、redgateというツールのMongoDBの部分を参考にしました。こちらはSpringBootの設定ではないようですが、ほぼこの内容に沿った形になります。

1. gradleの設定

留意点としては、今回使用したflyway-database-mongodbで使用しているcom.github.kornilova203:mongo-jdbc-driverのjarを別途ダウンロードする必要がある点です。(今回は1.19のバージョンのjarを使用しています)
事前準備としてこちらからjarをダウンロードして、mongo-jdbc-driver-1.19.jarにリネームの上プロジェクトのどこかに配置します。(今回はlibs配下に配置)

以下がgradleで設定を追加した部分です。

repositories {
	mavenCentral()
	maven { url 'https://repo.spring.io/milestone' }
	maven { url 'https://repo.spring.io/snapshot' }
	flatDir {
		dirs 'libs'
	}
}

dependencies {
	implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '3.2.5'
	implementation 'com.github.kornilova203:mongo-jdbc-driver:1.19'
	implementation 'org.flywaydb:flyway-database-mongodb:10.13.0'
}

2. application.propertiesの設定

以下が設定を追加した部分です。

spring.datasource.url=jdbc:mongodb://localhost:27017/sample_db
spring.datasource.driver-class-name=com.dbschema.MongoJdbcDriver
spring.flyway.sqlMigrationSuffixes=.js
spring.flyway.baseline-version=0
spring.flyway.enabled=true
spring.graphql.graphiql.enabled=true

3. マイグレーション用のjsファイル

マイグレーションで標準のパス、[resources]-[db]-[migration]配下にjsファイルを配置します。
今回はuser_accountsというドキュメントに対して、indexの設定を行ってます。

db.user_accounts.createIndex( { user_setting_id: 1 }, { unique: true } );
db.user_accounts.createIndex( { email: 1 }, { unique: true } );

その他参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0