1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.store.migration.hibernate

File R4359XWIKI1459DataMigration.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart1.png
82% of files have more coverage

Code metrics

6
38
5
1
173
116
12
0.32
7.6
5
2.4

Classes

Class Line # Actions
R4359XWIKI1459DataMigration 56 38 0% 12 47
0.0408163264.1%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20   
21    package com.xpn.xwiki.store.migration.hibernate;
22   
23    import java.sql.PreparedStatement;
24    import java.sql.ResultSet;
25    import java.sql.SQLException;
26    import java.sql.Statement;
27   
28    import javax.inject.Inject;
29    import javax.inject.Named;
30    import javax.inject.Singleton;
31   
32    import org.hibernate.HibernateException;
33    import org.hibernate.Session;
34    import org.hibernate.Transaction;
35    import org.slf4j.Logger;
36    import org.xwiki.component.annotation.Component;
37    import org.xwiki.component.manager.ComponentLookupException;
38   
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.XWikiException;
41    import com.xpn.xwiki.doc.XWikiDocumentArchive;
42    import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
43    import com.xpn.xwiki.store.XWikiHibernateVersioningStore;
44    import com.xpn.xwiki.store.XWikiVersioningStoreInterface;
45    import com.xpn.xwiki.store.migration.DataMigrationException;
46    import com.xpn.xwiki.store.migration.XWikiDBVersion;
47   
48    /**
49    * Migration for XWIKI1459: keep document history in a separate table.
50    *
51    * @version $Id: 81b89a45eb8f611a9b86759d53d5409e089d42f1 $
52    */
53    @Component
54    @Named("R4359XWIKI1459")
55    @Singleton
 
56    public class R4359XWIKI1459DataMigration extends AbstractHibernateDataMigration
57    {
58    /**
59    * Logger.
60    */
61    @Inject
62    private Logger logger;
63   
 
64  0 toggle @Override
65    public String getDescription()
66    {
67  0 return "See http://jira.xwiki.org/jira/browse/XWIKI-1459";
68    }
69   
 
70  206 toggle @Override
71    public XWikiDBVersion getVersion()
72    {
73  206 return new XWikiDBVersion(4359);
74    }
75   
76    /**
77    * @return version store system for execute store-specific actions.
78    * @throws XWikiException if the store could not be reached
79    */
 
80  0 toggle private XWikiHibernateVersioningStore getVersioningStore() throws XWikiException
81    {
82  0 try {
83  0 return (XWikiHibernateVersioningStore) this.componentManager
84    .getInstance(XWikiVersioningStoreInterface.class, "hibernate");
85    } catch (ComponentLookupException e) {
86  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
87    XWikiException.ERROR_XWIKI_STORE_MIGRATION,
88    String.format("Unable to reach the versioning store for database %s", getXWikiContext().getWikiId()),
89    e);
90    }
91    }
92   
 
93  0 toggle @Override
94    public void hibernateMigrate() throws DataMigrationException, XWikiException
95    {
96    // migrate data
97  0 getStore().executeWrite(getXWikiContext(), true, new HibernateCallback<Object>()
98    {
 
99  0 toggle @Override
100    public Object doInHibernate(Session session) throws HibernateException, XWikiException
101    {
102  0 try {
103  0 Statement stmt = session.connection().createStatement();
104  0 ResultSet rs;
105  0 try {
106    // We place an empty character in archives for documents that have already
107    // been migrated so that we can re-execute this data migration and not start over.
108    // Note that we cannot use NULL since in old databases (prior to 1.1) the
109    // XWD_ARCHIVE column had a not null constraint and since this column has disappeared in 1.2
110    // and after, the hibernate update script will not have modified the nullability of it...
111    // (see http://jira.xwiki.org/jira/browse/XWIKI-2074).
112  0 rs =
113    stmt
114    .executeQuery(
115    "select XWD_ID, XWD_ARCHIVE, XWD_FULLNAME from xwikidoc"
116    + " where (XWD_ARCHIVE is not null and XWD_ARCHIVE <> ' ') order by XWD_VERSION");
117    } catch (SQLException e) {
118    // most likely there is no XWD_ARCHIVE column, so migration is not needed
119    // is there easier way to find what column is not exist?
120  0 return null;
121    }
122   
123  0 XWikiContext context = getXWikiContext();
124  0 XWikiHibernateVersioningStore versioningStore = getVersioningStore();
125  0 Transaction originalTransaction = versioningStore.getTransaction(context);
126  0 versioningStore.setSession(null, context);
127  0 versioningStore.setTransaction(null, context);
128  0 PreparedStatement deleteStatement =
129    session.connection().prepareStatement("update xwikidoc set XWD_ARCHIVE=' ' where XWD_ID=?");
130   
131  0 while (rs.next()) {
132  0 if (R4359XWIKI1459DataMigration.this.logger.isInfoEnabled()) {
133  0 R4359XWIKI1459DataMigration.this.logger.info("Updating document [{}]...", rs.getString(3));
134    }
135  0 long docId = Long.parseLong(rs.getString(1));
136  0 String sArchive = rs.getString(2);
137   
138    // In some weird cases it can happen that the XWD_ARCHIVE field is empty
139    // (that shouldn't happen but we've seen it happening).
140    // In this case just ignore the archive...
141  0 if (sArchive.trim().length() != 0) {
142  0 XWikiDocumentArchive docArchive = new XWikiDocumentArchive(docId);
143  0 try {
144  0 docArchive.setArchive(sArchive);
145    } catch (XWikiException e) {
146  0 R4359XWIKI1459DataMigration.this.logger.warn(
147    "The RCS archive for [{}] is broken. Internal error [{}]."
148    + " The history for this document has been reset.",
149    rs.getString(3), e.getMessage());
150    }
151  0 getVersioningStore().saveXWikiDocArchive(docArchive, true, context);
152    } else {
153  0 R4359XWIKI1459DataMigration.this.logger.warn(
154    "Empty revision found for document [{}]. Ignoring non-fatal error.",
155    rs.getString(3));
156    }
157  0 deleteStatement.setLong(1, docId);
158  0 deleteStatement.executeUpdate();
159    }
160  0 deleteStatement.close();
161  0 stmt.close();
162  0 versioningStore.setSession(session, context);
163  0 versioningStore.setTransaction(originalTransaction, context);
164    } catch (SQLException e) {
165  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
166    XWikiException.ERROR_XWIKI_STORE_MIGRATION, getName() + " migration failed", e);
167    }
168   
169  0 return Boolean.TRUE;
170    }
171    });
172    }
173    }