Author: vmassol
Date: 2008-01-29 12:20:47 +0100 (Tue, 29 Jan 2008)
New Revision: 7150

Modified:
   
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/store/migration/AbstractXWikiMigrationManager.java
   
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/store/migration/hibernate/R6079XWIKI1878Migrator.java
Log:
XWIKI-2044: Modify the database migrator that aligns RCS Diffs data so that it 
can be executed even after the migrator that removes RCS data from the document 
table has executed

* Made it work with all Databases (was only working with MySQL)
* Improved message logs
* Fixed migration forcing


Modified: 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/store/migration/AbstractXWikiMigrationManager.java
===================================================================
--- 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/store/migration/AbstractXWikiMigrationManager.java
       2008-01-29 10:47:34 UTC (rev 7149)
+++ 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/store/migration/AbstractXWikiMigrationManager.java
       2008-01-29 11:20:47 UTC (rev 7150)
@@ -200,7 +200,7 @@
                     continue;
                 }
                 if (migrator.getVersion().compareTo(curversion) >= 0) {
-                    XWikiMigration migration = new XWikiMigration(migrator, 
true);
+                    XWikiMigration migration = new XWikiMigration(migrator, 
false);
                     neededMigrations.put(migrator.getVersion(), migration);
                 }
             }
@@ -213,8 +213,10 @@
                 LOG.info("List of migrations that will be executed:");
                 for (Iterator it = neededMigrationsAsCollection.iterator(); 
it.hasNext();) {
                     XWikiMigration migration = (XWikiMigration) it.next();
-                    LOG.info("  " + migration.migrator.getName() + " - " + 
migration.migrator.getDescription()
-                        + (migration.isForced ? " (forced)" : ""));
+                    if (migration.isForced || 
migration.migrator.shouldExecute(this.startupVersion)) {
+                        LOG.info("  " + migration.migrator.getName() + " - " + 
migration.migrator.getDescription()
+                            + (migration.isForced ? " (forced)" : ""));
+                    }
                 }
             } else {
                 LOG.info("No storage migration required since current version 
is [" + curversion.toString() + "]");
@@ -248,13 +250,18 @@
         XWikiDBVersion curversion = getDBVersion(context);
         for (Iterator it = migrations.iterator(); it.hasNext();) {
             XWikiMigration migration = (XWikiMigration) it.next();
-            if (LOG.isInfoEnabled()) {
-                LOG.info("Running migration [" + migration.migrator.getName() 
+ "] with version ["
-                    + migration.migrator.getVersion() + "]");
-            }
 
             if (migration.isForced || 
migration.migrator.shouldExecute(this.startupVersion)) {
+                if (LOG.isInfoEnabled()) {
+                    LOG.info("Running migration [" + 
migration.migrator.getName() + "] with version ["
+                        + migration.migrator.getVersion() + "]");
+                }
                 migration.migrator.migrate(this, context);
+            } else {
+                if (LOG.isInfoEnabled()) {
+                    LOG.info("Skipping uneeded migration [" + 
migration.migrator.getName() + "] with version ["
+                        + migration.migrator.getVersion() + "]");
+                }
             }
 
             if (migration.migrator.getVersion().compareTo(curversion) > 0) {
@@ -263,6 +270,7 @@
                     LOG.info("New storage version is now [" + 
getDBVersion(context) + "]");
                 }
             }
+
         }
     }
 

Modified: 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/store/migration/hibernate/R6079XWIKI1878Migrator.java
===================================================================
--- 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/store/migration/hibernate/R6079XWIKI1878Migrator.java
    2008-01-29 10:47:34 UTC (rev 7149)
+++ 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/store/migration/hibernate/R6079XWIKI1878Migrator.java
    2008-01-29 11:20:47 UTC (rev 7150)
@@ -24,17 +24,18 @@
 import org.hibernate.Session;
 import org.hibernate.HibernateException;
 import org.hibernate.Transaction;
