Why Spring Boot 2.0 application does not run schema.sql?
Not embedded (e.g. MySQL)
If you load a database that is not embedded, in Spring Boot 2 you need to add:
spring.datasource.initialization-mode=always
Check the Migration Guide:
Database Initialization
Basic DataSource initialization is now only enabled for embedded data sources and will switch off as soon as you’re using a production database. The new
spring.datasource.initialization-mode
(replacingspring.datasource.initialize
) offers more control.
Embedded (e.g. h2)
I once had a similar problem, even though it was an h2 (so it was an embedded DB), my h2 configuration was activated by a my-test
profile.
My test class was like:
@RunWith(SpringRunner.class)
@SpringBootTest // does not work alone
@ActiveProfiles("my-test")
public class MyEntityRepositoryTest {
The problem is @SpringBootTest
alone did not initialize the test database. I had to either use @DataJpaTest
or @SpringBootTest
+@AutoConfigureTestDatabase
. Examples
@RunWith(SpringRunner.class)
@DataJpaTest // works
@ActiveProfiles("sep-test")
public class MyEntityRepositoryTest {
or
@RunWith(SpringRunner.class)
@SpringBootTest // these two
@AutoConfigureTestDatabase // together work
@ActiveProfiles("sep-test")
public class MyEntityRepositoryTest {
Check the documents here.
In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql, but you cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql.
You have spring.jpa.hibernate.ddl-auto=create-drop
that's why schema.sql
is not executed.
Looks like this is the way Spring Boot works.
Edit
I think that the problem(not really a problem) is that your application points to a mysql instance.
See the current Spring Boot properties:
spring.datasource.initialization-mode=embedded # Initialize the datasource with available DDL and DML scripts.
The default value is embedded
- e.g. initialize only if you're running and embedded database, like H2.
Also see the answer of Stephan here. He said:
Adding spring.datasource.initialization-mode=always to your project is enough.
So try to set:
spring.datasource.initialization-mode=always