Redgate Flyway

Generate

Description

Generates a migration script based on the differences from an artifact produced by diff. The version, description and location of the migration script can be specified or auto-generated by flyway. Baseline, versioned and undo script types can be generated.

Note:

  • Specifying -generate.types=baseline,versioned or -generate.types=baseline,undo is disallowed and will result in an error.
  • generate.target must match one of the sources provided as either diff.source or diff.target.
  • A valid artifact must exist for the generate command to successfully generate migration scripts.

See Migrations for more information.

Usage examples

In all examples below the diff command must first be run to find changes to generate scripts from. The result of a diff operation is stored in the diff artifact. The diff command can be combined with generate into a single flyway call with verb chaining.

For example, the following command performs a diff and generate between the schema model directory and state represented by the current project migrations. A shadow database is used as a temporary build environment for the project migrations:

> flyway diff generate -diff.source=schemaModel -diff.target=migrations -diff.buildEnvironment=shadow

Generating a versioned migration script with auto-generated filename

If the migrations directory has the following contents before running the generate command:

B001__baseline.sql
V002.sql

Then running the generate command will create a new versioned V003.sql migration script:

> flyway generate -outputType=json

Flyway 11.8.0 by Redgate

{
  "output" : [ {
    "location" : "C:\\Users\\FlywayUser\\Project\\migrations\\V003.sql",
    "configPath" : null,
    "includedDependencies" : [ ],
    "messages" : [ ]
  } ]
}

Leaving the migrations directory with the following contents:

B001__baseline.sql
V002.sql
V003.sql

Generating versioned and undo migration scripts with a specified version and description

If the migrations directory has the following contents before running the generate command:

B001__baseline.sql
V002.sql

Then running the following generate command will generate versioned and undo scripts with the specified version and description:

> flyway generate -types=versioned,undo -version=003 -description=New-feature

Flyway 11.8.0 by Redgate

Database: jdbc:mysql://clone-internal.red-gate.com:32781/mysql (MySQL 8.0)
Database: jdbc:mysql://clone-internal.red-gate.com:32781/mysql (MySQL 8.0)
Generating undo migration: C:\Users\FlywayUser\Project\migrations\U003__New-feature.sql
Generating versioned migration: C:\Users\FlywayUser\Project\migrations\V003__New-feature.sql
Generated: C:\Users\FlywayUser\Project\migrations\V003__New-feature.sql
Generated: C:\Users\FlywayUser\Project\migrations\U003__New-feature.sql

Leaving the migrations directory with the following contents:

B001__baseline.sql
V002.sql
U003__New-feature.sql
V003__New-feature.sql

Generating a baseline migration script with a specified filename

In this case the migrations directory will be empty, as the baseline migration is the first script to be generated. Running the following generate command will generate a baseline script at the specified location:

> flyway generate -types=baseline -baselineFilename=C:\Users\FlywayUser\Project\migrations\B000__initial_state.sql

Flyway 11.8.0 by Redgate

Database: jdbc:mysql://clone-internal.red-gate.com:32781/mysql (MySQL 8.0)
Generating baseline migration: C:\Users\FlywayUser\Project\migrations\B000__initial_state.sql
Generated: C:\Users\FlywayUser\Project\migrations\B000__initial_state.sql

Leaving the migrations directory with the following contents:

B000__initial_state.sql

Generating an undo migration script for an existing versioned migration

If the migrations directory has the following contents before running the generate command:

B001__baseline.sql
V002_20240828161524__New_Feature.sql

Then running the following generate command will generate a corresponding undo script for the V002 migration, but only containing the changes for the comma separated change Ids specified in the changes.txt file:

> flyway generate -types=undo -changes='-' -outputType=json < changes.txt

Flyway 11.8.0 by Redgate

{
  "output" : [ {
    "location" : "C:\\Users\\FlywayUser\\Project\\migrations\\U002_20240828161524__New_Feature.sql",
    "configPath" : null,
    "includedDependencies" : [ ],
    "messages" : [ {
      "type" : "Warning",
      "warningCode" : null,
      "message" : "Line 9: [ColumnDrop] This deployment drops the column sakila.inventory.new_column. Any data in the column will be lost."
    } ]
  } ]
}

Leaving the migrations directory with the following contents:

B001__baseline.sql
U002_20240828161524__New_Feature.sql
V002_20240828161524__New_Feature.sql

Parameters

Optional

Parameter Namespace Description
artifactFilename generate The path to the diff artifact.
changes generate A comma separated list of change ids.
types generate A comma separated list of script types to generate.
location generate The location to generate the migration to.
target generate Sets the direction of script generation by stating the target to use. Must match one of diff.source or diff.target from flyway diff.
version generate The version part of the migration name.
timestamp generate Add a timestamp to the calculated version if one is not already present.
description generate The description part of the migration name.
baselineFilename generate The filename (or full path) to use for the generated baseline migration.
versionedFilename generate The filename (or full path) to use for the generated versioned migration.
undoFilename generate The filename (or full path) to use for the generated undo migration.
force generate If the migration script already exists, overwrite it.
workingDirectory (root) The directory to consider the current working directory. All relative paths will be considered relative to this.

Universal commandline parameters are listed here.

JSON output format

{
  "scripts": [
    {
      "type": "versioned",
      "location": "C:\\flywayProejct\\migrations\\V001__AddTable.sql",
      "scriptConfigPath": "C:\\flywayProejct\\migrations\\V001__AddTable.sql.conf",
        "includedDependencies": [
        "dbo.someDependency"
      ],
      "warnings": [
        {
          "type": "WARNING_TYPE",
          "message": "Warning message"
        }   
    }
  ],
}

Error codes

This command can produce the following error codes:


Didn't find what you were looking for?