English 中文(简体)
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?

问题回答

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.

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.

you should use flyway Callbacks like:

https://flywaydb.org/documentation/concepts/callbacks#beforeEachMigrate https://flywaydb.org/documentation/concepts/callbacks#beforeEachMigrate

see also : https://flywaydb.org/documentation/tutorials/callbacks.html

SQL Callbacks The most convenient way to hook into Flyway’s lifecycle is through SQL callbacks. These are simply sql files in the configured locations following a certain naming convention: the event name followed by the SQL migration suffix.

Using the default settings, Flyway looks in its default locations (<install_dir>/sql) for the Command-line tool) for SQL files like beforeMigrate.sql, beforeEachMigrate.sql, afterEachMigrate.sql, …

Placeholder replacement works just like it does for SQL migrations.

Optionally the callback may also include a description. In that case the callback name is composed of the event name, the separator, the description and the suffix. Example: beforeRepair__vacuum.sql.

Note: Flyway will also honor any sqlMigrationSuffixes you have configured, when scanning for SQL callbacks.

Java Callbacks If SQL Callbacks aren’t flexible enough for you, you have the option to implement the Callback interface yourself. You can even hook multiple Callback implementations in the lifecycle. Java callbacks have the additional flexibility that a single Callback implementation can handle multiple lifecycle events, and are therefore not bound by the SQL callback





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签