spring.datasource.platform is not working - java

I made a project from scratch just with the schema.sql and the data.sql just to try the schema.sql and the data.sql:
https://github.com/rmmcosta/TestSchema
Everything works fine. The table inside schema.sql is created in a MySql database (previously created and the grants were given to the user defined in application.properties) and the data.sql populates the data as it's supposed to do.
But, when I change schema.sql and data.sql to schema-mysql.sql and data-mysql.sql and I put in the application.properties the property spring.datasource.platform=mysql the schema-mysql.sql and the data-mysql.sql are not being executed.
No errors are being thrown, simple nothing happens on the database.
I tried with spring boot 2.2.4 and it works fine, but with spring boot 2.7.5 it isn't working.
Do you know if the spring.datasource.platform was deprecated? And if so, do you know how can I set the application.properties in order to run schema-mysql.sql?
Thank you in advance,
Ricardo
Note:
I tried without using spring.datasource.platform=mysql and with schema.sql and data.sql and everything works fine.
I tried with an old project, spring boot 2.2.4 and java 1.8, and works fine.

Do you know if the spring.datasource.platform was deprecated? And if so, do you know how can I set the application.properties in order to run schema-mysql.sql?
The property name changed to spring.sql.init.platform
https://github.com/spring-projects/spring-boot/blob/d870474fcd4899fac94d51311c4163832d6b109d/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json#L1148
Which occurred in Spring Boot 2.5: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.5.0-RC1-Configuration-Changelog

You need to use spring.sql.init.platform instead of spring.datasource.platform property in application.properties.
Please see the comment from latest documentation -
In addition, Spring Boot processes the schema-${platform}.sql and
data-${platform}.sql files (if present), where platform is the value
of spring.sql.init.platform

Related

I'm not able to disable the flyway in the application properties using the intellij

I'm using the spring boot to connect a postgreSQL database and when I run the application using Intellij it return me a error message
Description:
Flyway failed to initialize: none of the following migration scripts locations could be found:
- classpath:db/migration/postgresql
The research done in stackoverflow suggested me to disable the flyway in the application properties like showed below.
###################################################################################
# DB Migration - automatically execute SQL scripts
###################################################################################
cn.app.datasource-populate.enabled=false
spring.flyway.enabled=false
spring.flyway.locations=classpath:db/migration/{vendor}
spring.flyway.table=${spring.application.name}_schema_version
spring.flyway.baseline-on-migrate=true
spring.flyway.baseline-version=0
spring.sql.init.mode=always
#spring.jpa.hibernate.ddl-auto=none
I have changed the line spring.flyway.enable from true to false but strangely when I run the application in intellij it automatically turn to true and do not fix the error.
Does anyone know why it is happening?
Can you try to remove your target folder(Also try invalidating the cache if this doesn't work) from the project and try rebuilding again? Sometimes Intellij doesn't pick the changes in application.properties.

How to use Eclipse Variables in Spring Boot application.properties

I'm very new to Spring boot and I'm setting up my DB connection in then application.properties. Since I'd like to upload my project on GitHub I'm trying to access the datasource id and password from enviroment variables, instead of hardcoding them inside the properties file. But I'm not really sure about how to do it.
I'm using a Sql server instance running in a container, and I've tried the following:
In "run configuration", in the Enviroment Tab, I added 2 enviroment variables : DB_USERNAME and DB_PASSWORD
The application.properties looks like this:
spring.datasource.username=${DB_USERNAME)
spring.datasource.password=${DB_PASSWORD)
But when i do run the spring boot application i get the following error:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '${DB_USERNAME)'
Can you help me figure out how to use the eclipse enviroment variables for this purpose in the right way?
Looks like there is a typo there:
spring.datasource.username=${DB_USERNAME)
spring.datasource.password=${DB_PASSWORD)
Should actually be:
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
You can also specify default that would be applied if variables are not provided:
spring.datasource.username=${DB_USERNAME:username}
spring.datasource.password=${DB_PASSWORD:password}

Spring can't locate a driver class that *is* in the classpath

I'm trying to run a springboot app on a tomcat server that includes a datasource that's able to communicate with a vault and change db credentials during runtime. The only change I've made in this code is adding some properties that are necessary to communicate with the vault, and changing the datasource configuration to include these vault changes.
I get the following error during startup:
Description:
Cannot determine embedded database driver class for database type NONE
However, in my application.properties file I DO have the driver class specified...
spring.datasource.hikari.driverClassName=com.ibm.db2.jcc.DB2Driver
and in the pom file, I have the correct dependency so the driver is infact included in the classpath... I even see the jar in Intellij's 'External Libraries' drop down.
Again, I've not made many changes besides adding in addtional properties for our vault... and changing the code inside our datasource config to use the vault.
I've compared my changes against another module in which I did the exact same thing, and didn't have this issue at all there.
Does anyone have any ideas as to why this is happening, or suggestions on what I can try?
I've tried including a #Import annotation on my #Configuration class, which points to the vault configuration. I've tried adding a #ComponentScan on my application class to try and really get it to look at the config and properties properly.
If any further detail is required please just let me know. Thanks in advance for any and all help that can be offered.
I have faced same problem and got resolved by below annotation
#SpringDataApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
please provide code snippet.So, i can debug it.

How to run data.sql script when first run spring boot application in a new environment?

As we know hibernate.hbm2ddl.auto=update configuration can generate database tables automate when we run the application in a new environment and hibernate.hbm2ddl.auto=create can run some scripts like a data.sql file in classpath after generating tables in the database.
here comes the question, how can we run the data.sql script under the configuration hibernate.hbm2ddl.auto=update?
or is there any other solution when we want to init some data in the database after we deploy the application in a new environment?
data.sql is a Spring Boot mechanism and has nothing to do with Hibernate.
If you set spring.datasource.initialization-mode=always the script is executed:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
You could use some stored procedure code to initialize the database in the data.sql.
Another approach could be to listen for ApplicationReadyEvent and then to initalize the database.
#EventListener(ApplicationReadyEvent.class)
public void doSomethingAfterStartup() {
// Initalize the database
}

SpringBoot test configuration

I've started to learn Spring Boot in version 2.1.0 and I made a simple application Controller->Service->Repository->Database(H2). Very, very simple application just to start with Spring Boot.
I've read that in that framework I can add under src/resources a file data.sql where I can define insert/update etc. and after run my application that data will be stored. Everything works ok, but when I wanted to write test for my service, just to check if my repository works ok (I test DB for learning purpose) I see that while I run my test in my DB there are already values from data.sql, but I don't have data.sql under test folder. It is under src.
Maybe someone knows, how to configure project that not to take this data.sql from src/resources? Is it possible? Maybe I have to add some more annotations?
**** EDIT ****
This is my repo:
You can create test application-test.properties in test/resources
and override in in test like this:
#RunWith(SpringRunner.class)
#SpringApplicationConfiguration(classes = ExampleApplication.class)
#TestPropertySource(locations="classpath:test.properties")
public class ExampleApplicationTests {
}
Then in you new created application-test.properties file block running your script:
spring.datasource.initialization-mode=never

Categories