Reputation: 333
My spring boot 2.0 application recognizes and runs schema.sql to initialize my embedded h2 database. But when I add spring-cloud-starter-config dependency the application no longer runs the schema.sql. To illustrate, use spring initializr to generate a Spring Boot 2 (v2.0.1) application with dependencies on:
Add an entity:
package com.example.demo;
import javax.persistence.*;
@Entity
public class Room {
@Id
@Column(name = "ROOM_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
//...getters and setters
}
A Repository for the entity:
package com.example.demo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RoomRepository extends CrudRepository<Room, Long> {
}
Main class is unchanged from what initializr generated:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Add schema.sql and data.sql file to the base folder under resources. These are used by Spring Boot to create and populate the underlying table for the entity:
schema.sql:
CREATE TABLE ROOM(
ROOM_ID BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(16) NOT NULL,
);
data.sql:
INSERT INTO ROOM (NAME) VALUES ('Piccadilly');
INSERT INTO ROOM (NAME) VALUES ('Cambridge');
INSERT INTO ROOM (NAME) VALUES ('Oxford');
INSERT INTO ROOM (NAME) VALUES ('Manchester');
Replace the empty application.properties with this application.yml:
spring:
jpa:
hibernate.ddl-auto: none
Now, run the application and go to http://localhost:8080/rooms. Application will fail with JdbcSQLException showing that the table does not exist:
org.h2.jdbc.JdbcSQLException: Table "ROOM" not found; SQL statement: select room0_.room_id as room_id1_0_, room0_.name as name2_0_ from room room0_
Now go into pom.xml and comment out the reference to spring-cloud-starter-config dependency:
<!--
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
-->
Restart the application and everything now works! Open http://localhost:8080/rooms in your browser and you will find the expected JSON result (with the 4 rows) is returned. This tells me it was the spring-cloud-starter-config dependency that stopped Spring from executing the schema.sql and data.sql to initialize the database.
How can I get Spring Boot to execute the schema.sql and data.sql files when I use the Spring Cloud dependency?
Upvotes: 4
Views: 870
Reputation: 1622
I had similar issue but during tests execution with H2 database schema creation. I've just tried newer versions - spring-boot-starter-parent:2.0.2.RELEASE
and Finchley.RC2
for Spring Cloud and it works in my case.
Spent few minutes with your example - reproducible with Spring Boot 2.0.1 and Spring Cloud Finchley.RC1, but works fine with 2.0.2 and Finchley.RC2. I didn't manage to find github issue, but looks like it was fixed.
Upvotes: 1