gpt4 book ai didi

java - JUnit 使用 h2 数据库测试 flywaydb

转载 作者:行者123 更新时间:2023-11-30 08:09:43 28 4
gpt4 key购买 nike

我想 JUnit 我的迁移。我正在使用 jdbc:h2:mem:./test 作为 url 在内存中设置一个 H2 数据库。

Flyway 正常连接到数据库但无法执行 sql 脚本给我以下信息

com.nokia.avalanche.manager.migrations.FlywaydbWrap$MigrationException: Something happened in migration
at com.nokia.avalanche.manager.migrations.FlywaydbWrap.migrateDB(FlywaydbWrap.java:66)
at com.nokia.avalanche.manager.databasemanagement.FlywayDbMigrationTest.testIsDbCreated(FlywayDbMigrationTest.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException:
Migration V1__Base_version.sql failed
-------------------------------------
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "LOCK[*] TABLES ""ENODEB"" WRITE "; SQL statement:
LOCK TABLES `enodeb` WRITE [42000-187]
Location : db/migration/V1__Base_version.sql (D:\development\IPSL_source\trunk\myProject\target\classes\db\migration\V1__Base_version.sql)
Line : 120
Statement : LOCK TABLES `enodeb` WRITE

at org.flywaydb.core.internal.dbsupport.SqlScript.execute(SqlScript.java:117)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor.execute(SqlMigrationExecutor.java:71)
at org.flywaydb.core.internal.command.DbMigrate$5.doInTransaction(DbMigrate.java:284)
at org.flywaydb.core.internal.command.DbMigrate$5.doInTransaction(DbMigrate.java:282)
at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:72)
at org.flywaydb.core.internal.command.DbMigrate.applyMigration(DbMigrate.java:282)
at org.flywaydb.core.internal.command.DbMigrate.access$800(DbMigrate.java:46)
at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(DbMigrate.java:207)
at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(DbMigrate.java:156)
at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:72)
at org.flywaydb.core.internal.command.DbMigrate.migrate(DbMigrate.java:156)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:1059)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:1006)
at org.flywaydb.core.Flyway.execute(Flyway.java:1418)
at org.flywaydb.core.Flyway.migrate(Flyway.java:1006)
at com.nokia.avalanche.manager.migrations.FlywaydbWrap.migrateDB(FlywaydbWrap.java:63)
... 27 more
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "LOCK[*] TABLES ""ENODEB"" WRITE "; SQL statement:
LOCK TABLES `enodeb` WRITE [42000-187]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.message.DbException.getSyntaxError(DbException.java:191)
at org.h2.command.Parser.getSyntaxError(Parser.java:518)
at org.h2.command.Parser.parsePrepared(Parser.java:476)
at org.h2.command.Parser.parse(Parser.java:305)
at org.h2.command.Parser.parse(Parser.java:281)
at org.h2.command.Parser.prepareCommand(Parser.java:242)
at org.h2.engine.Session.prepareLocal(Session.java:461)
at org.h2.engine.Session.prepareCommand(Session.java:403)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1189)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:170)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:158)
at org.flywaydb.core.internal.dbsupport.JdbcTemplate.executeStatement(JdbcTemplate.java:238)
at org.flywaydb.core.internal.dbsupport.SqlScript.execute(SqlScript.java:114)
... 42 more

当我在我的真实 MySql 数据库上使用相同的 sql 脚本运行 flyway 时,它正常工作没有错误....H2 是否支持锁定表?

最佳答案

您可能在 flyway SQL 脚本中使用了 MySQL 命令。我建议将以下字符串添加到您的 H2 JDBC url:;MODE=MYSQL

如果使用的命令是H2的MySQL兼容模式支持的,可能会解决你的问题。如果不是,则您处于修复状态,您需要创建仅 H2 的 flyway SQL 脚本。

请注意:将基于 H2 内存的数据库与 flyway 一起使用可能会给您的 JUnit 带来麻烦。如果连接已关闭,您将无法重新打开它并找回内容。

祝你实验顺利。

更新:LOCK 在 H2 中似乎不受支持,即使使用 MODE=MYSQL。它可能会确认您需要在没有 MySQL 特定命令的情况下编写 flyway SQL。

关于java - JUnit 使用 h2 数据库测试 flywaydb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32222294/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com