iPhoneJavaDev
iPhoneJavaDev

Reputation: 825

Spring Boot not running schema.sql

I am having an issue running the schema.sql upon running the program.

In my pom.xml, I already have the mysql config:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

I only have one class

@SpringBootApplication
public class Application {...}

Under src/resources, I have schema.sql containing:

DROP TABLE IF EXISTS test_table;

CREATE TABLE test_table (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    ...
);

Under src/resources, I have application.yml containing:

spring.datasource.driverClassName: "com.mysql.jdbc.Driver"
spring.datasource.url: "jdbc:mysql://localhost:3306/sample?useSSL=false"
spring.datasource.username: "****"
spring.datasource.password: "****"

I have confirmed that I am able to connect to the database "sample" upon starting the application, however, it's not creating the table. Please advise.

Upvotes: 5

Views: 10871

Answers (3)

Ilman Iqbal
Ilman Iqbal

Reputation: 21

From spring boot version 2.7 onward, this will work:

spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=update

Upvotes: 1

kkaun
kkaun

Reputation: 603

In my case (Spring Boot 2.0.0+) it worked as expected only when property setting spring.datasource.initialization-mode=always was combined with spring.jpa.hibernate.ddl-auto=none.

Upvotes: 6

Javadroider
Javadroider

Reputation: 2450

That's because Spring Boot has check in DataSourceInitializer's initSchema() method.

enter image description here

It will execute scripts only if your database is of type H2,DERBY,HSQL

However, you can override this behaviour by using following setting in application.properties

spring.datasource.initialization-mode=always

DataSourceInitializer.java EmbeddedDatabaseConnection.java

Upvotes: 10

Related Questions