How to execute SQL files outside of spring application using maven command? - java

I have a spring boot application which is using Postgresql and also maven. It uses the Flyway migration to migrate database changes. I want to have a specific SQL file and run it by a command to insert some records for configuration whenever I want.
Could you please help me to handle this?

The specific topic is detailed here Spring Database Initialization.
Basically set the initialization values as you require:
spring.datasource.platform=postgres
spring.jpa.hibernate.ddl-auto=update
spring.datasource.initialization-mode=always
....
E.g. (there are much more options) if you place into resources the file data.sql it will be executed and you can add your fixtures.
For specific Flyway migrations you should check Execute Flyway Database Migrations on Startup.
Basically you add every new migration version script and it will be checked for every deployment.

Related

How to run DDL after Spring auto create db?

My question is simple. How to run any DDL statements after Spring's automatic schema creation?
I have set the following in my application-test.properties to auto create the database schema from the entity Java classes.
spring.jpa.hibernate.ddl-auto=create-drop
I'd like to change the data type of one column in one table after the schema is auto created.
I tried having a schema.sql file in the classpath, but that didn't help.
Any help please.
Scripts which you specify in script will run before it create database.
So when you fire Alter table command, you will get Table not found kind of error.
What I would suggest is to create database via SQL file only and set
spring.jpa.hibernate.ddl-auto=none so system wont create auto database.
Now to make your SQL file run, You can create the schema and initialize it based on the platform. Platform value is of spring.datasource.platform. Now you can create files schema-${platform}.sql and data-${platform}.sql which is processed by Spring Boot. It allows you to choose the database specific scripts if necessary. You can choose the vendor name of database(platform) like hsqldb, h2, oracle, mysql, postgresql, and so on.
I have tested with postgres and It worked for me. Sql file name was schema-postgres.sql
Set below properties in application.properties file
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/poc
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialization-mode=always

How to get ddl auto generated script in Spring boot JPA/Hibernate?

Is there a way to quickly get initial SQL script for the database that is being generated by hibernating through ddl-auto property?
I would like to get an initial SQL script for the flyway as a quick start.
use property spring.jpa.show-sql = true and it will print all script on console in format.
use ddl-auto as you did and take backup of schema for your respective DB and use that as initial script
example - mysql , postgres
You can see the DDL script generated by Hibernate in a Spring boot application by setting the logging level of Hibernate to debug mode in the application.properties file of your project.
logging.level.org.hibernate.SQL=DEBUG

Connect Java spring project with cloud database

I have created a spring boot project in Java, it works perfectly fine with my local database but I am not able to connect it to my GCP postgres cloud SQL instance.
I have followed the below steps: https://cloud.spring.io/spring-cloud-gcp/multi/multi__spring_jdbc.html
I also did the required changes in the pom.xml.
Is there anything I need to change in the application.properties file?
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.schema=classpath:/schema.sql
spring.datasource.continue-on-error=true
I want my project to working properly, pointing to cloud database.
Depend on which cloud you are using.
There is End Point for you created database.
That Endpoint link you have to put instead of Localhost.spring.datasource.url=jdbc:postgresql://(put your end point here instead of localhost):5432/postgres
Make sure that your database name should be same as in the link.
If you are using Spring there so you should first create database development environment.
Other than in Pom.xml you need to add Dependency of cloud.
If you want to access that DB then you have to go for JackDB platform.
Thanks & Regards,
JerryRank

Spring-boot application, H2 embedded database - but tables disappear when the application closes

My database.properties file is:
datasource.driver=org.h2.Driver
datasource.url=jdbc:h2:file:./test_database/comixed_db;create=true
datasource.username=sa
datasource.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.batch.size=20
hibernate.current.session.context.class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.generate_statistics=false
hibernate.enable_lazy_load_no_trans=false
When my application starts up I see I am able to see the tables by using the h2.bat tool and peeking at the database from a web browser. However, when I shut down the application and then go back to the database with the h2.bat tool the tables are all gone!
AM I doing something incorrectly with my hibernate configuration? I am not using create-drop but update since this code is currently in flux and I'd like the tables to be adjusted as changes occur. But that doesn't seem to be the issue since it's at app shutdown that the tables keep going away.
Any help would be appreciated.
If you want spring boot to catch your hibernate properties you should prefix them with spring.jpa, so:
spring.jpa.hibernate.ddl-auto=update
Otherwise, and that is the case in my opinion, spring will use the default create-drop options as it is dealing with an H2 in-memory database.
By adding the following line to applications.properties:
spring.jpa.hibernate.ddl-auto=update
Spring-boot stopped dropping tables when the application exits.

CDI Flyway with multiple database vendor

Flyway provides framework which will execute migrations based on directory
/data/migration
My application supports Oracle, SQLServer or MySQL, i would like to keep scripts as /data/migration/oracle and /data/migration/sqlserver etc...
Application can be deployed either on Oracle, SQLServer or MySQL. How to inform Flyway framework to use specific database deployment migrations using spring integration?
You will pass different location for each database. You may have placeholder for that. I have it done in maven plugin ond only what is needed to be changed is
<locations>
<location>db/${database.type}</location>
</locations>

Categories

Resources