h2 drop drop after drop constraint failes

时间:2016-04-26 15:32:41

标签: database h2

问题:

我们尝试在h2中运行您的更新脚本以进行测试。 每个版本都有一个update.sql来运行DB上的更新。

但是在尝试删除约束(外键)时遇到问题:

错误:

Exception in thread "main" org.h2.jdbc.JdbcSQLException: Index "FP_ACL_PERMISSION_UNQ" belongs to constraint "CONSTRAINT_31B"; SQL statement:
ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ; [90085-191]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.command.ddl.DropIndex.update(DropIndex.java:63)
    at org.h2.command.CommandContainer.update(CommandContainer.java:98)
    at org.h2.command.Command.executeUpdate(Command.java:258)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:184)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:158)
    at com.rbs.mib.portal.HelloWorld.main(HelloWorld.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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

重现问题的代码:

public class ReproduceH2Problem{

    public static void main(String... args) throws Exception {

        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:~/test;MODE=ORACLE", "sa", "sa");
        Statement stat = conn.createStatement();

        stat.execute("DROP ALL OBJECTS");

        // init v1.0.0
        stat.execute("CREATE TABLE FP_ACL" +
                "(" +
                "  ACL_ID         INTEGER NOT NULL," +
                "  REPORT_KEY     VARCHAR2(100)             NOT NULL," +
                "  PARENT_ACL_ID  INTEGER" +
                ");");

        stat.execute("CREATE TABLE FP_ACL_PERMISSION" +
                "(" +
                "  PERMISSION_ID  INTEGER NOT NULL," +
                "  ACL_ID         INTEGER                        NOT NULL," +
                "  NAME           VARCHAR2(100)" +
                ");");
        stat.execute("CREATE UNIQUE INDEX FP_ACL_PERMISSION_UNQ ON FP_ACL_PERMISSION" +
                "(ACL_ID, NAME);");

        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
                "CHECK (ACL_ID IS NOT NULL);");

        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
                "PRIMARY KEY " +
                "(PERMISSION_ID);");

        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
                "CONSTRAINT FP_ACL_PERMISSION_UNQ " +
                "UNIQUE (ACL_ID, NAME);");


        // !! this seems to be the "bad guy" !!
        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
                "FOREIGN KEY (ACL_ID) " +
                "REFERENCES FP_ACL (ACL_ID);");


        //update v1.0.1
        stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP CONSTRAINT FP_ACL_PERMISSION_UNQ;");

        //here the problems start
        stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ;");
        stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP COLUMN ACL_ID;");
        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD CONSTRAINT FP_ACL_PERMISSION_UNQ UNIQUE (NAME);");

        stat.close();
        conn.close();
    }
}

1 个答案:

答案 0 :(得分:1)

我必须通过在sql脚本中添加元命令来做一个解决方法

  • 无法重命名约束(h2不支持)
  • 直接subselect不起作用 alter table TABLE_NAME drop constraint (select unique_index_name from information_schema.constraints where table_name='TABLE_NAME' and CONSTRAINT_TYPE='REFERENTIAL' and COLUMN_LIST= ='SHORT_ID')

解决方法:

    update.sql脚本中的
  1. Meta Command

    --H2-DROP-REFERENCE TABLE::FP_ACL_PERMISSION:: COLUMN::ACL_ID::
    ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ;
    
  2. 在groovy测试中过滤元命令:

    public static enum H2_META_COMMAND {
     DROP_REFERENCE("--H2-DROP-REFERENCE")
    
     private String command;
     H2_META_COMMAND(String command){
      this.command = command
     }
    
     String getCommand(){
       return command;
     }
    } 
    
  3. /**
     * This checks for custom H2 Meta commands to close oracle compatibility gap
     * @param sqlCommand a single command
     */
    public static void executeH2MetaCommands(String sqlSingleCommand, Statement statement){
        if(sqlCommand.contains(H2_META_COMMAND.DROP_REFERENCE.getCommand())){
            int tableStart = sqlCommand.indexOf("TABLE::", sqlCommand.indexOf(H2_META_COMMAND.DROP_REFERENCE.getCommand()) + H2_META_COMMAND.DROP_REFERENCE.getCommand().size() ) + 7
            String table = sqlCommand.substring(tableStart, sqlCommand.indexOf("::",tableStart))
            int colStart = sqlCommand.indexOf("COLUMN::") + 8
            String col = sqlCommand.substring(colStart, sqlCommand.indexOf("::",colStart))
            ResultSet rs = statement.executeQuery("select CONSTRAINT_NAME from information_schema.constraints where table_name='"+ table+"' and CONSTRAINT_TYPE='REFERENTIAL' and COLUMN_LIST= '"+col+"'")
            rs.next()
            String constraintName = rs.getString(1)
            rs.close()
            statement.execute("ALTER TABLE FP_ACL_PERMISSION DROP constraint " + constraintName)
        }
    }