Index: topia-service/src/java/org/codelutin/topia/migration/DatabaseManager.java
diff -u topia-service/src/java/org/codelutin/topia/migration/DatabaseManager.java:1.2 topia-service/src/java/org/codelutin/topia/migration/DatabaseManager.java:1.3
--- topia-service/src/java/org/codelutin/topia/migration/DatabaseManager.java:1.2 Fri Nov 2 15:20:51 2007
+++ topia-service/src/java/org/codelutin/topia/migration/DatabaseManager.java Fri Nov 9 16:35:45 2007
@@ -33,7 +33,10 @@
import org.hibernate.cfg.Environment;
import org.hibernate.exception.JDBCConnectionException;
import org.hibernate.exception.SQLGrammarException;
+import org.hibernate.mapping.ForeignKey;
+import org.hibernate.mapping.Index;
import org.hibernate.mapping.Table;
+import org.hibernate.mapping.UniqueKey;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
@@ -46,9 +49,9 @@
* @author Chevallereau Benjamin
* @author Eon Sébastien
* @author Trève Vincent
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*
- * Last update : $Date: 2007-11-02 15:20:51 $
+ * Last update : $Date: 2007-11-09 16:35:45 $
*/
public class DatabaseManager {
@@ -71,7 +74,7 @@
/**
* Suffix du nom des tables : _tms_v0 par exemple
*/
- private static String VERSIONNED_TABLES_SUFFIX = "_tms_v";
+ private static String VERSIONNED_TABLES_SUFFIX = "_tmsv";
/**
* Constructeur
@@ -103,7 +106,6 @@
* Retourne la version de la base
* @return la version present en base, ou null si la version ne peut pas etre determinee
*/
- @SuppressWarnings("unchecked")
public Version getDataBaseVersion() throws TopiaMigrationServiceException {
Version version = null;
@@ -117,9 +119,9 @@
session.beginTransaction();
// execute query
- List result = (List)session.createCriteria(TMSVersion.class).list();
- if(result.size() == 1) {
- version = new Version(result.get(0).getVersion().toString());
+ TMSVersion result = (TMSVersion)session.createCriteria(TMSVersion.class).uniqueResult();
+ if(result != null) {
+ version = new Version(result.getVersion().toString());
logger.debug("Query executed, version found : " + version.getVersion());
}
else {
@@ -144,7 +146,8 @@
}
/**
- * Renomme les tables
+ * Renomme les tables en supprimant le suffixe
+ *
* @param vdbVersion Version a ajouter
* @param oldConfiguration Configuration contenant le schema
*/
@@ -165,13 +168,20 @@
Table table = i.next();
String tableName = table.getName();
- String newTableName = tableName + VERSIONNED_TABLES_SUFFIX + vdbVersion.getValidName();
+ String suffix = getTableSuffixForVersion(vdbVersion);
+ String newTableName = tableName;
+ if(tableName.endsWith(suffix)) {
+ newTableName = tableName.substring(0,tableName.length() - suffix.length());
+ }
// ALTER TABLE name RENAME TO newName
// marche normalement pour mysql, h2, postgres
// TODO table existe pas
// TODO autre table existe deja
+ // TODO get sql string from dialect ...
+
+ //Dialect dialect = Dialect.getDialect(oldConfiguration.getProperties());
logger.debug("Renaming table " + tableName + " to " + newTableName);
String sQuery = "ALTER TABLE " + tableName + " RENAME TO " + newTableName;
@@ -189,44 +199,57 @@
}
/**
+ * Renome les table dans la configuration hibernate
*
* @param oldConfiguration
* @param vdbVersion
*/
- public void setCurrentOldRenamedMappingSchemaInDatabase(Configuration oldConfiguration, Version vdbVersion) {
+ public Configuration setRenamedTableSchema(Configuration oldConfiguration, Version vdbVersion) {
logger.debug("Renaming tables in configuration for version " + vdbVersion.getVersion());
// voir si les relation *<->* sont listees par l'iterateur
// -> ok, normalement c bon, c gere
+
+ // bug: sans cette ligne, les tables many-to-many ne sont pas listées
+ oldConfiguration.buildMappings();
+
Iterator i = oldConfiguration.getTableMappings();
while(i.hasNext()) {
Table table = (Table)i.next();
-
- String tableName = table.getName();
- String newTableName = tableName + VERSIONNED_TABLES_SUFFIX + vdbVersion.getValidName();
-
- // rennomage dans la config
- logger.debug("Renaming table " + tableName + " to " + newTableName);
- table.setName(newTableName);
+
+ if(table.isPhysicalTable()) { // hibernate utilse ca
+
+ // rename constraints FK
+ Iterator fKeys = table.getForeignKeyIterator();
+ while(fKeys.hasNext()) {
+ ForeignKey fKey = fKeys.next();
+
+ logger.debug("Changing constraints name : " + fKey.getName() + " to " + fKey.getName() + vdbVersion.getValidName());
+ fKey.setName(fKey.getName() + vdbVersion.getValidName());
+ }
+
+ String tableName = table.getName();
+ String newTableName = tableName + getTableSuffixForVersion(vdbVersion);
+
+ // rennomage dans la config
+ logger.debug("Renaming table " + tableName + " to " + newTableName + " " + table.hasDenormalizedTables());
+ table.setName(newTableName);
+ }
}
- // log
- logger.debug("Creating schema for version " + vdbVersion.getVersion());
- createSchema(oldConfiguration);
- // log
- logger.debug("Schema created");
+ return oldConfiguration;
}
/**
* Creer le nouveau schema pour l'application
* @param newConfiguration la configuration contenant le nouveau schema
*/
- public void setCurrentApplicationSchemaInDatabase(Configuration newConfiguration) {
+ public void setApplicationSchemaInDatabase(Configuration newConfiguration) {
// log
logger.debug("Creating new schema");
-
+
createSchema(newConfiguration);
// log
@@ -235,15 +258,25 @@
/**
* Creer un schema
- * @param configuration la configuration contenant lae schema
+ * @param configuration la configuration contenant les schemas
*/
- private void createSchema(Configuration configuration) {
+ protected void createSchema(Configuration configuration) {
// creer le schema en base
SchemaExport schemaExport = new SchemaExport(configuration);
- schemaExport.create(false, true);
+ schemaExport.execute(false/*script*/, true/*export*/, false/*justDrop*/, true/*justCreate*/);
}
/**
+ * Supprimer un schema
+ * @param configuration la configuration contenant les schemas
+ */
+ protected void dropSchema(Configuration configuration) {
+ // supprimer le schema en base
+ SchemaExport schemaExport = new SchemaExport(configuration);
+ schemaExport.drop(true, true);
+ }
+
+ /**
* Creer le schema pour la table "tms_version"
*/
public void createVersionTable() {
@@ -262,7 +295,6 @@
* Introduit la version du nouveau schema dans la base
* @throws TopiaMigrationServiceException
*/
- @SuppressWarnings("unchecked")
public void putVersionInDatabase(Version nouvelleVersion) {
// get session
@@ -305,7 +337,7 @@
*/
public void removeTablesFromOldMapping(Configuration oldConfiguration) {
- logger.debug("Removing tables from old mapping");
+ /*logger.debug("Removing tables from old mapping");
// get session
Session session = sessionFactory.getCurrentSession();
@@ -332,7 +364,12 @@
}
// commit
- session.getTransaction().commit();
+ session.getTransaction().commit();*/
+
+ if(logger.isDebugEnabled()) {
+ logger.debug("Removing schema");
+ }
+ dropSchema(oldConfiguration);
}
/**
@@ -343,4 +380,15 @@
sessionFactory.getCurrentSession().close();
sessionFactory.close();
}
+
+ /**
+ * Return table suffix name
+ *
+ * @param version version
+ * @return suffix name
+ */
+ protected String getTableSuffixForVersion(Version version) {
+ String suffix = VERSIONNED_TABLES_SUFFIX + version.getValidName();
+ return suffix;
+ }
}
Index: topia-service/src/java/org/codelutin/topia/migration/TopiaMigrationServiceImpl.java
diff -u topia-service/src/java/org/codelutin/topia/migration/TopiaMigrationServiceImpl.java:1.6 topia-service/src/java/org/codelutin/topia/migration/TopiaMigrationServiceImpl.java:1.7
--- topia-service/src/java/org/codelutin/topia/migration/TopiaMigrationServiceImpl.java:1.6 Thu Apr 26 15:19:23 2007
+++ topia-service/src/java/org/codelutin/topia/migration/TopiaMigrationServiceImpl.java Fri Nov 9 16:35:45 2007
@@ -49,9 +49,9 @@
* @author Chevallereau Benjamin
* @author Eon Sébastien
* @author Trève Vincent
- * @version $Revision: 1.6 $
+ * @version $Revision: 1.7 $
*
- * Last update : $Date: 2007-04-26 15:19:23 $
+ * Last update : $Date: 2007-11-09 16:35:45 $
*/
public class TopiaMigrationServiceImpl implements TopiaMigrationService {
@@ -67,7 +67,6 @@
*/
static final private String TOPIA_PERSISTENCE_DIRECTORIES = "topia.persistence.directories";
static final private String TOPIA_PERSISTENCE_CLASSES = "topia.persistence.classes";
- //static final private String TOPIA_PERSISTENCE_PROPERTIES_FILE = "topia.persistence.properties.file";
/**
* Nom courant du fichier de configuration.
@@ -353,8 +352,9 @@
// les configurations sont chargees
// on doit :
- // - pour la version vdbVersion, renommer les tables deja en base
+ // - pour la version vdbVersion, on utilise les tables deja en base
// - pour les autres, creer les tables (suffixees avec la version)
+ // - creation du schema courant
logger.debug("Set old database from old mappings");
@@ -368,13 +368,12 @@
// on les met ici
cConfiguration.setProperties(this.currentApplicationConfiguration.getProperties());
- // renommage
- if(vVersion.equals(vdbVersion)) {
- // renomme les anciennes tables
- dbManager.renameTables(cConfiguration,vVersion);
- }
- else {
- dbManager.setCurrentOldRenamedMappingSchemaInDatabase(cConfiguration,vVersion);
+ // renommage des table
+ // et creation des schema intermediaires
+ if(!vVersion.equals(vdbVersion)) {
+ cConfiguration = dbManager.setRenamedTableSchema(cConfiguration,vVersion);
+ logger.debug("Creating schema for version : " + vVersion.getVersion());
+ dbManager.setApplicationSchemaInDatabase(cConfiguration);
}
// on construit les ConfigurationAdpater
@@ -382,17 +381,21 @@
smVersionAndConfigurationAdapterMap.put(vVersion, cfgAdpater);
}
- logger.debug("Creating new schema");
-
// enfin, il reste la configuration de l'application
// on va instancier le nouveau schema (le creer)
- dbManager.setCurrentApplicationSchemaInDatabase(this.currentApplicationConfiguration);
+
+ // on renomme le nom des table d'abord
+ this.currentApplicationConfiguration = dbManager.setRenamedTableSchema(this.currentApplicationConfiguration,this.currentApplicationVersion);
+
+ logger.debug("Creating current application schema");
+ dbManager.setApplicationSchemaInDatabase(this.currentApplicationConfiguration);
ConfigurationAdapter appCfgAdpater = new ConfigurationAdapter(this.currentApplicationConfiguration,this.currentApplicationVersion);
smVersionAndConfigurationAdapterMap.put(this.currentApplicationVersion, appCfgAdpater);
logger.info("Data migration");
- // Ici, on a l'ancien schema renommé
+
+ // Ici, on a l'ancien schema deja present en base
// les schemas intermediaires creer et vides
// et le nouveau schema cree et vide
// on doit maintenant migrer les donnees
@@ -418,6 +421,9 @@
dbManager.removeTablesFromOldMapping(cfg);
}
+ // renommage correct du schema courant
+ dbManager.renameTables(this.currentApplicationConfiguration, this.currentApplicationVersion);
+
// il faudrait ici valider les transactions et fermer les sessions
// vmvManager a sa propre gestion des transactions/session
// this.remoteConfiguration doit en avoir ouverte