This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository topia. See http://git.nuiton.org/topia.git commit 1d6fdebc6463ecfb87e238f95fac4f4386de4473 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Mar 22 15:35:29 2015 +0100 fixes #3661: Save *tms_version* after each successfull version migrated --- .../migration/AbstractTopiaMigrationCallback.java | 9 +- .../topia/migration/TMSVersionPersister.java | 166 +++++++++++++++++++++ .../topia/migration/TopiaMigrationEngine.java | 132 +++++----------- 3 files changed, 213 insertions(+), 94 deletions(-) diff --git a/topia-service-migration/src/main/java/org/nuiton/topia/migration/AbstractTopiaMigrationCallback.java b/topia-service-migration/src/main/java/org/nuiton/topia/migration/AbstractTopiaMigrationCallback.java index 3e788ec..a96c12a 100644 --- a/topia-service-migration/src/main/java/org/nuiton/topia/migration/AbstractTopiaMigrationCallback.java +++ b/topia-service-migration/src/main/java/org/nuiton/topia/migration/AbstractTopiaMigrationCallback.java @@ -51,6 +51,8 @@ public abstract class AbstractTopiaMigrationCallback { /** Logger */ static private Log log = LogFactory.getLog(AbstractTopiaMigrationCallback.class); + private TMSVersionPersister tmsVersionPersister; + /** @return the available versions from the call back */ public abstract Version[] getAvailableVersions(); @@ -118,7 +120,8 @@ public abstract class AbstractTopiaMigrationCallback { migrateForVersion(v, tx, showSql, showProgression); - // commit des modifs + tmsVersionPersister.saveVersion(tx, v); + tx.commitTransaction(); } catch (Exception eee) { @@ -224,4 +227,8 @@ public abstract class AbstractTopiaMigrationCallback { } + public void setTmsVersionPersister(TMSVersionPersister tmsVersionPersister) { + this.tmsVersionPersister = tmsVersionPersister; + } + } diff --git a/topia-service-migration/src/main/java/org/nuiton/topia/migration/TMSVersionPersister.java b/topia-service-migration/src/main/java/org/nuiton/topia/migration/TMSVersionPersister.java new file mode 100644 index 0000000..dd273d2 --- /dev/null +++ b/topia-service-migration/src/main/java/org/nuiton/topia/migration/TMSVersionPersister.java @@ -0,0 +1,166 @@ +package org.nuiton.topia.migration; + +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.cfg.Configuration; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaUtil; +import org.nuiton.topia.migration.mappings.TMSVersion; +import org.nuiton.topia.migration.mappings.TMSVersionDAO; +import org.nuiton.util.version.Version; +import org.nuiton.util.version.VersionBuilder; + +import static org.nuiton.i18n.I18n.t; + +/** + * To persiste the version inside the {@link TMSVersion}. + * + * Created on 3/22/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 2.9.4 + */ +public class TMSVersionPersister { + + /** Logger. */ + private static final Log log = LogFactory.getLog(TMSVersionPersister.class); + + /** Configuration hibernate ne mappant que l'entite version. */ + protected final Configuration versionConfiguration; + + /** Configuration hibernate ne mappant que l'entite version de l'ancien systeme de migration. */ + protected final Configuration legacyVersionConfiguration; + + /** Un drapeau pour savoir si la table version existe en base. */ + protected Boolean versionTableExist; + + /** Un drapeau pour savoir si la table version (de l'ancien service Manual) existe en base. */ + protected Boolean legacyVersionTableExist; + + public TMSVersionPersister(Configuration versionConfiguration, Configuration legacyVersionConfiguration) { + this.versionConfiguration = versionConfiguration; + this.legacyVersionConfiguration = legacyVersionConfiguration; + } + + public boolean isLegacyVersionTableExist() { + + if (legacyVersionTableExist == null) { + + legacyVersionTableExist = TopiaUtil.isSchemaExist(legacyVersionConfiguration, TMSVersion.class.getName()); + + } + + return legacyVersionTableExist; + + } + + public boolean isVersionTableExist() { + + if (versionTableExist == null) { + + versionTableExist = TopiaUtil.isSchemaExist(versionConfiguration, TMSVersion.class.getName()); + + } + + return versionTableExist; + + } + + public void saveVersion(TopiaContext tx, Version version) throws TopiaException { + + if (log.isInfoEnabled()) { + log.info("Table exists = " + isVersionTableExist()); + log.info("Legacy table exists = " + isLegacyVersionTableExist()); + } + + if (isLegacyVersionTableExist()) { + deleteLegacyTable(); + } + + Version dbVersion; + + if (isVersionTableExist()) { + + dbVersion = getVersion(tx); + + } else { + + dbVersion = null; + createTableIfRequired(); + + } + + if (log.isInfoEnabled()) { + log.info("Db version = " + dbVersion); + log.info("Version to save = " + version); + } + + if (ObjectUtils.notEqual(dbVersion, version)) { + + persistVersion(tx, version); + + } + + } + + protected Version getVersion(TopiaContext tx) throws TopiaException { + + // delete all previous data in table + TMSVersion tmsVersion = TMSVersionDAO.get(tx); + Version version = VersionBuilder.create(tmsVersion.getVersion()).build(); + return version; + + } + + protected void persistVersion(TopiaContext tx, Version version) throws TopiaException { + + // delete all previous data in table + TMSVersionDAO.deleteAll(tx); + + if (log.isInfoEnabled()) { + log.info(t("topia.migration.saving.db.version", version)); + } + + // create new version and store it in table + TMSVersion tmsVersion = TMSVersionDAO.create(tx, version.getVersion()); + if (log.isDebugEnabled()) { + log.debug("Created version : " + tmsVersion.getVersion()); + } + + } + + protected void createTableIfRequired() { + + // si la base n'etait pas versionnee, la table version n'existe pas + // creation + if (log.isDebugEnabled()) { + log.debug("Adding tms_version table"); + } + + // creer le schema en base + // dans la configuration versionConfiguration, il n'y a que la table version + TMSVersionDAO.createTable(versionConfiguration); + + if (log.isDebugEnabled()) { + log.debug("Table for " + TMSVersion.class.getSimpleName() + " created"); + } + + versionTableExist = true; + + } + + protected void deleteLegacyTable() { + + if (log.isDebugEnabled()) { + log.debug("Will drop legacy tmsVersion table"); + } + // on supprime l'ancienne table + TMSVersionDAO.dropTable(legacyVersionConfiguration); + + legacyVersionTableExist = false; + + } + +} diff --git a/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java b/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java index d2f69ee..2bba85d 100644 --- a/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java +++ b/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java @@ -70,17 +70,20 @@ public class TopiaMigrationEngine implements TopiaMigrationService { /** logger */ private final static Log log = LogFactory.getLog(TopiaMigrationEngine.class); - /** Configuration hibernate ne mappant que l'entite version (initialise en pre-init) */ - protected Configuration versionConfiguration; - - /** Un drapeau pour savoir si la table version existe en base (initialise en pre-init) */ - protected boolean versionTableExist; - - /** Configuration hibernate ne mappant que l'entite version de l'ancien systeme de migration (initialise en pre-init) */ - protected Configuration legacyVersionConfiguration; - - /** Un drapeau pour savoir si la table version (de l'ancien service Manual) existe en base (initialise en pre-init) */ - protected boolean legacyVersionTableExist; +// /** Configuration hibernate ne mappant que l'entite version (initialise en pre-init) */ +// protected Configuration versionConfiguration; +// +// /** Un drapeau pour savoir si la table version existe en base (initialise en pre-init) */ +// protected boolean versionTableExist; +// +// /** Configuration hibernate ne mappant que l'entite version de l'ancien systeme de migration (initialise en pre-init) */ +// protected Configuration legacyVersionConfiguration; +// +// /** Un drapeau pour savoir si la table version (de l'ancien service Manual) existe en base (initialise en pre-init) */ +// protected boolean legacyVersionTableExist; + + /** Pour enregistrer les version en base (initialise en pre-init) */ + protected TMSVersionPersister tmsVersionPersister; /** Version courante de la base (initialise en pre-init) */ protected Version dbVersion; @@ -235,7 +238,14 @@ public class TopiaMigrationEngine implements TopiaMigrationService { configuration.addClass(aClass); } - versionConfiguration = createHibernateConfiguration(configuration); + Configuration versionConfiguration = createHibernateConfiguration(configuration); + + Configuration conf = new Configuration(); + conf.addXML(TMSVersionDAO.LEGACY_MAPPING); + + Configuration legacyVersionConfiguration = createHibernateConfiguration(conf); + + tmsVersionPersister = new TMSVersionPersister(versionConfiguration, legacyVersionConfiguration); init = true; @@ -302,11 +312,16 @@ public class TopiaMigrationEngine implements TopiaMigrationService { dbVersion.getVersion()) ); + boolean versionTableExist = tmsVersionPersister.isVersionTableExist(); + boolean legacyVersionTableExist = tmsVersionPersister.isLegacyVersionTableExist(); + if (log.isDebugEnabled()) { - log.debug("Migrate schema to version = " + dbVersion); log.debug("is db not versionned ? = " + dbNotVersioned); log.debug("is db empty ? = " + dbEmpty); - log.debug("TMSVersion exists = " + versionTableExist); + log.debug("TMSVersion exists ? = " + versionTableExist); + log.debug("LegacyTMSVersion exists ? = " + legacyVersionTableExist); + log.debug("Update from version = " + dbVersion); + log.debug("Update to version = " + version); } if (dbEmpty) { @@ -371,6 +386,8 @@ public class TopiaMigrationEngine implements TopiaMigrationService { log.info(t("topia.migration.migrate.versions", versionsToApply)); } + callback.setTmsVersionPersister(tmsVersionPersister); + // perform the migration needToMigrate = callback.doMigration(rootContext, dbVersion, @@ -419,89 +436,31 @@ public class TopiaMigrationEngine implements TopiaMigrationService { Version version = callback.getApplicationVersion(); - detectDbVersion(); - if (log.isDebugEnabled()) { - log.debug("Save version = " + version); - log.debug("Table exists = " + versionTableExist); - log.debug("Detected version = " + dbVersion); + log.debug("Save application version = " + version); } try { - - boolean createTable = !versionTableExist; - // update version even if database has not been migrated - // only case that database doesn't exist match this - if (createTable) { - // si la base n'etait pas versionnee, la table version n'existe pas - // creation - if (log.isDebugEnabled()) { - log.debug("Adding tms_version table"); - } - - // creer le schema en base - // dans la configuration versionConfiguration, il n'y a que la table version - TMSVersionDAO.createTable(versionConfiguration); - - if (log.isDebugEnabled()) { - log.debug("Table for " + TMSVersion.class.getSimpleName() + " created"); - } - } - - // Set new version in database TopiaContext tx = rootContext.beginTransaction(); try { - - // delete all previous data in table - TMSVersionDAO.deleteAll(tx); - - if (log.isInfoEnabled()) { - log.info(t("topia.migration.saving.db.version", version)); - } - - // create new version and store it in table - TMSVersion tmsVersion = - TMSVersionDAO.create(tx, version.getVersion()); - if (log.isDebugEnabled()) { - log.debug("Created version : " + tmsVersion.getVersion()); - } - - tx.commitTransaction(); - } catch (TopiaException e) { - if (tx != null) { - tx.rollbackTransaction(); - } - throw e; + tmsVersionPersister.saveVersion(tx, version); } finally { if (tx != null) { tx.closeContext(); } } - - if (legacyVersionTableExist) { - - if (log.isDebugEnabled()) { - log.debug("Will drop legacy tmsVersion table"); - } - // on supprime l'ancienne table - TMSVersionDAO.dropTable(legacyVersionConfiguration); - } } catch (TopiaException e) { - throw new TopiaRuntimeException(e); + throw new TopiaRuntimeException("Can't save application version for reason " + e.getMessage(), e); } - // on change les etats internes du service - // ainsi cela empechera le redeclanchement de la migration - // suite a une creation de schema - versionTableExist = true; dbVersion = version; + } /** * Recupere depuis la base les états internes du service : * <p/> * <ul> - * <li>{@link #versionTableExist}</li> * <li>{@link #dbVersion}</li> * <li>{@link #dbEmpty}</li> * </ul> @@ -528,13 +487,7 @@ public class TopiaMigrationEngine implements TopiaMigrationService { Version v = null; try { - // on detecte si la table de versionning existe - versionTableExist = - TopiaUtil.isSchemaExist(versionConfiguration, - TMSVersion.class.getName()); - - // check if at least one class exists in db - + boolean versionTableExist = tmsVersionPersister.isVersionTableExist(); if (log.isDebugEnabled()) { log.debug("Table " + TMSVersionDAO.TABLE_NAME + " exist = " + versionTableExist); @@ -543,7 +496,7 @@ public class TopiaMigrationEngine implements TopiaMigrationService { if (versionTableExist) { // recuperation de la version de la base - v = getVersion(versionTableExist, TMSVersionDAO.TABLE_NAME); + v = getVersion(true, TMSVersionDAO.TABLE_NAME); if (log.isWarnEnabled()) { if (v == null) { @@ -553,14 +506,7 @@ public class TopiaMigrationEngine implements TopiaMigrationService { return; } - // try with legacy table tmsVersion - Configuration conf = new Configuration(); - conf.addXML(TMSVersionDAO.LEGACY_MAPPING); - - legacyVersionConfiguration = createHibernateConfiguration(conf); - legacyVersionTableExist = - TopiaUtil.isSchemaExist(legacyVersionConfiguration, - TMSVersion.class.getName()); + boolean legacyVersionTableExist = tmsVersionPersister.isLegacyVersionTableExist(); if (legacyVersionTableExist) { @@ -569,7 +515,7 @@ public class TopiaMigrationEngine implements TopiaMigrationService { } // recuperation de la version de la base - v = getVersion(legacyVersionTableExist, TMSVersionDAO.LEGACY_TABLE_NAME); + v = getVersion(true, TMSVersionDAO.LEGACY_TABLE_NAME); if (v != null) { -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.