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

File R35100XWIKI7564DataMigration.java

 

Coverage histogram

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

Code metrics

4
21
6
2
137
88
14
0.67
3.5
3
2.33

Classes

Class Line # Actions
R35100XWIKI7564DataMigration 56 11 0% 7 14
0.12512.5%
R35100XWIKI7564DataMigration.R35100Work 107 10 0% 7 15
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.io.BufferedReader;
24    import java.io.IOException;
25    import java.io.InputStreamReader;
26    import java.io.UnsupportedEncodingException;
27    import java.sql.BatchUpdateException;
28    import java.sql.Connection;
29    import java.sql.SQLException;
30    import java.sql.Statement;
31   
32    import javax.inject.Named;
33    import javax.inject.Singleton;
34   
35    import org.hibernate.HibernateException;
36    import org.hibernate.Session;
37    import org.hibernate.jdbc.Work;
38    import org.xwiki.component.annotation.Component;
39   
40    import com.xpn.xwiki.XWikiException;
41    import com.xpn.xwiki.store.DatabaseProduct;
42    import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
43    import com.xpn.xwiki.store.migration.DataMigrationException;
44    import com.xpn.xwiki.store.migration.XWikiDBVersion;
45   
46    /**
47    * Migration for XWIKI-7564: Manually change the SQL type of long binary columns from inline bytea to proper LOBs when
48    * the underlying database is PostgreSQL.
49    *
50    * @version $Id: f1bd88cdef933d9d6612f437c0021d124fae600a $
51    * @since 3.5.1
52    */
53    @Component
54    @Named("R35100XWIKI7564")
55    @Singleton
 
56    public class R35100XWIKI7564DataMigration extends AbstractHibernateDataMigration
57    {
 
58  0 toggle @Override
59    public String getDescription()
60    {
61  0 return "See http://jira.xwiki.org/browse/XWIKI-7564";
62    }
63   
 
64  206 toggle @Override
65    public XWikiDBVersion getVersion()
66    {
67  206 return new XWikiDBVersion(35100);
68    }
69   
 
70  0 toggle @Override
71    public boolean shouldExecute(XWikiDBVersion startupVersion)
72    {
73  0 boolean shouldExecute = false;
74  0 try {
75  0 getStore().beginTransaction(getXWikiContext());
76    // Run this migration if the database isn't new
77  0 shouldExecute = (startupVersion.getVersion() > 0
78    && getStore().getDatabaseProductName() == DatabaseProduct.POSTGRESQL);
79  0 getStore().endTransaction(getXWikiContext(), false);
80    } catch (XWikiException ex) {
81    // Shouldn't happen, ignore
82    } catch (DataMigrationException ex) {
83    // Shouldn't happen, ignore
84    }
85  0 return shouldExecute;
86    }
87   
 
88  0 toggle @Override
89    public void hibernateMigrate() throws DataMigrationException, XWikiException
90    {
91  0 getStore().executeWrite(getXWikiContext(), new HibernateCallback<Object>()
92    {
 
93  0 toggle @Override
94    public Object doInHibernate(Session session) throws HibernateException, XWikiException
95    {
96  0 session.doWork(new R35100Work());
97  0 return Boolean.TRUE;
98    }
99    });
100    }
101   
102    /**
103    * Hibernate {@link Work} class that reads an SQL script file and executes them.
104    *
105    * @version $Id: f1bd88cdef933d9d6612f437c0021d124fae600a $
106    */
 
107    private static class R35100Work implements Work
108    {
 
109  0 toggle @Override
110    public void execute(Connection connection) throws SQLException
111    {
112  0 try {
113  0 Statement stmt = connection.createStatement();
114  0 BufferedReader in = new BufferedReader(new InputStreamReader(
115    this.getClass().getResourceAsStream("R35100XWIKI7564.sql"), "UTF-8"));
116  0 String line;
117  0 while ((line = in.readLine()) != null) {
118  0 stmt.addBatch(line);
119    }
120  0 stmt.executeBatch();
121    } catch (BatchUpdateException ex) {
122  0 if (ex.getNextException() != null
123    && ex.getNextException().getMessage().contains("function lowrite(integer, oid)")) {
124    // This exception is thrown when this migrator isn't really needed. This happens when migrating from
125    // a version between 3.2 and 3.5, which do use the proper table structure, but we can't easily
126    // distinguish between a pre-3.2 database and a post-3.2 database.
127  0 return;
128    }
129  0 throw ex;
130    } catch (UnsupportedEncodingException ex) {
131    // Should never happen, UTF-8 is always available
132    } catch (IOException ex) {
133    // Shouldn't happen, the script is supposed to be there
134    }
135    }
136    }
137    }