Liquibase dropFirst with postgis

I am trying to add a postgis extension to my postgressql database through Liquibase, here is how I did it:

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

It works great, the problem is what I'm developing. I want my database to be reset every time I restart my web server, so I installed linibase 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 dump everything, including postgis views, at startup. Which leads to this error.

org.postgresql.util.PSQLException: ERROR: Unable to cancel view of column_ geography because Indice is required for postgis extension: Instead, you can cancel postgis extension.

But how should I tell Liquibase to abandon extensions before resetting everything else? Is there a way to tell Liquibase how to delete a database?

Versions I use:

  • Spring IO 2.0.7
  • Liquibase 3.4.2 (version installed by spring IO)
  • Postgres 9.5
+5
source share
1 answer

Solved this by adding a bit of SQL that runs before starting Liquibase:

 @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

 DROP EXTENSION IF EXISTS postgis; 

This is not a fantasy, but it works.

+3
source

All Articles