Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre- and Post-migration scripts for Flyway

I am looking for a way to execute a hook script before and after migration. I have a bunch of views and stored procedures and would like the process to be:

  1. Drop all views and stored procedures.
  2. Run the migration.
  3. Rebuild views and stored procedures.

This insures that any change to the schema is reflected in related views and stored procedures. Steps (1) and (3) will be bash scripts.

Is this possible in Flyway?

like image 414
Eli Avatar asked Aug 28 '11 07:08

Eli


People also ask

What is Flyway migration script?

To achieve migrations, Flyway uses so-called migration scripts. Migrations scripts are "sequentially ordered SQL scripts" that incrementally migrate your schema. In order to recognize migration scripts, Flyway relies on naming conventions.

What are migration scripts?

Migration scripts are customizable SQL scripts created by user through ApexSQL Source Control containing any specific configuration changes, or overrides that need to be executed before or after applying an object change from source control, or they can be executed instead of a change.


2 Answers

Update 2014-04-29: This is now Possible with Flyway 3.0 by implementing the FlywayCallback interface.

Previous answer

The short answer is: no, not at this point.

Here is the reason: I thought about this as well as I was laying down the initial design for Flyway. The more I thought about this aspect though, the more it became clear to me that these pre and post scripts are also an integral part of the migration, or at least something a migration can not do without if it wants to be successful. Therefore I would recommend to either:

  • Merge 1, 2 & 3 in a single migration
  • Have 3 separate migrations x.1 (drop views), x.2 (actual migration), x.3 (rebuild views)

You might even be able to have x.1 and x.3 call stored procedures that do the work for you to avoid code duplication between migrations if these steps are repeating.

Having Flyway take care of performing all changes to the database structure makes the whole thing more straightforward, avoiding a mix of different technologies.

like image 121
Axel Fontaine Avatar answered Oct 23 '22 11:10

Axel Fontaine


To expand on Axel's response: callbacks with sql scripts simply means putting beforeMigrate.sql (for instance, this is one keyword among others) in the directory containing the migrations, and Flyway will execute beforeMigrate.sql before the other migration scripts. Even before schema_version gets locked.

Other callback names (e.g. afterMigrate) are listed in the documentation for callbacks.

like image 30
elmotec Avatar answered Oct 23 '22 12:10

elmotec