Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 339
iPhoneJavaDev Avatar asked Dec 05 '22 12:12

iPhoneJavaDev


2 Answers

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

like image 191
Javadroider Avatar answered Dec 15 '22 20:12

Javadroider


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.

like image 31
kkaun Avatar answered Dec 15 '22 20:12

kkaun