teopost
teopost

Reputation: 121

Flyway conditional db migration

I'm trying to use flyway, but I have a scenario that does not know how to resolve:

When I apply the STANDARD scripts, I have to run V1.0__create_table_TAB1.sql When I apply the scripts to customer1, the TAB1 table is a view, and so I have to run V1.0__create_view_TAB1_to_schema1.sql.

Practically:

└── sql
     ├── sql_common
     │   ├── V0.0 __.... sql
     │   └── V1.0__create_table_TAB1.sql
     ├── sql_customer1
     │   └── V1.0__create_view_TAB1_to_schema1.sql
     └── sql_customer2
         └── V1.0__create_view_TAB1_to_schema2.sql

The scripts of sql_common folder should always be applied, but the V1.0 (create table TAB1), if I apply to the customer1 script should not be applied. In its place is to be applied V1.0__create_view_TAB1_to_schema1.sql

How can I handle prevents this case?

Upvotes: 3

Views: 2778

Answers (1)

Axel Fontaine
Axel Fontaine

Reputation: 35179

Use the following structure:

└── sql
     ├── sql_common
     │   └── V0.0 __.... sql
     ├── sql_regular
     │   └── V1.0__create_table_TAB1.sql
     ├── sql_customer1
     │   └── V1.0__create_view_TAB1_to_schema1.sql
     └── sql_customer2
         └── V1.0__create_view_TAB1_to_schema2.sql

in combination with Flyway.setLocations().

  • Regular: flyway.setLocations("filesystem:sql/sql_common", "filesystem:sql/sql_regular");
  • customer1: flyway.setLocations("filesystem:sql/sql_common", "filesystem:sql/sql_customer1");
  • customer2: flyway.setLocations("filesystem:sql/sql_common", "filesystem:sql/sql_customer2");

This way there will be exactly 1 migration with v1.0 per case.

Upvotes: 5

Related Questions