Flyway clean不能以小写形式丢弃H2表

时间:2016-02-25 12:45:49

标签: java mysql h2 flyway

这是我的flyway配置:

url: jdbc:h2:./target/test;MODE=MYSQL;INIT=CREATE SCHEMA IF NOT EXISTS "test";

这就是我在测试套件中初始化它的方法:

Flyway flyway = new Flyway();
flyway.setDataSource(APP.getConfiguration().getDatabaseUrl(), APP.getConfiguration().getDatabaseUser(),
        APP.getConfiguration().getDatabasePass());
flyway.setBaselineOnMigrate(true);
flyway.setSchemas("test");
flyway.clean();
flyway.migrate();

这很好,但只是第一次。任何后续时间具有相同的架构,我都会收到错误:

org.flywaydb.core.api.FlywayException: Unable to drop "test"."my_table"

    at org.flywaydb.core.internal.dbsupport.SchemaObject.drop(SchemaObject.java:82)
    at org.flywaydb.core.internal.dbsupport.h2.H2Schema.doClean(H2Schema.java:69)
    at org.flywaydb.core.internal.dbsupport.Schema.clean(Schema.java:148)
    at org.flywaydb.core.internal.command.DbClean$4.doInTransaction(DbClean.java:154)
    at org.flywaydb.core.internal.command.DbClean$4.doInTransaction(DbClean.java:152)
    at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:72)
    at org.flywaydb.core.internal.command.DbClean.cleanSchema(DbClean.java:152)
    at org.flywaydb.core.internal.command.DbClean.clean(DbClean.java:106)
    at org.flywaydb.core.Flyway$3.execute(Flyway.java:1125)
    at org.flywaydb.core.Flyway$3.execute(Flyway.java:1121)
    at org.flywaydb.core.Flyway.execute(Flyway.java:1418)
    at org.flywaydb.core.Flyway.clean(Flyway.java:1121)
    at com.TestSuite.beforeClass(TestSuite.java:63)
    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:498)
    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.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    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:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.h2.jdbc.JdbcSQLException: Table "my_table" not found; SQL statement:
DROP TABLE "test"."my_table" CASCADE [42102-190]
    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.command.ddl.DropTable.prepareDrop(DropTable.java:69)
    at org.h2.command.ddl.DropTable.update(DropTable.java:113)
    at org.h2.command.CommandContainer.update(CommandContainer.java:78)
    at org.h2.command.Command.executeUpdate(Command.java:253)
    at org.h2.jdbc.JdbcPreparedStatement.execute(JdbcPreparedStatement.java:198)
    at org.flywaydb.core.internal.dbsupport.JdbcTemplate.execute(JdbcTemplate.java:219)
    at org.flywaydb.core.internal.dbsupport.h2.H2Table.doDrop(H2Table.java:43)
    at org.flywaydb.core.internal.dbsupport.SchemaObject.drop(SchemaObject.java:80)
    ... 31 more

如果我从初始化中删除flyway.clean(),由于数据库已经被填充,我得到一些错误与迁移不起作用,因此所有表都可以。

之前,在我添加MODE=MYSQL;之前,这项工作正常。以下是V1_migration.sql

中表格的创建方式
CREATE TABLE my_table (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, session_id INT, token VARCHAR(50), CONSTRAINT s_id FOREIGN KEY (session_id) REFERENCES user_sessions(id));

发生了什么事?为什么Flyway不能丢桌子?

1 个答案:

答案 0 :(得分:6)

必须使用

将H2数据库配置为不区分大小写
DATABASE_TO_UPPER=false;