H2 User defined functions for MySQL.
I always use H2 for unit test instead of real MySQL(Docker), but some MySQL functions are not available in H2. So I create this project to define some functions for MySQL and make unit test easy.
In you pom.xml add following dependency:
For H2 1.4.x, please use 1.0.0 version as following:
<dependency>
<groupId>org.mvnsearch</groupId>
<artifactId>h2-functions-4-mysql</artifactId>
<version>1.0.0</version>
</dependency>
For H2 2.0.x, please use 2.0.0 version as following:
<dependency>
<groupId>org.mvnsearch</groupId>
<artifactId>h2-functions-4-mysql</artifactId>
<version>2.0.0</version>
</dependency>
- Flyway with Spring Boot: automatically(classpath:db/migration/h2/V0_functions.sql), no configuration.
- Manual: Add following code in your application. datasource is h2 datasource.
H2FunctionsLoader.loadMysqlFunctions(dataSource);
- From H2 210, and you can use INIT parameter to load functions automatically.
String jdbcUrl = "jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:db/migration/h2/V0_functions.sql'";
String jdbcUrl2 = "jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:db/migration/h2/V0_functions.sql';RUNSCRIPT FROM 'classpath:your_script.sql'";
N: not available, Y: same, A: available
- H2 functions: http://www.h2database.com/html/functions.html
- MySQL functions: https://dev.mysql.com/doc/refman/8.0/en/func-op-summary-ref.html
- application-test.properties with H2 MySQL Mode support
### datasource
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:public;MODE=MYSQL;DATABASE_TO_UPPER=false;INIT=CREATE SCHEMA IF NOT EXISTS public
spring.datasource.username=sa