Author: tchemit Date: 2011-03-17 15:06:40 +0100 (Thu, 17 Mar 2011) New Revision: 2230 Url: http://nuiton.org/repositories/revision/topia/2230 Log: improve migration code (use a unique db connection for dbempty code) Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java 2011-03-17 13:33:29 UTC (rev 2229) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java 2011-03-17 14:06:40 UTC (rev 2230) @@ -222,4 +222,72 @@ return exist; } + + /** + * Test if the db associated to the given {@code configuration} contaisn any of + * the dealed entities. + * + * @param configuration hibernate db configuration + * @return {@code true} if there is no schema for any of the dealed entities, + * {@code false} otherwise. + * @since 2.5.3 + */ + public static boolean isSchemaEmpty(Configuration configuration) { + + try { + + ConnectionProvider connectionProvider = + ConnectionProviderFactory.newConnectionProvider( + configuration.getProperties()); + + Dialect dialect = Dialect.getDialect(configuration.getProperties()); + + Connection connection = null; + try { + connection = connectionProvider.getConnection(); + + DatabaseMetadata meta = new DatabaseMetadata(connection, dialect); + + Iterator<?> itr = configuration.getClassMappings(); + while (itr.hasNext()) { + PersistentClass classMapping = (PersistentClass) itr.next(); + Table testTable = classMapping.getTable(); + + if (testTable == null) { + throw new IllegalArgumentException( + "could not find entity with name " + classMapping.getClassName()); + } + + + TableMetadata tmd = meta.getTableMetadata( + testTable.getName(), testTable.getSchema(), + testTable.getCatalog(), testTable.isQuoted()); + + if (tmd != null) { + //table exist + + + if (log.isDebugEnabled()) { + log.debug("Existing table found " + + testTable.getName() + " for entity " + + classMapping.getClassName() + + ", db is not empty."); + } + + return false; + } + } + + } finally { + if (connection != null) { + connection.close(); + } + } + + } catch (SQLException e) { + log.error("Cant connect to database", e); + } + + return true; + } } Modified: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java =================================================================== --- trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java 2011-03-17 13:33:29 UTC (rev 2229) +++ trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java 2011-03-17 14:06:40 UTC (rev 2230) @@ -46,7 +46,6 @@ import org.nuiton.util.VersionUtil; import org.nuiton.util.VersionUtil.VersionComparator; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Properties; @@ -92,7 +91,7 @@ /** * A flag to know if none of the dealed entities tables exists in db. - * + * * @since 2.5.3 */ protected boolean dbEmpty; @@ -312,11 +311,11 @@ } if (dbEmpty) { - + // db is empty (no table, no migration to apply) return true; } - + if (versionTableExist && dbVersion.equals(version)) { if (log.isInfoEnabled()) { log.info(_("topia.migration.skip.migration.db.is.up.to.date")); @@ -605,42 +604,19 @@ */ protected boolean detectDbEmpty() { - boolean result = true; - + try { + boolean result; // get db real hibernate configuration Configuration rootConfiguration = rootContext.getHibernateConfiguration(); - // get all dealed classes - List<Class<?>> persistenceClasses = - new ArrayList<Class<?>>(rootContext.getPersistenceClasses()); - - // remove the tmsversion class - persistenceClasses.remove(TMSVersion.class); - - for (Class<?> persistenceClass : persistenceClasses) { - - boolean tableExist = - TopiaUtil.isSchemaExist(rootConfiguration, - persistenceClass.getName() - ); - if (tableExist) { - - // at least one table exists - result = false; - - if (log.isDebugEnabled()) { - log.debug("Detected table for " + persistenceClass); - } - break; - } - } + result = TopiaUtil.isSchemaEmpty(rootConfiguration); + return result; } catch (TopiaNotFoundException e) { throw new RuntimeException(e); } - return result; } protected Version getVersion(boolean versionTableExist, String tableName) {