Spring Boot h2 initial data not creating table first - spring-boot

I am trying to create a small Spring Boot setup with a h2 database.
Now I have a weird problem, which I can't solve.
If I don't create an data.sql for initial data, the app starts fine and creates my entity tables.
If I create an data.sql for initial data and keep the existing table from previous step, everything works fine.
If I create an data.sql for initial data and remove my existing h2 file, I get the error that it can't import the data, because the table is missing.
How do I tell Spring to create my tables before importing the initial data?

This is covered in the release notes for Spring Boot 2.5:
By default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.

Related

Spring boot hibernate application throws Negative revision numbers are not allowed when inserting

I have taken a DB dump from my dev database to QA database ( oracle) for testing. My application is a spring boot application and uses hibernate envers for auditing. I get above error when trying to insert data to the tables. I tried removing data from all the audit tables and revinfo table. But the issue is still there. Anybody has any idea on this?
Same error after updating the version of hibernate-envers for springboot3+. One quick solution for me was to (first backup) remove all tables related to audit + remove revinfo table and also the sequencer (revinfo_seq if you created some). Then let the ddl-auto property do the work by setting in application.yml
jpa.hibernate.ddl-auto: update
It creates all needed tables and sequencers by its needed definition, and after that inserts were committed.

Springboot Database Initialization

As I understand from Spring reference docs, we can use data.sql and schema.sql for initializing the database.
When there are multiple instances of the application running against a single database, how to make the data.sql, schema.sql run only once and instead of executing in all the containers.
You need to disable the default spring data initialization. You can do this through your properties file by
spring.datasource.initialization-mode=never
While Spring do not recommend using multiple data source initialization technologies, if you want script-based DataSource initialization to be able to build upon the schema creation performed by Hibernate, set
spring.jpa.defer-datasource-initialization
To true, this will defer data source initialization until after any EntityManagerFactory beans have been created and initialized.
And for managing your database across multiple application instance you can use Flyway and Liquibase, sing the basic schema.sql and data.sql scripts alongside Flyway or Liquibase is not recommended and support will be removed in a future release.

Programmatically recreate H2 database schema in SpringBoot application (not while unit testing)?

I have a SpringBoot application with in memory H2 database and Spring Data JPA.
I need to configure a #Scheduled job that drops and recreates the schema and loads it with fresh data from a file.
How can I programmatically recreate the schema in my application?
You can use database version control tool like eg Liquibase to create and maintain database schema definition as well as initial data. Than, you will be able to easily invoke database migration including drop of whole schema during applicaiton runtime. IT has some integration with Spring Boot already.
Keep in mind, that you will have to lock database access in order to execute migration - DDL is not transactional, so database will be of no use anyway during the migration process and you app can yeld many errors during that time.
If locking is not an option - you should be able to create another instance or at least separate schema in running instance, run migration against it and if everything is done, "switch" peristence context to use brand new schema (and probably remove the old one)

How can I make my flyway default DB pointing to different db and my actual Spring boot Application to some other DB

I am trying to integrate flyway to my existing Spring boot app. My requirement is, the default table created by flyway needs to be created on different DB schema and my applications actual table migration must happen on different schema. Reason being, no addition table must be created on the applications DB schema. Currently we are using Mysql
We had a Spring Boot app with the flyway configuration in application.properties file for different environments:
flyway.enabled=true
flyway.url=jdbc:postgresql://localhost:5432/finance
flyway.schemas=user
flyway.password=password
flyway.user=postgres
flyway.baseline-on-migrate=true
We had the application-dev.properties, application-local.properties and application-aws.properties with the configuration specific to the environment
Flyway provides a property flyway.schemas. When the flyway.schemas property is set (multi-schema mode), the schema history table is placed in the first schema of the list.
https://flywaydb.org/documentation/commandline/migrate.html
ex:flyway.schemas=schema1,schema2,schema3

Any way to execute the schema.sql (or concrete schema-postgres.sql) after "schema from model" creation?

Is there any way to tell the Spring Boot to execute its schema.sql or in my case schema-postgres.sql after creating the database model from object model?
The relevant part of my application.properties looks like that
spring.datasource.platform: postgresql
spring.jpa.generate-ddl: true
spring.jpa.hibernate.ddl-auto: update
In my schema-postgres.sql I define a view on two of the tables specified by my object model.
Of course when the app is started for the first time there are no tables in the schema yet and Spring Boot tries to execute schema-postgres.sql before creating the actual schema, which as the result ends with an error.
I could not find anything in the doc on it.

Resources