Reputation: 934
I use Quarkus Devservices to spinup postgres db for test purposes.
As also mentioned at https://quarkus.io/blog/hibernate-orm-config-profiles/ I set generation to drop-and-create
and created import.sql file which must be used to create db tables from scratch every time the app is started, however, nothing is created, instead I get odd error messages which don't fit to the status of the postgres db.
2022-03-12 13:43:29,851 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 00000
2022-03-12 13:43:29,852 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) relation "filepart" does not exist, skipping
2022-03-12 13:43:29,853 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 00000
2022-03-12 13:43:29,854 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) table "download" does not exist, skipping
2022-03-12 13:43:29,855 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 00000
2022-03-12 13:43:29,855 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) table "filepart" does not exist, skipping
2022-03-12 13:43:29,867 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 42P07
2022-03-12 13:43:29,867 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) relation "download" already exists, skipping
2022-03-12 13:43:29,868 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 42P07
2022-03-12 13:43:29,868 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) relation "filepart" already exists, skipping
2022-03-12 13:43:51,035 INFO [io.quarkus] (Quarkus Main Thread) quarkus-resteasy-postgres 1.0.0-SNAPSHOT on JVM (powered by Quarkus 2.7.4.Final) started in 27.125s. Listening on: http://localhost:8080
appliction.properties:
quarkus.datasource.devservices.enabled=true
quarkus.datasource.db-kind=postgresql
quarkus.datasource.devservices.port=5432
quarkus.datasource.username=postgres
quarkus.datasource.password=postgres
quarkus.hibernate-orm.database.generation=drop-and-create
# it is default, only explicit to be clear:
quarkus.hibernate-orm.sql-load-script=import.sql
Complete project is here: https://github.com/syr/quarkus-resteasy-postgres/tree/hibernate_drop_not_working
UPDATE: when disabling db generation by
quarkus.hibernate-orm.database.generation=none
SQL, which is obviously generated from the entities is still executed, whereas import.sql is ignored.
create table Download (idProperty1 varchar(255) not null, idProperty2 varchar(255) not null, finished boolean, id int8, primary key (idProperty1, idProperty2));
create table FilePart (id int8 not null, filePartFilePath varchar(255), idProperty1 varchar(255), idProperty2 varchar(255), primary key (id));
alter table if exists FilePart add constraint FKia6okjgd7yxo21sfga0hej3ni foreign key (idProperty1, idProperty2) references Download;
Upvotes: 2
Views: 6287
Reputation: 1
The import.sql is invoked when there is a schema change like a new table created. Hence, it only runs when you change the database generation to drop-and-create.
quarkus.hibernate-orm.scripts.generation=drop-and-create
After DB is initialized, you can change the database generation setting back to update
quarkus.hibernate-orm.scripts.generation=update
Upvotes: -1
Reputation: 495
Please refer to the guide at https://quarkus.io/guides/hibernate-orm#quarkus-hibernate-orm_quarkus.hibernate-orm.scripts-database-scripts-related-configuration
The property "quarkus.hibernate-orm.sql-load-script" is used for loading the data (insert statements primarily).
For DDL (Create and Drop tables), you might have to use the properties below:
quarkus.hibernate-orm.scripts.generation=drop-and-create
quarkus.hibernate-orm.scripts.generation.create-target=create.sql
quarkus.hibernate-orm.scripts.generation.drop-target=drop.sq
Upvotes: 5