Gradle Flyway plugin

Flyway migration tasks for Gradle.

License

License

Categories

Categories

Gradle Build Tools Flyway Data Databases
GroupId

GroupId

com.github.ben-manes
ArtifactId

ArtifactId

gradle-flyway-plugin
Last Version

Last Version

0.7
Release Date

Release Date

Type

Type

jar
Description

Description

Gradle Flyway plugin
Flyway migration tasks for Gradle.
Project URL

Project URL

https://github.com/ben-manes/gradle-flyway-plugin
Source Code Management

Source Code Management

https://github.com/ben-manes/gradle-flyway-plugin

Download gradle-flyway-plugin

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ben-manes/gradle-flyway-plugin/ -->
<dependency>
    <groupId>com.github.ben-manes</groupId>
    <artifactId>gradle-flyway-plugin</artifactId>
    <version>0.7</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ben-manes/gradle-flyway-plugin/
implementation 'com.github.ben-manes:gradle-flyway-plugin:0.7'
// https://jarcasting.com/artifacts/com.github.ben-manes/gradle-flyway-plugin/
implementation ("com.github.ben-manes:gradle-flyway-plugin:0.7")
'com.github.ben-manes:gradle-flyway-plugin:jar:0.7'
<dependency org="com.github.ben-manes" name="gradle-flyway-plugin" rev="0.7">
  <artifact name="gradle-flyway-plugin" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ben-manes', module='gradle-flyway-plugin', version='0.7')
)
libraryDependencies += "com.github.ben-manes" % "gradle-flyway-plugin" % "0.7"
[com.github.ben-manes/gradle-flyway-plugin "0.7"]

Dependencies

compile (1)

Group / Artifact Type Version
com.googlecode.flyway : flyway-core jar 2.2

Project Modules

There are no modules declared in this project.

Gradle Flyway Plugin (deprecated)

Flyway database migration tasks for Gradle.

Flyway v2.2 includes an [official plugin](http://flywaydb.org/documentation/gradle) based on this one. Note that the feature set diverged after contributing, e.g. removal of multi-db support.

Usage

This plugin is hosted on the Maven Central Repository. All actions are logged at the info level.

See Flyway's command-line arguments for the configuration reference. If the locations configuration is not set and the java plugin is detected then setting is defaulted to the sourceSets.main.output.resourcesDir. If compiled classes are detected then the sourceSets.main.output.classesDir are added to the classpath and classpath:db/migration added to the default locations.

The flyway tasks may include Java migrations. This requires that the classes are compiled in a dependent task. The dependsOnTasks configuration acts as a dependsOn relationship that applies to all flyway tasks. This relationship is not required to allow for the inverse relationship, such as for code generating from the schema. See the gradle-jooq-plugin for one such example.

For a single-database environment, only a single database extension needs to be used. For an environment where deployments to multiple databases must be run as part of the release, defaults can be used in addition to databases. For lists, such as schemas and placeholders, the list that appears in both extensions will be combined. In all other cases, values specified in databases will take precedence over values in defaults.

apply plugin: 'flyway'

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'com.h2database:h2:1.3.170'
    classpath 'com.github.ben-manes:gradle-flyway-plugin:0.7'
  }
}

flyway {
  databases {
    main {
      url = "jdbc:h2:${buildDir}/db/flyway"
    }
  }
}

Tasks

flywayClean

Drops all objects in the configured schemas.

flywayInit

Creates and initializes the metadata table in the schema.

flywayMigrate

Migrates the schema to the latest version.

flywayValidate

Validates the applied migrations against the ones available on the classpath.

flywayInfo

Prints the details and status information about all the migrations.

flywayRepair

Repairs the Flyway metadata table after a failed migration.

Sample for Single Database

flyway {
  dependsOnTasks(compileJava)
  databases {
    main {
      url = "jdbc:h2:${buildDir}/db/flyway"    
      driver = 'org.h2.Driver'
      user = 'SA'
      password = 'mySecretPwd'
      table = 'schema_history'
      schemas = [ 'schema1', 'schema2', 'schema3' ]
      initVersion = '1.0'
      initDescription = 'Base Migration'
      locations = [
        'classpath:com.mycompany.project.migration',
        'filesystem:/sql-migrations',
        'database/migrations'
      ]
      sqlMigrationPrefix = 'Migration-'
      sqlMigrationSuffix = '-OK.sql'
      encoding = 'ISO-8859-1'
      placeholders = [ 
        'aplaceholder': 'value',
        'otherplaceholder': 'value123'
      ]
      placeholderPrefix = '#['
      placeholderSuffix = ']'
      target = '5.1'
      outOfOrder = false
      validateOnMigrate = true
      cleanOnValidationError = false
      initOnMigrate = false
    }
  }
}

Sample for Multiple Databases

flyway {
  dependsOnTasks(compileJava)
  schemaDefaultFirst = true
  defaults {  
    driver = 'org.h2.Driver'
    user = 'SA'
    password = 'mySecretPwd'
    table = 'schema_history'
    schemas = [ 'schema1', 'schema2', 'schema3' ]
    initVersion = '1.0'
    initDescription = 'Base Migration'
    locations = [
      'classpath:com.mycompany.project.migration',
      'filesystem:/sql-migrations',
      'database/migrations'
    ]
    encoding = 'ISO-8859-1'
    placeholderPrefix = '#['
    placeholderSuffix = ']'
    target = '5.1'
    outOfOrder = false
    validateOnMigrate = true
    cleanOnValidationError = false
    initOnMigrate = false
    placeholders = [ 'aplaceholder': 'value' ]
  }
  databases {
    transactional {
      url = "jdbc:h2:${buildDir}/db/flyway/transaction" 
      sqlMigrationPrefix = 'Transaction-'
      sqlMigrationSuffix = '-OK.sql'
      placeholders = [ 'otherplaceholder': 'value123'
      schemas = [ 'schema4', 'schema5' ]
    }
    reporting {
      url = "jdbc:h2:${buildDir}/db/flyway/report" 
      sqlMigrationPrefix = 'Reporting-'
      sqlMigrationSuffix = '-OK.sql'
      placeholders = [ 'otherplaceholder': 'value456' ]
      schemas = [ 'schema6', 'schema7' ]
    }
  }
}

Versions

Version
0.7
0.6
0.5
0.4
0.3
0.2
0.1