lundi 11 juin 2018

How to run MySQL ddl inside H2 while doing Spring boot integration test

I am newbee to integration test of Spring boot.

I am developing a new Spring boot application which use mysql as production database. I tried to use H2 as an Embedded database for integration test, so that it can run test independently.

My datasources configuration for spring boot is like this:

datasource:
    url: jdbc:h2:mem:;mode=mysql
    username: sa
    password: sa
    driver-class-name: org.h2.Driver

Actually I copied this from many articles and stackoverflow answers, I thought the mysql mode will work.

And my schema.sql is like this:

CREATE TABLE `opph_ordercenter_collector_order` (
  `id`                       bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `generate_time`            datetime            NOT NULL DEFAULT '1971-01-01 00:00:00',
  `source_type`              int(11) unsigned    NOT NULL DEFAULT '0' ,
  `source_trade_no`          varchar(64)         NOT NULL DEFAULT '1' ,
  `trade_order_detail`       text                NOT NULL ,
  `collector_order_id`       bigint(20) unsigned NOT NULL DEFAULT '0' ,
  `expected_plan_count`      int(11) unsigned    NOT NULL DEFAULT '0' ,
  `global_validation_status` tinyint(4)          NOT NULL DEFAULT '0',
  `update_time`              datetime            NOT NULL DEFAULT '1971-01-01 00:00:00',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_collector_order_id` (`collector_order_id`) ,
  UNIQUE KEY `uniq_source_type_source_trade_no` 
  (`source_type`,`source_trade_no`) 
)
  ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

And when I run my test, the schema.sql will be used to init the database, and I got this:

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/magicliang/Desktop/Programming/git/manhattan-oppenheimer-order-center-collector/out/test/classes/schema.sql]: ..... expected "INDEX, USING, NOCHECK, CHECK, ,, )"; SQL statement:

When I remove the index part and innodb part of my schema.sql, the exception is gone and tests passed.

I know h2 is not completely compatible to MySQL syntax. But if I need to run my test in H2, must I check the h2 sql mannul to revise my ddl line by line like this even I turn on the mode=mysql?

Or am I doing anything stupid with datasource configuration?

Aucun commentaire:

Enregistrer un commentaire