0

For Spring Boot 3.1.1

About dependencies is declared:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

In the src/main/resources exists only the following:

src/main/resources
   application.properties
   application-mysql.properties

Observe there are no .sql files. The application.properties file contains

spring.profiles.active=mysql,cache

The application-mysql.properties file - that must be used for runtime because the mysql profile was declared and the application-{profile}.properties pattern is applied - contains:

spring.datasource.url=jdbc:mysql://192....
spring.datasource.username=root
spring.datasource.password=<something>

spring.sql.init.mode=always
spring.sql.init.platform=mysql

The connection is accomplished and some CRUD operations is executed at peace but ...

but is expected to receive an error/exception at startup due the following:

  • spring.sql.init.mode=always because there is no an in-memory database such h2 and MySQL is used instead, therefore some sql scripts must be executed.
  • spring.sql.init.platform=mysql is expected to find the schema-mysql.sql and data-mysql.sql files at src/main/resources, it according with the schema-${platform}.sql and data-${platform}.sql patterns respectively

Now because the schema-mysql.sql and data-mysql.sql does not exist at src/main/resources is expected an error, but it does not happen. Why? ... Same situation if is declared

spring.sql.init.platform=mysqlabc

I am assuming some Application Property is missing

Consider the use the MySQL and not H2 for pre-production purposes.

4
  • Both scripts are optional, so no error will be thrown. Commented Jul 3, 2023 at 14:34
  • Interesting it is not mentioned at - docs.spring.io/spring-boot/docs/current/reference/html/… Commented Jul 3, 2023 at 14:40
  • 1
    You can see it in the implementation. Neither is required, you can only have a schema.sql, or a data.sql or a schema-mysql.sql and generic data.sql all sorts of combinations are possible, then what should be required? Although a warning in the logs could be an improvement if init is enabled but no sql files are actually found. Commented Jul 3, 2023 at 14:42
  • I see, I am going to create an issue on Github, a new Application Property would be added, it to enable or not to thrown an exception if the expected sql scripts are not found ... of of course for backwards compatibility it should be false as default Commented Jul 3, 2023 at 14:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.