Redgate Flyway

Tutorial - Resolve drift by incorporating it (migrations-based deployments)

Pre-requisites

  • Ensure that your current working directory is set correctly. The working directory can be specified explicitly on every flyway command and will otherwise be the command-line current working directory. All commands in this tutorial assume that the command-line current working directory will be correct.
  • Check for drift - if drift is detected, scripts to help with drift resolution will be generated in the drift-resolution folder under your working directory.

Commands to run

  1. Ensure that your development environment is in sync with your schema model. You can verify this by running the diff command (as we are checking for equality it doesn't matter which is the source and which is the target).
    flyway diff -source=development -target=schemaModel
    If the environment is in sync with the schema model, no differences will be detected.
  2. Copy the incorporate migration into your migrations folder and adjust the version number if necessary. The incorporate migration will have been generated with a version number based on the biggest numbered migration in your project at the time of running the check -drift command, incremented by 1. e.g. if you had a migration named V1.2__deployMyChanges.sql in your migrations folder and that was the migration with the highest version number, then the incorporate script would be named V1.3__incorporateDrift.sql.
    You may also want to edit the script to make it idempotent, so as to avoid step 7 below. Note that for SQL Server databases the incorporate migration is generated with object existence checks, so it should be idempotent out of the box.
  3. Execute the incorporate migration against your development environment.
    If you are using the migrate command with your development environment, run
    flyway migrate -environment=development
    Otherwise run the deploy command (this is likely the necessary approach if using Flyway Desktop)
    flyway deploy -scriptFilename="migrations/V1.3__incorporateDrift.sql" -environment=development
  4. Update your schema model (see this tutorial for more information on this step)
    flyway diff -source=development -target=schemaModel
    flyway model
  5. Commit and push to version control
  6. (Optional) Update snapshot in target environment (not applicable if using a build environment for drift checks) - if you don't do this, the drift check will continue to fail (see this tutorial for more information on embedded snapshots, and this tutorial for snapshots on the filesystem) 
    flyway snapshot -source=production -filename="snapshotHistory:drift_correction"
  7. (If needed) Mark the new migration as applied in your target environment. You can avoid this step by making sure the incorporate migration is idempotent (see step 2).
    flyway migrate -environment=production -cherryPick=1.3 -skipExecutingMigrations=true
  8. Rerun your deployment (see common migrations based deployment scripts) - this could be part of an automated pipeline and does not need to be done manually
    flyway check -drift -environment=production -failOnDrift=true
    flyway check -changes -buildEnvironment="build" -environment="production"
    flyway migrate -environment="production" -saveSnapshot=true
    If you have done step 5, the drift check should now pass.

Related command reference

Pre-requisites

  • Check for drift - if drift is detected, scripts to help with drift resolution will be generated in the drift-resolution folder under your project directory.

Run command

  1. Ensure that your development environment is in sync with your schema model, by navigating to the schema model page and ensuring there are no differences.
  2. Open your project folder on the filesystem. Copy the incorporate migration into your migrations folder and adjust the version number if necessary. The incorporate migration will have been generated with a version number based on the biggest numbered migration in your project at the time of running the check -drift command, incremented by 1. e.g. if you had a migration named V1.2__deployMyChanges.sql in your migrations folder and that was the migration with the highest version number, then the incorporate script would be named V1.3__incorporateDrift.sql.
    You may also want to edit the script to make it idempotent, so as to avoid step 6 below. Note that for SQL Server databases the incorporate migration is generated with object existence checks, so it should be idempotent out of the box.
  3. Execute the incorporate script against your development database
    It isn't currently possible to run the deploy command with custom arguments from Flyway Desktop. Navigate to the command line, and follow the instructions for the command-line.
    flyway deploy -scriptFilename="migrations/V1.3__incorporateDrift.sql" -environment=development
  4. Navigate to the schema model page, refresh the left hand pane, and click Save, to update the schema model.
  5. Commit and push to version control
  6. (If needed) Mark the new migration as applied in your target environment. You can avoid this step by making sure the incorporate migration is idempotent (see step 2).
    1. Navigate to the Migration scripts page
    2. Connect to your target environment, by selecting the relevant connection from the Target database dropdown, or adding a new target configuration.
    3. Expand the Advanced settings and click Add parameters
    4. Select cherryPick from the drop down.  You can start typing to limit the results and find the parameter you need easier.
    5. Enter the version number of the incorporate migration for the parameter value. (1.3 in the example above)
    6. Click Add parameter.
    7. Click Back.
    8. Repeat the previous 5 steps, but this time select skipExecutingMigrations as the parameter name and true as the parameter value 
    9. Click Run migrate
  7. If using automated deployments, see the command-line tutorial steps.
    If using manual deployment:
    1. Navigate to the migrations page.
    2. Connect to your target environment, by selecting the relevant connection from the Target database dropdown, or adding a new target configuration.
    3. Click Run migrate


Further information


Didn't find what you were looking for?