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

File R42000XWIKI7726DataMigration.java

 

Coverage histogram

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

Code metrics

2
24
7
2
136
84
9
0.38
3.43
3.5
1.29

Classes

Class Line # Actions
R42000XWIKI7726DataMigration 53 8 0% 6 11
0.1538461615.4%
R42000XWIKI7726DataMigration.R42000Work 97 16 0% 3 20
0.00%
 

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.Connection;
24    import java.sql.PreparedStatement;
25    import java.sql.ResultSet;
26    import java.sql.SQLException;
27   
28    import javax.inject.Named;
29    import javax.inject.Singleton;
30   
31    import org.hibernate.HibernateException;
32    import org.hibernate.Session;
33    import org.hibernate.jdbc.Work;
34    import org.xwiki.component.annotation.Component;
35   
36    import com.xpn.xwiki.XWikiException;
37    import com.xpn.xwiki.store.DatabaseProduct;
38    import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
39    import com.xpn.xwiki.store.migration.DataMigrationException;
40    import com.xpn.xwiki.store.migration.XWikiDBVersion;
41   
42    /**
43    * Migration for XWIKI-7726: Unable to delete attachments larger than 10 mb using the jetty + hsql distribution. Early
44    * versions of the HSQLDialect ignored the specified minimum length of LOB columns, creating them with the default
45    * length of 16M. Thus, the precision of existing CLOB and BLOB columns must be manually extended to the required 1G.
46    *
47    * @version $Id: 051ba1ec82ec927e8af6f41bfcad147828070fb5 $
48    * @since 4.2M3
49    */
50    @Component
51    @Named("R42000XWIKI7726")
52    @Singleton
 
53    public class R42000XWIKI7726DataMigration extends AbstractHibernateDataMigration
54    {
 
55  0 toggle @Override
56    public String getDescription()
57    {
58  0 return "Increase the size of the standard BLOB and CLOB columns which were created with the default 16M size.";
59    }
60   
 
61  206 toggle @Override
62    public XWikiDBVersion getVersion()
63    {
64  206 return new XWikiDBVersion(42000);
65    }
66   
 
67  0 toggle @Override
68    public boolean shouldExecute(XWikiDBVersion startupVersion)
69    {
70   
71  0 try {
72  0 return getStore().getDatabaseProductName() == DatabaseProduct.HSQLDB;
73    } catch (DataMigrationException ex) {
74  0 return false;
75    }
76    }
77   
 
78  0 toggle @Override
79    public void hibernateMigrate() throws DataMigrationException, XWikiException
80    {
81  0 getStore().executeWrite(getXWikiContext(), new HibernateCallback<Object>()
82    {
 
83  0 toggle @Override
84    public Object doInHibernate(Session session) throws HibernateException, XWikiException
85    {
86  0 session.doWork(new R42000Work());
87  0 return Boolean.TRUE;
88    }
89    });
90    }
91   
92    /**
93    * Hibernate {@link Work} class doing the actual work of this migrator.
94    *
95    * @version $Id: 051ba1ec82ec927e8af6f41bfcad147828070fb5 $
96    */
 
97    private static class R42000Work implements Work
98    {
 
99  0 toggle @Override
100    public void execute(Connection connection) throws SQLException
101    {
102  0 processColumn("XWIKIRCS", "XWR_PATCH", connection);
103  0 processColumn("XWIKIRECYCLEBIN", "XDD_XML", connection);
104  0 processColumn("XWIKIATTRECYCLEBIN", "XDA_XML", connection);
105  0 processColumn("XWIKIATTACHMENT_CONTENT", "XWA_CONTENT", connection);
106  0 processColumn("XWIKIATTACHMENT_ARCHIVE", "XWA_ARCHIVE", connection);
107    }
108   
109    /**
110    * Increase the size of one column.
111    *
112    * @param tableName the name of the table to process
113    * @param columnName the name of the column to process
114    * @param connection the database connection to use
115    * @throws SQLException in case anything goes wrong
116    */
 
117  0 toggle private void processColumn(String tableName, String columnName, Connection connection)
118    throws SQLException
119    {
120  0 String command = "ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE %s(%d)";
121  0 PreparedStatement getCurrentColumnType = connection.prepareStatement(
122    "select DATA_TYPE from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME=?");
123  0 getCurrentColumnType.setString(1, tableName);
124  0 getCurrentColumnType.setString(2, columnName);
125  0 ResultSet result = getCurrentColumnType.executeQuery();
126  0 if (!result.next()) {
127  0 return;
128    }
129  0 String currentColumnType = result.getString(1);
130  0 result.close();
131  0 getCurrentColumnType.close();
132  0 connection.createStatement().execute(
133    String.format(command, tableName, columnName, currentColumnType, 1 << 30));
134    }
135    }
136    }