I'm working on an application with liquibase, spring-boot and hibernate. The database used is PostgreSQL. In order to populate the DB at startup I configured a data.sql file in src/main/resources, containing some insert statements. In addition, after the boot there is also liquibase trying to apply all the changesets, one of them being the creation of a table populated using the data.sql file. So I get the following non blocking error, when executing the mvn cmd to start the app:

2018-04-25 14:33:53.417 ERROR 11060 --- [neut-Executor-1] liquibase : classpath:config/liquibase/master.xml: config/liquibase/changelog/20180424154826_added_entity_Container.xml::20180424154826-1::jhipster: Change Set config/liquibase/changelog/20180424154826_added_entity_Container.xml::20180424154826-1::jhipster failed. Error: ERROR: relation "container" already exists [Failed SQL: CREATE TABLE public.container (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(2000), container_type VARCHAR(255), created TIMESTAMP WITHOUT TIME ZONE, CONSTRAINT PK_CONTAINER PRIMARY KEY (id))] 2018-04-25 14:33:53.453 ERROR 11060 --- [neut-Executor-1] i.g.j.c.liquibase.AsyncSpringLiquibase : Liquibase could not start correctly, your database is NOT ready: Migration failed for change set config/liquibase/changelog/20180424154826_added_entity_Container.xml::20180424154826-1::jhipster: Reason: liquibase.exception.DatabaseException: ERROR: relation "container" already exists [Failed SQL: CREATE TABLE public.container (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(2000), container_type VARCHAR(255), created TIMESTAMP WITHOUT TIME ZONE, CONSTRAINT PK_CONTAINER PRIMARY KEY (id))] liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/20180424154826_added_entity_Container.xml::20180424154826-1::jhipster: Reason: liquibase.exception.DatabaseException: ERROR: relation "container" already exists [Failed SQL: CREATE TABLE public.container (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(2000), container_type VARCHAR(255), created TIMESTAMP WITHOUT TIME ZONE, CONSTRAINT PK_CONTAINER PRIMARY KEY (id))] at liquibase.changelog.ChangeSet.execute(ChangeSet.java:619) at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51) at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:79) at liquibase.Liquibase.update(Liquibase.java:214) at liquibase.Liquibase.update(Liquibase.java:192) at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431) at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388) at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:94) at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:77) at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:68) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: liquibase.exception.DatabaseException: ERROR: relation "container" already exists [Failed SQL: CREATE TABLE public.container (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(2000), container_type VARCHAR(255), created TIMESTAMP WITHOUT TIME ZONE, CONSTRAINT PK_CONTAINER PRIMARY KEY (id))] at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:309) at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55) at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:113) at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1277) at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1259) at liquibase.changelog.ChangeSet.execute(ChangeSet.java:582) ... 12 common frames omitted Caused by: org.postgresql.util.PSQLException: ERROR: relation "container" already exists at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288) at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430) at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356) at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:303) at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:289) at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:266) at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:262) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114) at com.sun.proxy.$Proxy129.execute(Unknown Source) at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:307) ... 17 common frames omitted 

What I'm thinking is that the hibernate executing the data.sql gets executed just a moment before the liquibase migration process, so maybe there should be a way to avoid this exception. Is it possible to force the execution of hibernate importing data.sql to happen after liquibase migration?

3

1 Answer

you have to decide by which mechanism do you want to execute the changes. So you can use hibernate to create your tables and insert data or you can use liquibase to create you tables and data not both at the same time. What I was doing on previous projects was setting the hibenate to only validate mode spring.jpa.hibernate.ddl-auto=validateand using liquibase to create tables and insert data.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.