grunt-flyway

Run Flyway database migration tool.

grunt-flyway

Run Flyway database migration tool with Grunt.

This Grunt plugin and has not been tested thoroughly yet so use it at your own risk!

The plugin supports all Flyway configuration options for clean, init, migrate and validate commands.

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-flyway --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-flyway');

The plugin uses Flyway "The Agile Database Migration Framework" which is developed in Java.

So, you have to install Java and have the java executable available in your PATH.

The plugin uses Flyway 2.3.

In your project's Gruntfile, add a section named flyway to the data object passed into grunt.initConfig().

grunt.initConfig({
  flyway: {
    options: {
      driver: 'com.mysql.jdbc.Driver',
      url: 'jdbc:mysql://localhost/flyway',
      user: 'flyway',
      password: 'flyway'
    },
    clean: {
      command: 'clean'
    },
    init: {
      options: {
        initDescription: 'Sample database created using Flyway:-)',
        initVersion: '1.0'
      },
      command: 'init'
    },
    migrate: {
      options: {
        locations: 'filesystem:src/main/resources/sql/migration'
      },
      command: 'migrate'
    }
  }
})

The name of the Grunt targets to use inside the flyway task configuration are not pre-defined, you can choose what you want to name those targets.

Any number of targets can be defined and you can have multiple targets with different configurations, for example:

grunt.initConfig({
  flyway: {
    options: {
      driver: 'com.mysql.jdbc.Driver',
      url: 'jdbc:mysql://localhost/db1',
      user: 'flyway',
      password: 'flyway'
    },
    clean_db1: {
      command: 'clean'
    },
    clean_db2: {
      options {
        url: 'jdbc:mysql://localhost/db2'
      },
      command: 'clean',
    },
    init_db1: {
      options: {
        initDescription: 'Sample database created using Flyway:-)',
        initVersion: '1.0'
      },
      command: 'init'
    },
    init_db2: {
      options {
        url: 'jdbc:mysql://localhost/db2',
        initDescription: 'Sample database created using Flyway:-)',
        initVersion: '1.0'
      },
      command: 'init'
    },
    migrate_db1: {
      options: {
        locations: 'filesystem:src/main/resources/sql/migration'
      },
      command: 'migrate'
    },
    migrate_db2: {
      options {
        url: 'jdbc:mysql://localhost/db2',
        locations: 'filesystem:src/main/resources/sql/migration',
        placeholders: {
          name: 'Tom'
        }
      },
      command: 'migrate'
    }
  }
})

The only commands which are supported for the moment are clean, init, migrate and validate.

Options' descriptions come from Flyway's documentation.

optionrequireddefaultdescription
urlyesThe jdbc url to use to connect to the database
drivernoAuto-detected based on urlThe fully qualified classname of the jdbc driver to use to connect to the database
usernoThe user to use to connect to the database
passwordnoThe password to use to connect to the database
schemasnodefault schema of the connectionComma-separated case-sensitive list of schemas managed by Flyway.

The schemas will be cleaned in the order of this list.
jarDirno<install-dir>/jarsThe directory containing the JDBC driver
optionrequireddefaultdescription
urlyesThe jdbc url to use to connect to the database
drivernoAuto-detected based on urlThe fully qualified classname of the jdbc driver to use to connect to the database
usernoThe user to use to connect to the database
passwordnoThe password to use to connect to the database
schemasnodefault schema of the connectionComma-separated case-sensitive list of schemas managed by Flyway.

The first schema will be the one containing the metadata table.
tablenoschema_versionThe name of Flyway's metadata table.

By default (single-schema mode) the metadata table is placed in the default schema for the connection provided by the datasource.

When the schemas property is set (multi-schema mode), the metadata table is placed in the first schema of the list.
jarDirno<install-dir>/jarsThe directory containing the JDBC driver
initVersionno1The initial version to put in the database
initDescriptionno<< Flyway Init >>The description of the initial version
optionrequireddefaultdescription
urlyesThe jdbc url to use to connect to the database
drivernoAuto-detected based on urlThe fully qualified classname of the jdbc driver to use to connect to the database
usernoThe user to use to connect to the database
passwordnoThe password to use to connect to the database
schemasnodefault schema of the connectionComma-separated case-sensitive list of schemas managed by Flyway.

