aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Moger <james.moger@gmail.com>2012-10-15 12:17:02 -0400
committerJames Moger <james.moger@gmail.com>2012-10-15 12:17:02 -0400
commit6d3704b7bad4732a289c0be68e8f18a1fdf8abac (patch)
treefd4e7153189aba4af06859d1d31dce5cb31f3c93
parentef95be59dbd124ee07d195ef3706e1daaa25f768 (diff)
downloadiciql-6d3704b7bad4732a289c0be68e8f18a1fdf8abac.tar.gz
iciql-6d3704b7bad4732a289c0be68e8f18a1fdf8abac.zip
Added drop reference table test (fails on H2)
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.
-rw-r--r--src/com/iciql/IciqlException.java13
-rw-r--r--tests/com/iciql/test/ForeignKeyTest.java13
2 files changed, 25 insertions, 1 deletions
diff --git a/src/com/iciql/IciqlException.java b/src/com/iciql/IciqlException.java
index 320604d..38c8fac 100644
--- a/src/com/iciql/IciqlException.java
+++ b/src/com/iciql/IciqlException.java
@@ -29,6 +29,7 @@ public class IciqlException extends RuntimeException {
public static final int CODE_DUPLICATE_KEY = 2;
public static final int CODE_OBJECT_NOT_FOUND = 3;
public static final int CODE_OBJECT_ALREADY_EXISTS = 4;
+ public static final int CODE_CONSTRAINT_VIOLATION = 5;
private static final String TOKEN_UNMAPPED_FIELD = "\\? (=|\\>|\\<|\\<\\>|!=|\\>=|\\<=|LIKE|BETWEEN) \\?";
@@ -97,6 +98,9 @@ public class IciqlException extends RuntimeException {
if ("23000".equals(state)) {
// MySQL duplicate primary key on insert
iciqlCode = CODE_DUPLICATE_KEY;
+ if (s.getErrorCode() == 1217) {
+ iciqlCode = CODE_CONSTRAINT_VIOLATION;
+ }
} else if ("23505".equals(state)) {
// Derby duplicate primary key on insert
iciqlCode = CODE_DUPLICATE_KEY;
@@ -139,6 +143,15 @@ public class IciqlException extends RuntimeException {
} else if ("42504".equals(state)) {
// HSQL index already exists
iciqlCode = CODE_OBJECT_ALREADY_EXISTS;
+ } else if ("2BP01".equals(state)) {
+ // PostgreSQL constraint violation
+ iciqlCode = CODE_CONSTRAINT_VIOLATION;
+ } else if ("42533".equals(state)) {
+ // HSQL constraint violation
+ iciqlCode = CODE_CONSTRAINT_VIOLATION;
+ } else if ("X0Y25".equals(state)) {
+ // Derby constraint violation
+ iciqlCode = CODE_CONSTRAINT_VIOLATION;
}
}
}
diff --git a/tests/com/iciql/test/ForeignKeyTest.java b/tests/com/iciql/test/ForeignKeyTest.java
index 3e0314c..d789408 100644
--- a/tests/com/iciql/test/ForeignKeyTest.java
+++ b/tests/com/iciql/test/ForeignKeyTest.java
@@ -17,12 +17,14 @@
package com.iciql.test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.iciql.Db;
+import com.iciql.IciqlException;
import com.iciql.test.models.CategoryAnnotationOnly;
import com.iciql.test.models.ProductAnnotationOnlyWithForeignKey;
@@ -62,6 +64,15 @@ public class ForeignKeyTest {
assertEquals(count1, count2 + 2L);
}
-
+
+ @Test
+ public void testForeignKeyDropReferenceTable() {
+ try {
+ db.dropTable(CategoryAnnotationOnly.class);
+ assertTrue("Should not be able to drop reference table!", false);
+ } catch (IciqlException e) {
+ assertEquals(e.getMessage(), IciqlException.CODE_CONSTRAINT_VIOLATION, e.getIciqlCode());
+ }
+ }
}