From 75b3c1c1cf2203940634168e5a618b3d276fc325 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 15 Oct 2012 10:55:47 -0400 Subject: [PATCH] Fix name collissions on indexes, unique keys, and foreign keys --- src/com/iciql/TableDefinition.java | 14 +++++++------- tests/com/iciql/test/AnnotationsTest.java | 4 ++-- tests/com/iciql/test/ModelsTest.java | 10 +++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java index a2b4f36..1ecf394 100644 --- a/src/com/iciql/TableDefinition.java +++ b/src/com/iciql/TableDefinition.java @@ -294,7 +294,7 @@ public class TableDefinition { private void addIndex(String name, IndexType type, List columnNames) { IndexDefinition index = new IndexDefinition(); if (StringUtils.isNullOrEmpty(name)) { - index.indexName = tableName + "_" + indexes.size(); + index.indexName = tableName + "_idx_" + indexes.size(); } else { index.indexName = name; } @@ -325,7 +325,7 @@ public class TableDefinition { private void addConstraintUnique(String name, List columnNames) { ConstraintUniqueDefinition constraint = new ConstraintUniqueDefinition(); if (StringUtils.isNullOrEmpty(name)) { - constraint.constraintName = tableName + "_" + constraintsUnique.size(); + constraint.constraintName = tableName + "_unique_" + constraintsUnique.size(); } else { constraint.constraintName = name; } @@ -886,8 +886,8 @@ public class TableDefinition { try { stat.executeUpdate(); } catch (IciqlException e) { - // maybe we should check more error codes - if (e.getIciqlCode() != IciqlException.CODE_OBJECT_ALREADY_EXISTS) { + if (e.getIciqlCode() != IciqlException.CODE_OBJECT_ALREADY_EXISTS + && e.getIciqlCode() != IciqlException.CODE_DUPLICATE_KEY) { throw e; } } @@ -901,8 +901,8 @@ public class TableDefinition { try { stat.executeUpdate(); } catch (IciqlException e) { - // maybe we should check more error codes - if (e.getIciqlCode() != IciqlException.CODE_OBJECT_ALREADY_EXISTS) { + if (e.getIciqlCode() != IciqlException.CODE_OBJECT_ALREADY_EXISTS + && e.getIciqlCode() != IciqlException.CODE_DUPLICATE_KEY) { throw e; } } @@ -1090,7 +1090,7 @@ public class TableDefinition { ConstraintUpdateType updateType, ConstraintDeferrabilityType deferrabilityType) { ConstraintForeignKeyDefinition constraint = new ConstraintForeignKeyDefinition(); if (StringUtils.isNullOrEmpty(name)) { - constraint.constraintName = tableName + "_" + constraintsForeignKey.size(); + constraint.constraintName = tableName + "_fkey_" + constraintsForeignKey.size(); } else { constraint.constraintName = name; } diff --git a/tests/com/iciql/test/AnnotationsTest.java b/tests/com/iciql/test/AnnotationsTest.java index ad229d9..6aa75ad 100644 --- a/tests/com/iciql/test/AnnotationsTest.java +++ b/tests/com/iciql/test/AnnotationsTest.java @@ -79,8 +79,8 @@ public class AnnotationsTest { String index = rs.getString("INDEX_NAME"); list.add((col + ":" + index).toLowerCase()); } - assertTrue(list.contains("name:annotatedproduct_0")); - assertTrue(list.contains("cat:annotatedproduct_0")); + assertTrue(list.contains("name:annotatedproduct_idx_0")); + assertTrue(list.contains("cat:annotatedproduct_idx_0")); assertTrue(list.contains("name:nameidx")); } diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java index ceed7a9..c5ba27a 100644 --- a/tests/com/iciql/test/ModelsTest.java +++ b/tests/com/iciql/test/ModelsTest.java @@ -123,20 +123,20 @@ public class ModelsTest { // a poor test, but a start String dbName = IciqlSuite.getDatabaseEngineName(db); if (dbName.equals("H2")) { - assertEquals(1579, models.get(0).length()); + assertEquals(1587, models.get(0).length()); } else if (dbName.startsWith("HSQL")) { // HSQL uses Double instead of Float - assertEquals(1583, models.get(0).length()); + assertEquals(1591, models.get(0).length()); } else if (dbName.equals("Apache Derby")) { // Derby uses java.sql.Timestamp not java.util.Date // Derby uses username as schema name - assertEquals(1593, models.get(0).length()); + assertEquals(1601, models.get(0).length()); } else if (dbName.equals("PostgreSQL")) { - assertEquals(1635, models.get(0).length()); + assertEquals(1643, models.get(0).length()); } else if (dbName.equals("MySQL")) { // MySQL uses timestamp default values like // 0000-00-00 00:00:00 and CURRENT_TIMESTAMP - assertEquals(1665, models.get(0).length()); + assertEquals(1673, models.get(0).length()); } else { // unknown database assertEquals(0, models.get(0).length()); -- 2.39.5