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 :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With