Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquibase dropFirst with postgis

I'm trying to add postgis extension to my postgressql database through Liquibase, here's how I've done it :

<sql>CREATE EXTENSION IF NOT EXISTS postgis;</sql>

It works fine, problem is when I'm developing I want my database to be reset every time I restart my web server, so I've set liquibase in spring like this

@Bean(name = "liquibase")
@DependsOn("dataSource")
@Profile("dev")
public SpringLiquibase liquibaseDev() {
    SpringLiquibase springLiquibase = new SpringLiquibase();
    springLiquibase.setDataSource(this.dataSource());
    springLiquibase.setChangeLog("classpath:liquibase.xml");
    springLiquibase.setDefaultSchema(this.environment.getProperty("jdbc.defaultSchema"));
    springLiquibase.setDropFirst(true);
    return springLiquibase;
}

And so Liquibase tries to drop everything, including postgis views, at startup. Which lead to this err

org.postgresql.util.PSQLException: ERROR: cannot drop view geography_columns because extension postgis requires it Indice : You can drop extension postgis instead.

But how I'm supposed to tell liquibase to drop extensions before dropping everything else? Is there a way to tell Liquibase how to drop a database?

Versions I'm using :

  • Spring IO 2.0.7
  • Liquibase 3.4.2 (version set by Spring IO)
  • Postgres 9.5
like image 987
lepak Avatar asked Aug 18 '16 10:08

lepak


1 Answers

Solved this by adding a little bit of SQL that is executed before Liquibase starts :

@Bean(name = "liquibase")
@DependsOn("dataSource")
@Profile("dev")
public SpringLiquibase liquibaseDev() {

    ScriptUtils.executeSqlScript(this.dataSource().getConnection(), new ClassPathResource("delete-postgis.sql"));

    SpringLiquibase springLiquibase = new SpringLiquibase();
    springLiquibase.setDataSource(this.dataSource());
    springLiquibase.setChangeLog("classpath:liquibase.xml");
    springLiquibase.setDefaultSchema(this.environment.getProperty("jdbc.defaultSchema"));
    springLiquibase.setDropFirst(true);
    return springLiquibase;
}

with file src/main/resources/delete-postgis.sql being

DROP EXTENSION IF EXISTS postgis;

That's not fancy, but it works.

like image 194
lepak Avatar answered Oct 11 '22 03:10

lepak