Spring Boot - Using import.sql in production? - spring

I am building a RESTful API using Spring Data REST with Spring boot. There are some data (eg. a list of countries) I would like to insert into the database when it is created so I am using import.sql to do this. But in the spring boot docs it is mentioned that import.sql is not good for production. To quote:
In addition, a file named import.sql in the root of the classpath will be executed on startup if Hibernate creates the schema from scratch (that is if the ddl-auto property is set to create or create-drop). This can be useful for demos and for testing if you are careful, but probably not something you want to be on the classpath in production. It is a Hibernate feature (nothing to do with Spring).
So my question is why is it bad to have import.sql in production and what would be a better alternative?
Note: I am only going to use import.sql the first time the application is deployed to production after that I will turn off hibernate-ddl. Any further changes to the schema will be made using Flyway.

Related

Is it possible to apply versioning on just data.sql and not schema.sql for Flyway in Spring Boot?

I am trying to use Flyway. I understand that it is used for migration purposes. My use case is to load data from data.sql in my classpath. And I will want to add more data so I will need another data.sql file in future.
Is it possible to version just the data.sql file using Flyway? I am using Spring Boot to configure this.
it might be worth while starting with the Flyway Migrate documentation to help with understanding Flyway versioning scripts and naming conventions.
https://flywaydb.org/documentation/concepts/migrations

Can a single Spring boot/Spring data application have repo of Posgres and MongoDB

I am having a need where in a same spring boot application I need to use repo of Spring Data Mongo and Spring data jpa (postgres), during my initial testing I got runtime errors. While I debug more just wanted to check in same Spring Data Application can it have both repos for Postgres and MongoDB ?
Yes, you can have, make sure to add all dependency and create separate repositories for each data connection. I used MongoDB and MySql database it is working fine.

Can SpringBoot be use for Backend application?

One of the Spring framework advantage is dependency injection. Many had used SpringBoot for providing REST Web Services.
Read up and notice there are Scheduler and CommandLineRunner for SpringBoot, could we using SpringBoot for backend type of application to replace the usual standalone java program while making use of SpringBoot advantage (Dependency Injection)
- Cron Job (Execute and stop running)
- Long Running Process
One of the main thing I am looking into is to use annotation such as Spring Configuration, Spring Data JPA and other technology in backend application.
Of course!
I used spring boot to back CLI projects, DB access projects and more.
Spring boot is very modular. It works by providing auto-configuration based on your maven/gradle imports. If you don't import starter-web/starter-jersey or any other starter that is for the web/rest api, the auto-configuration for this resources won't be triggered and you can basically enjoy all the power of spring boot to support your needs
Definitely,
Spring boot is not a separate framework.It reduces the configuration difficulties when you using spring framework. Spring boot provides a Rapid Application Development using without complex configuration including your dispatcher servlet, XML file for database connectivity and configuration files. You can use spring boot for back-end development. Simply says you can do everything what you does in spring MVC without any complex configuration. If you are using spring boot , You can configure your database details in application.properties file. I am adding one of two links for proper reading,
https://projects.spring.io/spring-boot/ ,
https://dzone.com/articles/why-springboot

If using Spring Boot and hibernate, should I use data.sql or import.sql to import data into my database, and why?

I am using Spring Boot 1.4.1, MySQL for our different environments (dev, qc, uat, live, staging). We are using H2 for our tests.
Sprint Boot says you can import data using data.sql
Hibernate seems to look for import.sql
Both work independently, but if I include both, only import.sql seems to run.
So, should I use data.sql or import.sql to import data into my database, and why?
When you use Spring JDBC use data.sql, when you use hibernate use import.sql
I would suggest taking a look in your hibernate.cfg.xml to choose file(s), yes, you can import files or a single file to be loaded in MySQL database. My instincts tell me this is a duplicate.
How to insert default data into table using import.sql file in Hibernate, MySQL Application

How to use Flyway in Spring Boot with JDBC Security?

I would like to use Flyway as preferred way of database migration handling in my Spring Boot project (using current V1.2.1.RELEASE).
This is working fine so far however the integration with Spring Security using a JDBC DataSource seems to override the Flyway mechanism.
Following simple scenario:
Spring Boot 1.2.1
PostgreSQL 9.4.1
Flyway migration scripts for users, groups and authorities according to Spring Security documentation
Problem:
The Flyway migration scripts are not executed at startup as expected.
Maybe cause: It seems that Flyway is only executed at startup if the using Spring Boot project is also using JPA at least. Since Spring Security is based on plain JDBC, I've tried to temporary use the JDBC based database initialization scheme as described in Spring Boot docs (Chapter 68.3) which works, but (as documented) this way is like the 'poor man approach' and I'd really like to use Flyway also for these tables containing the User/Group/Authorities information.
Ok, after some further investigation I've found the problem:
Indeed, in a standard Spring Boot project the security context is initialized before any Flyway based migration takes place.
Normally this is not a big issue, but I've also used the AuthenticationManagerBuilder for creating a default admin user. This seems to be the wrong approach for creating such an initial user account.

Resources