From: Patrick Schmitz Date: Thu, 23 Jun 2011 23:50:06 +0000 (+0000) Subject: CSPACE-4127 Indexes were not correctly being added to the DB. Also fixed bug with... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=37589a384b435e63a51e12b46652aeb0a4c4b2df;p=tmp%2Fjakarta-migration.git CSPACE-4127 Indexes were not correctly being added to the DB. Also fixed bug with code that modifies field types. --- diff --git a/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml b/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml index 035f41361..ba019d47b 100644 --- a/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml +++ b/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml @@ -844,11 +844,11 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.vocabularies_common + vocabularies_common displayname - nuxeo.vocabularies_common + vocabularies_common shortidentifier @@ -894,15 +894,15 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.vocabularyitems_common + vocabularyitems_common inauthority - nuxeo.vocabularyitems_common + vocabularyitems_common displayname - nuxeo.vocabularyitems_common + vocabularyitems_common shortidentifier @@ -941,11 +941,11 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.orgauthorities_common + orgauthorities_common displayname - nuxeo.orgauthorities_common + orgauthorities_common shortidentifier @@ -988,15 +988,15 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.organizations_common + organizations_common inauthority - nuxeo.organizations_common + organizations_common displayname - nuxeo.organizations_common + organizations_common shortidentifier @@ -1048,11 +1048,11 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.personauthorities_common + personauthorities_common displayname - nuxeo.personauthorities_common + personauthorities_common shortidentifier @@ -1092,15 +1092,15 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.persons_common + persons_common inauthority - nuxeo.persons_common + persons_common displayname - nuxeo.persons_common + persons_common shortidentifier @@ -1149,11 +1149,11 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.locationauthorities_common + locationauthorities_common displayname - nuxeo.locationauthorities_common + locationauthorities_common shortidentifier @@ -1189,15 +1189,15 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.locations_common + locations_common inauthority - nuxeo.locations_common + locations_common displayname - nuxeo.locations_common + locations_common shortidentifier @@ -1245,11 +1245,11 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.taxonomyauthority_common + taxonomyauthority_common displayname - nuxeo.taxonomyauthority_common + taxonomyauthority_common shortidentifier @@ -1285,15 +1285,15 @@ org.collectionspace.services.common.init.AddIndices - nuxeo.taxon_common + taxon_common inauthority - nuxeo.taxon_common + taxon_common displayname - nuxeo.taxon_common + taxon_common shortidentifier @@ -1413,6 +1413,15 @@ predicateDisplayName + + org.collectionspace.services.common.init.AddIndices + + + relations_common + documentid1,documentid2 + + + diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java b/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java index 59a78daf8..5735c9aea 100755 --- a/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java @@ -79,6 +79,7 @@ public class AddIndices extends InitHandler implements IInitHandler { final Logger logger = LoggerFactory.getLogger(AddIndices.class); private final static String INDEX_SUFFIX = "_idx"; + private final static String INDEX_SEP = "_"; /** See the class javadoc for this class: it shows the syntax supported in the configuration params. */ @@ -108,10 +109,10 @@ public class AddIndices extends InitHandler implements IInitHandler { private int addOneIndex(String tableName, String columnName){ int rows = 0; String sql = ""; - String indexName = columnName + INDEX_SUFFIX; + String indexName = tableName + INDEX_SEP + columnName + INDEX_SUFFIX; try { DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType(); - if (indexExists(databaseProductType, tableName, indexName)) { + if (indexExists(databaseProductType, tableName, columnName, indexName)) { logger.trace("Index already exists for column " + columnName + " in table " + tableName); // FIXME: Can add the option to drop and re-create an index here. @@ -121,12 +122,11 @@ public class AddIndices extends InitHandler implements IInitHandler { // TODO: Consider refactoring this 'if' statement to a general-purpose // mechanism for retrieving and populating catalog/DDL-type SQL statements // appropriate to a particular database product. - if (databaseProductType == DatabaseProductType.MYSQL) { - logger.info("Creating index for column " + columnName + " in table " + tableName); - sql = "CREATE INDEX " + indexName + " ON " + tableName + " (" + columnName + ")"; - } else if (databaseProductType == DatabaseProductType.POSTGRESQL) { - logger.info("Creating index for column " + columnName + " in table " + tableName); - sql = "CREATE INDEX ON " + tableName + " (" + columnName + ")"; + logger.info("Creating index for column " + columnName + " in table " + tableName); + if (databaseProductType == DatabaseProductType.MYSQL + || databaseProductType == DatabaseProductType.POSTGRESQL) { + sql = "CREATE INDEX " + indexName + " ON " + + tableName + " (" + columnName + ")"; } else { throw new Exception("Unrecognized database system " + databaseProductType); } @@ -151,7 +151,7 @@ public class AddIndices extends InitHandler implements IInitHandler { } private boolean indexExists(DatabaseProductType databaseProductType, - String tableName, String indexName) { + String tableName, String colName, String indexName) { // FIXME: May need to qualify table name by database/catalog, // as table names likely will not be globally unique across same @@ -169,7 +169,6 @@ public class AddIndices extends InitHandler implements IInitHandler { // java.sql.DatabaseMetaData.getIndexInfo() boolean indexExists = false; - int rows = 0; String sql = ""; Connection conn = null; Statement stmt = null; @@ -178,9 +177,10 @@ public class AddIndices extends InitHandler implements IInitHandler { if (databaseProductType == DatabaseProductType.MYSQL) { sql = "SHOW INDEX FROM " + tableName + " WHERE key_name='" + indexName + "'"; } else if (databaseProductType == DatabaseProductType.POSTGRESQL) { + // We want to see if any index on that column exists, not just ours... sql = "SELECT indexname FROM pg_catalog.pg_indexes " - + "WHERE indexname = '" + indexName + "'" - + " AND tablename = '" + tableName + "'"; + + "WHERE tablename = '" + tableName + + "' AND indexdef ILIKE '%("+colName+")'"; } try { @@ -196,15 +196,12 @@ public class AddIndices extends InitHandler implements IInitHandler { conn = JDBCTools.getConnection(JDBCTools.NUXEO_REPOSITORY_NAME); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); - if (rs.last()) { - rows = rs.getRow(); + if (rs.next()) { + indexExists = true; } rs.close(); stmt.close(); conn.close(); - if (rows > 0) { - indexExists = true; - } } catch (Exception e) { logger.debug("Error when identifying whether index exists in table " + tableName + ":" + e.getMessage()); diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java b/services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java index 701cacd97..fa68210bd 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java @@ -202,7 +202,7 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler { private String getTableName(Field field) { String tableName = ""; String[] databaseAndTableNames = field.getTable().split("\\.", 2); - if (! databaseAndTableNames[1].isEmpty()) { + if (databaseAndTableNames.length>1 && !databaseAndTableNames[1].isEmpty()) { tableName = databaseAndTableNames[1]; } else { tableName = field.getTable();