+import org.hibernate.Query;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.doc.rcs.XWikiRCSNodeContent;
+import com.xpn.xwiki.doc.rcs.XWikiRCSNodeId;
+import com.xpn.xwiki.doc.rcs.XWikiPatch;
 import com.xpn.xwiki.store.migration.XWikiDBVersion;
 import com.xpn.xwiki.store.XWikiHibernateBaseStore;
 import com.xpn.xwiki.store.XWikiHibernateVersioningStore;
 
-import java.sql.Statement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.PreparedStatement;
+import java.util.Iterator;
 
 /**
  * Migration for XWIKI1878: Fix xwikircs table isdiff data not matching RCS 
state of some revisions (when the state
@@ -93,34 +94,34 @@
             public Object doInHibernate(Session session) throws 
HibernateException, XWikiException
             {
                 try {
-                    Statement stmt = session.connection().createStatement();
-                    ResultSet rs;
-                    try {
-                        rs = stmt.executeQuery("select xwikircs.XWR_DOCID, 
xwikircs.XWR_VERSION1, xwikircs.XWR_VERSION2, xwikidoc.XWD_FULLNAME from 
xwikircs, xwikidoc where xwikidoc.XWD_ID = xwikircs.XWR_DOCID and XWR_ISDIFF = 
b'1' and XWR_PATCH like '<?xml%'");
-                    } catch (SQLException e) {
-                        // Means the xwikircs table doesn't exist which isn't 
normal.
-                        throw new 
XWikiException(XWikiException.MODULE_XWIKI_STORE,
-                            XWikiException.ERROR_XWIKI_STORE_MIGRATION, 
"Failed to get RCS data", e);
-                    }
+                    Query query = session.createQuery("select rcs.id, 
rcs.patch, doc.fullName "
+                        + "from XWikiDocument as doc, XWikiRCSNodeContent as 
rcs where "
+                        + "doc.id = rcs.id.docId and rcs.patch.diff = true and 
rcs.patch.content like '<?xml%'");
+                    Iterator it = query.list().iterator();
+
                     Transaction originalTransaction = 
((XWikiHibernateVersioningStore)context.getWiki().getVersioningStore()).getTransaction(context);
                     
((XWikiHibernateVersioningStore)context.getWiki().getVersioningStore()).setSession(null,
 context);
                     
((XWikiHibernateVersioningStore)context.getWiki().getVersioningStore()).setTransaction(null,
 context);
-                    PreparedStatement updateStatement = 
session.connection().prepareStatement("update xwikircs set XWR_ISDIFF = b'0' 
where XWR_DOCID=? and XWR_VERSION1=? and XWR_VERSION2=?");
 
-                    while (rs.next()) {
+                    while (it.hasNext()) {
+                        Object[] result = (Object[]) it.next();
                         if (LOG.isInfoEnabled()) {
-                            LOG.info("Fixing document [" + rs.getString(4) + 
"]...");
+                            LOG.info("Fixing document [" + result[2] + "]...");
                         }
-                        updateStatement.setLong(1, rs.getLong(1));
-                        updateStatement.setInt(2, rs.getInt(2));
-                        updateStatement.setInt(3, rs.getInt(3));
-                        updateStatement.executeUpdate();
+
+                        // Reconstruct a XWikiRCSNodeContent object with 
isDiff set to false and update it.
+                        XWikiRCSNodeId nodeId = (XWikiRCSNodeId) result[0];
+                        XWikiRCSNodeContent fixedNodeContent = new 
XWikiRCSNodeContent(nodeId);
+                        XWikiPatch patch = (XWikiPatch) result[1];
+                        patch.setDiff(false);
+                        fixedNodeContent.setPatch(patch);
+
+                        session.update(fixedNodeContent);
                     }
-                    updateStatement.close();
-                    stmt.close();
+
                     
((XWikiHibernateVersioningStore)context.getWiki().getVersioningStore()).setSession(session,
 context);
                     
((XWikiHibernateVersioningStore)context.getWiki().getVersioningStore()).setTransaction(originalTransaction,
 context);
-                } catch (SQLException e) {
+                } catch (Exception e) {
                     throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
                         XWikiException.ERROR_XWIKI_STORE_MIGRATION, getName() 
+ " migration failed", e);
                 }

_______________________________________________
notifications mailing list
notifications@xwiki.org
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to