The first schema in the list will be automatically set as the default one during the migration. It will also be the one containing the metadata table.
tablenoschema_versionThe name of Flyway's metadata table. By default (single-schema mode) the metadata table is placed in the default schema for the connection provided by the datasource. When the schemas property is set (multi-schema mode), the metadata table is placed in the first schema of the list.
locationsnofilesystem:<install-dir>/sqlComma-separated list of locations to scan recursively for migrations. The location type is determined by its prefix.

Unprefixed locations or locations starting with classpath: point to a package on the classpath and may contain both sql and java-based migrations.

Locations starting with filesystem: point to a directory on the filesystem and may only contain sql migrations.
jarDirno<install-dir>/jarsThe directory containing the JDBC driver
sqlMigrationPrefixnoVThe file name prefix for sql migrations
sqlMigrationSuffixno.sqlThe file name suffix for sql migrations
encodingnoUTF-8The encoding of sql migrations
placeholders.namenoPlaceholders to replace in sql migrations
placeholderPrefixno${The prefix of every placeholder
placeholderSuffixno}The suffix of every placeholder
targetnolatest versionThe target version up to which Flyway should run migrations. Migrations with a higher version number will not be applied.
outOfOrdernofalseAllows migrations to be run "out of order".

If you already have versions 1 and 3 applied, and now a version 2 is found, it will be applied too instead of being ignored.
validateOnMigratenofalseWhether to automatically call validate or not when running migrate.

For each sql migration a CRC32 checksum is calculated when the sql script is executed. The validate mechanism checks if the sql migration in the classpath still has the same checksum as the sql migration already executed in the database.
cleanOnValidationErrornofalseWhether to automatically call clean or not when a validation error occurs.

This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM.

Warning! Do not enable in production!
initOnMigratenofalseWhether to automatically call init when migrate is executed against a non-empty schema with no metadata table. This schema will then be initialized with the initVersion before executing the migrations. Only migrations above initVersion will then be applied.

This is useful for initial Flyway production deployments on projects with an existing DB.

Be careful when enabling this as it removes the safety net that ensures Flyway does not migrate the wrong database in case of a configuration mistake!
initVersionno1The initial version to put in the database
initDescriptionno<< Flyway Init >>The description of the initial version
optionrequireddefaultdescription
urlyesThe jdbc url to use to connect to the database
drivernoAuto-detected based on urlThe fully qualified classname of the jdbc driver to use to connect to the database
usernoThe user to use to connect to the database
passwordnoThe password to use to connect to the database
schemasnodefault schema of the connectionComma-separated case-sensitive list of schemas managed by Flyway.

The first schema will be the one containing the metadata table.
tablenoschema_versionThe name of Flyway's metadata table. By default (single-schema mode) the metadata table is placed in the default schema for the connection provided by the datasource. When the schemas property is set (multi-schema mode), the metadata table is placed in the first schema of the list.
locationsnofilesystem:<install-dir>/sqlComma-separated list of locations to scan recursively for migrations. The location type is determined by its prefix.

Unprefixed locations or locations starting with classpath: point to a package on the classpath and may contain both sql and java-based migrations.

Locations starting with filesystem: point to a directory on the filesystem and may only contain sql migrations.
jarDirno<install-dir>/jarsThe directory containing the JDBC driver
sqlMigrationPrefixnoVThe file name prefix for sql migrations
sqlMigrationSuffixno.sqlThe file name suffix for sql migrations
encodingnoUTF-8The encoding of sql migrations
placeholders.namenoPlaceholders to replace in sql migrations
placeholderPrefixno${The prefix of every placeholder
placeholderSuffixno}The suffix of every placeholder
targetnolatest versionThe target version up to which Flyway should run migrations. Migrations with a higher version number will not be applied.
outOfOrdernofalseAllows migrations to be run "out of order".

If you already have versions 1 and 3 applied, and now a version 2 is found, it will be applied too instead of being ignored.
cleanOnValidationErrornofalseWhether to automatically call clean or not when a validation error occurs.

This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM.

Warning! Do not enable in production!

Simply call the Flyway targets you've defined inside your Gruntfile:

grunt flyway:clean

grunt flyway:init

grunt flyway:migrate

grunt flyway:validate

  • Add develop branch has been created, pull request have to be performed on this branch now
  • Upgrade to Flyway 2.3
  • Added support for validate command
  • Added support for all options for each supported command
  • Added configuration validation
    • Required options cause an error when not present
    • Any extra option that is not available for selected command also causes an error
  • Fix JSHint Lint errors
  • Fix a Flyway classpath build problem under UNIX. The ';' character was used instead ':'.