]> source.dussan.org Git - iciql.git/commitdiff
Added drop reference table test (fails on H2)
authorJames Moger <james.moger@gmail.com>
Mon, 15 Oct 2012 16:17:02 +0000 (12:17 -0400)
committerJames Moger <james.moger@gmail.com>
Mon, 15 Oct 2012 16:17:02 +0000 (12:17 -0400)
Unfortunately, it looks like H2 1.3.168 has a bug which allows you to
drop a table even though there are active constraints referencing it.
HSQL, Derby, MySQL, and PostgreSQL all throw a constraint violation
exception, as expected, but H2 does not.

src/com/iciql/IciqlException.java
tests/com/iciql/test/ForeignKeyTest.java

index 320604d1c4748dca6439698ba5c0ec24e371b8b7..38c8fac633d4be2dbbc3ab730c643fc6ec9252c4 100644 (file)
@@ -29,6 +29,7 @@ public class IciqlException extends RuntimeException {
        public static final int CODE_DUPLICATE_KEY = 2;\r
        public static final int CODE_OBJECT_NOT_FOUND = 3;\r
        public static final int CODE_OBJECT_ALREADY_EXISTS = 4;\r
+       public static final int CODE_CONSTRAINT_VIOLATION = 5;\r
 \r
        private static final String TOKEN_UNMAPPED_FIELD = "\\? (=|\\>|\\<|\\<\\>|!=|\\>=|\\<=|LIKE|BETWEEN) \\?";\r
 \r
@@ -97,6 +98,9 @@ public class IciqlException extends RuntimeException {
                        if ("23000".equals(state)) {\r
                                // MySQL duplicate primary key on insert\r
                                iciqlCode = CODE_DUPLICATE_KEY;\r
+                               if (s.getErrorCode() == 1217) {\r
+                                       iciqlCode = CODE_CONSTRAINT_VIOLATION;  \r
+                               }\r
                        } else if ("23505".equals(state)) {\r
                                // Derby duplicate primary key on insert\r
                                iciqlCode = CODE_DUPLICATE_KEY;\r
@@ -139,6 +143,15 @@ public class IciqlException extends RuntimeException {
                        } else if ("42504".equals(state)) {\r
                                // HSQL index already exists\r
                                iciqlCode = CODE_OBJECT_ALREADY_EXISTS;\r
+                       } else if ("2BP01".equals(state)) {\r
+                               // PostgreSQL constraint violation\r
+                               iciqlCode = CODE_CONSTRAINT_VIOLATION;\r
+                       } else if ("42533".equals(state)) {\r
+                               // HSQL constraint violation\r
+                               iciqlCode = CODE_CONSTRAINT_VIOLATION;\r
+                       } else if ("X0Y25".equals(state)) {\r
+                               // Derby constraint violation\r
+                               iciqlCode = CODE_CONSTRAINT_VIOLATION;\r
                        }\r
                }\r
        }\r
index 3e0314c84fd99959ac6d79ff75c0b2471d10545f..d7894086484995e8f58b8db2b281e4556c3e96b7 100644 (file)
 package com.iciql.test;\r
 \r
 import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertTrue;\r
 \r
 import org.junit.After;\r
 import org.junit.Before;\r
 import org.junit.Test;\r
 \r
 import com.iciql.Db;\r
+import com.iciql.IciqlException;\r
 import com.iciql.test.models.CategoryAnnotationOnly;\r
 import com.iciql.test.models.ProductAnnotationOnlyWithForeignKey;\r
 \r
@@ -62,6 +64,15 @@ public class ForeignKeyTest {
                \r
                assertEquals(count1, count2 + 2L);\r
        }\r
-\r
+       \r
+       @Test\r
+       public void testForeignKeyDropReferenceTable() {\r
+               try {\r
+                       db.dropTable(CategoryAnnotationOnly.class);\r
+                       assertTrue("Should not be able to drop reference table!", false);\r
+               } catch (IciqlException e) {\r
+                       assertEquals(e.getMessage(), IciqlException.CODE_CONSTRAINT_VIOLATION, e.getIciqlCode());\r
+               }\r
+       }\r
 \r
 }\r