弹簧测试中用于TRIGGER的SQL DELIMITER导致错误

我有两个表和触发器。当我们在一个表中插入行时-然后触发在另一表中插入行。

  

revenue_multi_currency_test_init_script.sql:

CREATE TABLE `game_operation` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'GAME_OPERATION unique identifier',`time` DATETIME NOT NULL COMMENT 'Time',`total_bet` INT NOT NULL COMMENT 'Total bet of game operation',`total_win` BIGINT NOT NULL COMMENT 'Total win of game operation,including bonus,excluding jackpot',`balance` BIGINT DEFAULT 0 COMMENT 'Balance of game operation',`result` TEXT COMMENT 'Detailed result of game round,serialized as text',`game_session_id` BIGINT UNSIGNED NOT NULL COMMENT 'GAME_SESSION unique identifier during which operation is performed',`start_id` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Unique identifier of GAME_OPERATION that started current bet session (paid game)',PRIMARY KEY (`id`),KEY `ix_game_operation_game_session_start_id` (`game_session_id`,`start_id`)
);

CREATE TABLE `game_operation_hourly` (
  `id` BIGINT UNSIGNED NOT NULL COMMENT 'Composed value of (HOUR_NUMber<<45) + (GAME_ID<<32) + PLAYER_ID,where HOUR_NUMber = (HOUR - 2019-01-01 00:00:00 UTC)/60/60',`hour` DATETIME NOT NULL COMMENT 'Timestamp when aggregated hour starts,inclusive',`total_bet` BIGINT NOT NULL COMMENT 'Sum of bets in aggregated hour',`total_win` BIGINT NOT NULL COMMENT 'Sum of wins in aggregated hour,`bet_count` INT NOT NULL COMMENT 'Count of operations in aggregated hour',`game_id` INT NOT NULL COMMENT 'Unique identifier of game engine,defined in code',`player_id` INT UNSIGNED NOT NULL COMMENT 'PLAYER unique identifier',KEY `ix_game_operation_hourly_hour_game_id` (`hour`,`game_id`),KEY `ix_game_operation_hourly_hour_player_id` (`hour`,`player_id`)
);

DELIMITER //
CREATE TRIGGER game_operation_insert AFTER INSERT ON `game_operation`
FOR EACH ROW
  BEGIN
    IF NEW.total_bet != 0 OR NEW.total_win != 0 THEN
      BEGIN
        DeclARE l_player_id INT;
        DeclARE l_game_id INT;
        DeclARE l_hour_int INT;
        DeclARE l_hour_date DATETIME;
        DeclARE l_id BIGINT;

        SELECT `player_id`,`game_id` INTO l_player_id,l_game_id FROM `game_session` WHERE `id` = NEW.game_session_id;

        IF (l_player_id IS NULL OR l_game_id IS NULL) THEN
          SIGNAL SQLSTATE '45000'
          SET MESSAGE_TEXT = 'Game session not found';
        ELSEIF (l_game_id > 8191) THEN
          SIGNAL SQLSTATE '45000'
          SET MESSAGE_TEXT = 'Parameter `game_id` is greater than 8191';
        END IF;

        SET l_hour_int := UNIX_TIMESTAMP(NEW.time) DIV 3600;
        SET l_id := ((l_hour_int - 429528) << 45) | (l_game_id << 32) | (l_player_id);
        SET l_hour_date := FROM_UNIXTIME(l_hour_int * 3600);

        INSERT INTO `game_operation_hourly` (`id`,`hour`,`total_bet`,`total_win`,`bet_count`,`game_id`,`player_id`)
          VALUES (l_id,l_hour_date,NEW.total_bet,NEW.total_win,IF(NEW.total_bet = 0,1),l_game_id,l_player_id)
          ON DUPLICATE KEY UPDATE `total_bet`=`total_bet`+VALUES(`total_bet`),`total_win`=`total_win`+VALUES(`total_win`),`bet_count`=`bet_count`+VALUES(`bet_count`);
      END;
    END IF;
  END//
DELIMITER ;

在mySQL workBanch和项目中的flyWay sql文件中,一切正常。但是,当我在测试中的脚本中插入触发器创建时,就会抛出错误

  

产生原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:   您的SQL语法有误;检查手册   对应于您的MySQL服务器版本以使用正确的语法   在'DELIMITER附近//创建触发器game_operation_insert AFTER INSERT之后   在第1行的“ game_operatio”上

我像这样测试我的脚本:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = ReportApplicationTests.Initializer.class)
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,scripts = "classpath:db/migration/revenue_multi_currency_test_init_script.sql")
@Import(DataSourceAutoConfiguration.class)

如何在测试中创建触发器?

CHGXING 回答:弹簧测试中用于TRIGGER的SQL DELIMITER导致错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3122050.html

大家都在问