From: Aron Roberts Date: Thu, 22 Mar 2012 23:38:06 +0000 (-0700) Subject: CSPACE-4633: When changing field (column) datatypes in the database during services... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=c7f581d11621b300b3a89b4d76b0c1cc410eca22;p=tmp%2Fjakarta-migration.git CSPACE-4633: When changing field (column) datatypes in the database during services post-init phase, get database name from JDBC URL used in current connection, rather than from configuration in tenant bindings, where it is no longer present. --- 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 5ab9040cb..328cadfa2 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 @@ -142,21 +142,20 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler { Statement stmt = null; ResultSet rs = null; - if (databaseProductType == DatabaseProductType.MYSQL) { - sql = "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS " - + "WHERE TABLE_SCHEMA = '" + getDatabaseName(field) + "'" + try { + conn = JDBCTools.getConnection(dataSource); + stmt = conn.createStatement(); + if (databaseProductType == DatabaseProductType.MYSQL) { + sql = "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS " + + "WHERE TABLE_SCHEMA = '" + JDBCTools.getDatabaseName(conn) + "'" + " AND TABLE_NAME = '" + getTableName(field) + "'" + " AND COLUMN_NAME = '" + field.getCol() + "'"; - } else if (databaseProductType == DatabaseProductType.POSTGRESQL) { - sql = "SELECT data_type FROM information_schema.columns " - + "WHERE table_catalog = '" + getDatabaseName(field) + "'" + } else if (databaseProductType == DatabaseProductType.POSTGRESQL) { + sql = "SELECT data_type FROM information_schema.columns " + + "WHERE table_catalog = '" + JDBCTools.getDatabaseName(conn) + "'" + " AND table_name = '" + getTableName(field) + "'" + " AND column_name = '" + field.getCol() + "'"; - } - - try { - conn = JDBCTools.getConnection(dataSource); - stmt = conn.createStatement(); + } rs = stmt.executeQuery(sql); while (rs.next()) { currentDatatype = rs.getString(1); diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java index bf9e5310c..ab5909642 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java @@ -52,6 +52,7 @@ public class JDBCTools { //todo: make sure this will get instantiated in the right order final static Logger logger = LoggerFactory.getLogger(JDBCTools.class); + private static String JDBC_URL_DATABASE_SEPARATOR = "\\/"; public static DataSource getDataSource(String repositoryName) throws NamingException { DataSource result = null; @@ -253,6 +254,36 @@ public class JDBCTools { private static String getDefaultRepositoryName() { return DEFAULT_REPOSITORY_NAME; } + + /** + * Returns the catalog name for an open JDBC connection. + * + * @param conn an open JDBC Connection + * @return the catalog name. + * @throws SQLException + */ + public static String getDatabaseName(Connection conn) throws Exception { + String databaseName = ""; + if (conn == null) { + return databaseName; + } + DatabaseMetaData metadata = conn.getMetaData(); + String urlStr = metadata.getURL(); + + // Format of the PostgreSQL JDBC URL: + // http://jdbc.postgresql.org/documentation/80/connect.html + if (getDatabaseProductType() == DatabaseProductType.POSTGRESQL) { + String tokens[] = urlStr.split(JDBC_URL_DATABASE_SEPARATOR); + databaseName = tokens[tokens.length - 1]; + // Format of the MySQL JDBC URL: + // http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html + // FIXME: the last token could contain optional parameters, not accounted for here. + } else if (getDatabaseProductType() == DatabaseProductType.MYSQL) { + String tokens[] = urlStr.split(JDBC_URL_DATABASE_SEPARATOR); + databaseName = tokens[tokens.length - 1]; + } + return databaseName; + } /** * Prints metadata, such as database username and connection URL,