Spring boot offers the great option to load initial data into the database on startup using data.sql. The problem is, I have a table where the column is unique and spring tries to executes the sqls on every startup and fails if the data already present. Now the question is, what is the proper way to handle such cases, is there any way to tell spring when to execute and when not to execute the data.sql file. Thanks
2 Answers
You have couple of options:
- Disable the database initialization at start up using
spring.datasource.initialize=false - Turning off Spring Boot's fail fast behavior using
spring.datasource.continue-on-error=true
Second option would let your Spring Boot app to start normally by ignoring any exceptions arising from script execution.
If both of the above options doesn't suit your requirement, as Darren Forsythe mentioned in comments, Consider data migration tools like Flyway or Liquibase. Spring boot provides great support for both tools. Check this out.