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.
From this article you may learn about Flyway, how to make schema update.
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:
- dataSource bean created.
Applying Common schema migrations
- 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.
- flyway_common created and call Flyway.migrate() if enabled property is True.
Applying Poulpe schema migrations
- 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.
- 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.
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
Now we need to write the first SQL migration and place it to resource directory. This migration contains all poulpe schema specific objects.
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:
- Don’t forget use in SQL script only one case – upper or low (UNIX environment is case sensitive).
- 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.