Redgate Flyway

For PostgreSQL users - where are your DBs hosted?

Tutorial - Generate undo scripts

The first time you generate a migration script in the project, you will be asked if you also want to generate undo scripts.  You can change your mind later in the settings cog in the top right:

When you generate a migration using the generate command, you can configure the types of migration to generate using the types parameter.

Versioned and undo scripts can be generated using a diff and generate command combination, where the differences between the schema model and the shadow environment are converted into a migration script that can be executed.

$ flyway diff generate "-diff.source=schemaModel" "-diff.target=migrations" "-diff.buildEnvironment=shadow" "-generate.types=versioned,undo" "-generate.description=NewTableAdded"

Flyway Enterprise Edition 11.2.0 by Redgate
Licensed to red-gate.com (license ID 1174ed6b-b10e-41bd-9a1b-285ddc3239c7)

See release notes here: https://rd.gt/416ObMi
Successfully applied 1 migration to schema [dbo], now at version v001.20241209164727 (execution time 00:02.820s)
diff artifact generated: C:\Users\Flyway\AppData\Local\Temp\flyway.artifact.diff
+-----------------------------+--------+-------------+--------+----------+
| Id                          | Change | Object Type | Schema | Name     |
+-----------------------------+--------+-------------+--------+----------+
| J7JsJA_jmgaSrCD.hAdkbUqxxQs | Add    | Table       | dbo    | NewTable |
+-----------------------------+--------+-------------+--------+----------+

Using diff artifact: C:\Users\Flyway\AppData\Local\Temp\flyway.artifact.diff
Generating versioned migration: C:\Users\Flyway\FlywayProjects\SqlServerProject\migrations\V002_20241210110201__NewTableAdded.sql
Generating undo migration: C:\Users\Flyway\FlywayProjects\SqlServerProject\migrations\U002_20241210110201__NewTableAdded.sql
Generated: C:\Users\Flyway\FlywayProjects\SqlServerProject\migrations\U002_20241210110201__NewTableAdded.sql
Generated: C:\Users\Flyway\FlywayProjects\SqlServerProject\migrations\V002_20241210110201__NewTableAdded.sql

We can validate the newly generated migration script by running the diff command and checking there are no differences between the schema model and the shadow database after applying the migration (remember that the diff command will provision the shadow environment with the latest migration scripts when -diff.target=migrations):

$ flyway diff "-diff.source=schemaModel" "-diff.target=migrations" "-diff.buildEnvironment=shadow"

Flyway Enterprise Edition 11.2.0 by Redgate
Licensed to red-gate.com (license ID 1174ed6b-b10e-41bd-9a1b-285ddc3239c7)

See release notes here: https://rd.gt/416ObMi
Successfully validated 3 migrations (execution time 00:00.016s)
Current version of schema [dbo]: 001.20241209164727
Migrating schema [dbo] to version "002.20241210110201 - NewTableAdded"
Successfully applied 1 migration to schema [dbo], now at version v002.20241210110201 (execution time 00:00.028s)
diff artifact generated: C:\Users\Flyway\AppData\Local\Temp\flyway.artifact.diff
+-----------------------------+--------+-------------+--------+------+
| Id                          | Change | Object Type | Schema | Name |
+-----------------------------+--------+-------------+--------+------+
| No differences found                                               |
+-----------------------------+--------+-------------+--------+------+


Next steps

Optionally save your new project to Version Control:



Didn't find what you were looking for?