Redgate Flyway

Tutorial - Roll back or roll forward by generating scripts ad-hoc using snapshot history

FLYWAY ENTERPRISE

Snapshots ONLY handle schema objects.  Any static data or transactional data changes must be considered.  Carefully review generated scripts and update them as needed before using them.  

  1. Configure the snapshot table history limit to be more than 1 by editing your toml project file. e.g.
    [flyway.snapshot]
    historyLimit = 5
  2. Save snapshots on deployment using one of the following options
    1. If using state-based deployments, save snapshots on deploy


      flyway deploy -environment=production -saveSnapshot=true

      You can also optionally give your snapshot a name on deployment using the snapshot filename parameter, e.g.

      flyway deploy -environment=production -deploy.saveSnapshot=true -snapshot.filename="snapshotHistory:1.0.0_initial_release"

      Use this tutorial to deploy changes manually, but when reviewing the deployment script and before deploying (step 6 in the tutorial), make sure to check the Save snapshot checkbox:

    2. If using migrations-based deployment, save snapshots on migrate


      flyway migrate -baselineOnMigrate=true -environment=production -saveSnapshot=true

      You can also optionally give your snapshot a name on deployment using the snapshot filename parameter, e.g.
      flyway migrate -baselineOnMigrate=true -environment=production -migrate.saveSnapshot=true -snapshot.filename="snapshotHistory:1.0.0_initial_release"

      Note that for migrations-based deployments it is also possible to use the prepare and deploy commands, as outlined for state-based deployments above.

      Use this tutorial to run migrate manually, but after selecting a target database (step 2 in the tutorial), set the migrate.saveSnapshot advanced parameter using the following steps

      1. Expand the Advanced settings and click Add parameters
      2. Select migrate.saveSnapshot from the drop down.  You can start typing to limit the results and find the parameter you need easier.
      3. Enter true for the parameter value.
      4. Click Add parameter.
      5. Click Back

      You can also optionally give your snapshot a name on deployment using the snapshot filename parameter, by repeating the above steps, but specifying the snapshot.filename parameter and giving it a value of e.g. snapshotHistory:1.0.0_initial_release

    3. Or, take a snapshot ad-hoc, using the snapshot command


      flyway snapshot -source=production -filename="snapshotHistory:ad-hoc deployment"

      In the examples above, "production" is the ID of a database environment that
       is defined in the project and provides the connection information for the database that you want to capture in a snapshot.

      If this is the first time you're saving a snapshot in the target database, then the snapshot history table will be created.

  3. Repeat step 2 until you have multiple entries in your snapshot history table, e.g.:


  4. You can now generate scripts to roll back or roll forward to a past state using one of the following options
    1. If using state-based deployments

      Generate a deployment script, "D__rollback.sql" in the example below, by performing a comparison between an older snapshot and the current database state

      flyway diff -source=snapshotHistory:previous -target=production
      flyway prepare -scriptFilename="D__rollback.sql"

      You can execute this script using the deploy command. See this tutorial.
      Under the hood the diff command generates an artifact to a temporary directory which is used by the prepare command.
      The diff source is set to snapshotHistory:previous , which refers to the second most recent entry in the snapshot history table. If your entries have unique names, you can also instead them by name, e.g. snapshotHistory:1.0.0_initial_release 

    2. If using migrations-based deployments you have three options

      1. Generate a migration script to roll forward

        Generate a new migration, by performing a comparison between an older snapshot and the current database state. This will automatically be generated into your first migration location on disk, with an automatically calculated version number 

        flyway diff -source=snapshotHistory:previous -target=production
        flyway generate -description=rollback

        You can execute this script using the migrate command (or by running the prepare and deploy commands). See this tutorial.
        Under the hood the diff command generates an artifact to a temporary directory which is used by the generate command.
        The diff source is set to snapshotHistory:previous , which refers to the second most recent entry in the snapshot history table. If your entries have unique names, you can also instead them by name, e.g. snapshotHistory:1.0.0_initial_release 
      2. Generate an undo script to roll back

        Generate an undo script for the most recently created migration. This will automatically be generated into your first migration location on disk.

        flyway diff -source=snapshotHistory:previous -target=production
        flyway generate -types=undo

        You can execute this script using the undo command.
        Under the hood the diff command generates an artifact to a temporary directory which is used by the generate command.
        The diff source is set to snapshotHistory:previous , which refers to the second most recent entry in the snapshot history table.

Didn't find what you were looking for?