Skip to end of metadata
Go to start of metadata

Problem

Some of our project classes come from Common Model component. To implement our schema migration, we need a mechanism to apply Common Model migration first and our changes second.

Solution

From this article you may learn about Flyway, how to make schema update.

First step

First step is registering Flyway beans into Spring Application Context. There is FlywayWrapper bean in common. This bean purpose is control (enabling/disabling) flyway migrations, schema cleanup and initialization. Due to current FlywayWrapper implementation we need used two the same bean registration records with different init methods for each schema.

Our beans require data source instance and we have the next init chain:

  1. dataSource bean created.

Applying Common schema migrations

  1. flyway_common_init bean created and make Flyway.init() if there is no common metadata table in the specified schema and if smartInit is enabled.
  2. flyway_common created and call Flyway.migrate() if enabled property is True.
    Applying Poulpe schema migrations
  3. flyway_poulpe_init bean created and make Flyway.init() if there is no poulpe metadata table in the specified schema and if smartInit is enabled.
  4. flyway_poulpe_init created and call Flyway.migrate() if enabled property is True.

So this chain may seem a little ugly. For our current developer needs we may use one FlywayWrapper method with two sequence calls for init() and migrate() Flyway methods.

Second step

We use Hibernate for persistence and we need avoid Hibernate schema object creation. Now Flyway will responsible for schema and data managing.

In datasource.properties file need to set next value

Third step

Now we need to write the first SQL migration and place it to resource directory. This migration contains all poulpe schema specific objects.

Flyway Logging

Sometimes there is a need to see exact migration actions. Through Flyway it is not possible, solution - usage of Apache Logging – Log4j.
Customize your log4j.xml to be able to see logs from Flyway Migrations in the following way:

Notes

  1. Don’t forget use in SQL script only one case – upper or low (UNIX environment is case sensitive).
  2. Don’t forget add new resource to maven build goal.

Now on our project we have 2 phase Flyway migrations. Please make all future schema changes according next instruction.