LoginSignup
1
3

More than 5 years have passed since last update.

Gradle Wrapper で Flyway 付き SQL 配布ファイルを作る

Last updated at Posted at 2017-10-17

背景

  • Gradle 4 + MySQL 5.7 + Spring Boot 1.5 で開発
  • 勤務先はシンクライアントに近い環境。社則で毎日退社前に個人PC上のデータを消す必要がある (アプリは別)
    • → マイグレーションツール必須
  • どうせならステージング環境等のDBマイグレーションも開発に使っているものと同じツールを使って省力化したい
    • でも Spring Boot + flyway-core の強制自動マイグレーションは怖い

→ サーバ用の SQL 配布アーカイブに、Flyway タスクのみを入れた build.gradle を足してみます。

サーバに配布する build.gradle

plugins {
    id 'org.flywaydb.flyway' version '3.2.1'
}
// java プラグインを入れないと JDBC ドライバの dependencies を解決してくれない
apply plugin: 'java'

flyway {
    url = 'jdbc:mysql://localhost/foo'
    user = xxx
    password = xxx
}
flywayClean.enabeld = false // 誤爆防

repositories {
    jcenter()
}
dependencies {
    runtime group: 'mysql', name: 'mysql-connector-java', version: '5.1.44'
}

展開して ./gradlew flywayMigrate で SQL を適用できます。
なお Gradle のキャッシュは $HOME/.gradle に入ります。

サーバ用 SQL 配布アーカイブを作成するための Gradle タスク

tarball は build/distributions/ 以下に作成されます。

task sqlDist(type: Tar) {
    compression = Compression.GZIP
    archiveName = "${baseName}-sql-${version}.tar.gz"

    // SQL files
    into('src') {
        from 'src'
        include 'main/resources/db/migration/**'
    }

    // Gradle Wrapper
    into('/') {
        from 'config/flyway'
        include 'build.gradle'
    }
    into('/') {
        from '.'
        include 'gradlew'
    }
    into('gradle') {
        from 'gradle'
    }
}
1
3
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
1
3