Tutorial - Roll back or roll forward by generating scripts ad-hoc using snapshot history
Published 21 November 2025
- Configure the snapshot table history limit to be more than 1 by editing your toml project file. e.g.
[flyway.snapshot] historyLimit = 5
- Save snapshots on deployment using one of the following options
- 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:
- 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
- Expand the Advanced settings and click Add parameters
- Select migrate.saveSnapshot from the drop down. You can start typing to limit the results and find the parameter you need easier.
- Enter true for the parameter value.
- Click Add parameter.
- 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
- 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.
- If using state-based deployments, save snapshots on deploy
- Repeat step 2 until you have multiple entries in your snapshot history table, e.g.:
- You can now generate scripts to roll back or roll forward to a past state using one of the following options
- 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 tosnapshotHistory: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 - If using migrations-based deployments you have three options
- 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 tosnapshotHistory: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 - 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 tosnapshotHistory:previous, which refers to the second most recent entry in the snapshot history table.
- Generate a migration script to roll forward
- If using state-based deployments

