Is there any way to run Flyway task excluding some

2019-07-30 00:09发布

问题:

I'm currently using Flyway to manage migrations on an application which uses Postgis (PostgreSQL geospatial extension).

This plugin uses a table called spatial_ref_sys which is located in the same schema the application uses too, when I call mvn flyway:clean I'm getting an error indicating that Flyway was unable to delete this table (it was created using user postgres); if I change the owner to my application database user, then the error changes to:

ERROR: cannot drop table spatial_ref_sys because extension postgis requires it

[ERROR] Hint: You can drop extension postgis instead.

However, I don't want to drop these items, which are external to my application logic, as they are just "auxiliars".

I have seen two questions where Axel Fontaine said the feature of ignore some table(s) is not supported (both questions are two or more years old), I even have cloned the GitHub repo to add this feature to Flyway by myself, and I've been reading some parts of the code where this change could be implemented; but I'm suspecting it will affect several parts of the code, and my unknowledgement about it could make the things harder..

So, I'm looking some help to implement the change, or maybe some ideas to do a work-around this issue..

I'm thinking in simply do DROP of the entire database and recreate it, then recreate the geospatial extensions (Postgis, PGRouting, etc), and make the migration using Flyway, but this will be not much suitable if I have to do it several times during the development process..

回答1:

I had this problem for test environment and i wanted to delete schema by flyway. I fixed it by manipulating flyway spring bean sequence. First, I dropped postgis extension before flyway.clean() and then at the first line of V1__init.sql add CREATE EXTENSION postgis SCHEMA public;:

@Bean
@Profile("test")
public Flyway flyway(DataSource dataSource) {
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setLocations("classpath:db/migration");

    runSql("drop extension IF EXISTS postgis CASCADE;", dataSource);

    flyway.clean();
    flyway.migrate();

    return flyway;
}