1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
import java.sql.Statement; |
28 |
|
import java.text.MessageFormat; |
29 |
|
import java.util.HashMap; |
30 |
|
import java.util.Map; |
31 |
|
import java.util.Map.Entry; |
32 |
|
|
33 |
|
import javax.inject.Named; |
34 |
|
import javax.inject.Singleton; |
35 |
|
|
36 |
|
import org.apache.commons.lang3.StringUtils; |
37 |
|
import org.hibernate.HibernateException; |
38 |
|
import org.hibernate.Session; |
39 |
|
import org.hibernate.jdbc.Work; |
40 |
|
import org.slf4j.Logger; |
41 |
|
import org.slf4j.LoggerFactory; |
42 |
|
import org.xwiki.component.annotation.Component; |
43 |
|
|
44 |
|
import com.xpn.xwiki.XWikiException; |
45 |
|
import com.xpn.xwiki.store.DatabaseProduct; |
46 |
|
import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback; |
47 |
|
import com.xpn.xwiki.store.migration.DataMigrationException; |
48 |
|
import com.xpn.xwiki.store.migration.XWikiDBVersion; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@Component |
57 |
|
@Named("R35102XWIKI7771") |
58 |
|
@Singleton |
|
|
| 11.1% |
Uncovered Elements: 16 (18) |
Complexity: 7 |
Complexity Density: 0.54 |
|
59 |
|
public class R35102XWIKI7771DataMigration extends AbstractHibernateDataMigration |
60 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
61 |
0 |
@Override... |
62 |
|
public String getDescription() |
63 |
|
{ |
64 |
0 |
return "See http://jira.xwiki.org/browse/XWIKI-7771"; |
65 |
|
} |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
206 |
@Override... |
68 |
|
public XWikiDBVersion getVersion() |
69 |
|
{ |
70 |
206 |
return new XWikiDBVersion(35102); |
71 |
|
} |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
|
73 |
0 |
@Override... |
74 |
|
public boolean shouldExecute(XWikiDBVersion startupVersion) |
75 |
|
{ |
76 |
0 |
boolean shouldExecute = false; |
77 |
0 |
try { |
78 |
0 |
getStore().beginTransaction(getXWikiContext()); |
79 |
|
|
80 |
0 |
shouldExecute = (startupVersion.getVersion() > 0 |
81 |
|
&& getStore().getDatabaseProductName() == DatabaseProduct.POSTGRESQL); |
82 |
0 |
getStore().endTransaction(getXWikiContext(), false); |
83 |
|
} catch (XWikiException ex) { |
84 |
|
|
85 |
|
} catch (DataMigrationException ex) { |
86 |
|
|
87 |
|
} |
88 |
0 |
return shouldExecute; |
89 |
|
} |
90 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
0 |
@Override... |
92 |
|
public void hibernateMigrate() throws DataMigrationException, XWikiException |
93 |
|
{ |
94 |
0 |
getStore().executeWrite(getXWikiContext(), new HibernateCallback<Object>() |
95 |
|
{ |
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
96 |
0 |
@Override... |
97 |
|
public Object doInHibernate(Session session) throws HibernateException, XWikiException |
98 |
|
{ |
99 |
0 |
session.doWork(new R35102Work("xwikircs", "xwr_patch", "xwr_docid", "History for document with ID")); |
100 |
0 |
session.doWork(new R35102Work("xwikirecyclebin", "xdd_xml", "xdd_id", "Deleted document with ID")); |
101 |
0 |
session.doWork(new R35102Work("xwikiattrecyclebin", "xda_xml", "xda_id", "Deleted attachment with ID")); |
102 |
0 |
return Boolean.TRUE; |
103 |
|
} |
104 |
|
}); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
|
@link |
109 |
|
|
110 |
|
@version |
111 |
|
|
|
|
| 0% |
Uncovered Elements: 37 (37) |
Complexity: 6 |
Complexity Density: 0.21 |
|
112 |
|
private static class R35102Work implements Work |
113 |
|
{ |
114 |
|
|
115 |
|
private String tableName; |
116 |
|
|
117 |
|
|
118 |
|
private String columnName; |
119 |
|
|
120 |
|
|
121 |
|
private String idColumnName; |
122 |
|
|
123 |
|
|
124 |
|
private String dataType; |
125 |
|
|
126 |
|
|
127 |
|
private Logger logger = LoggerFactory.getLogger(R35102Work.class); |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
@param |
133 |
|
@param |
134 |
|
@param |
135 |
|
|
136 |
|
@param |
137 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
138 |
0 |
R35102Work(String tableName, String columnName, String idColumnName, String dataType)... |
139 |
|
{ |
140 |
0 |
this.tableName = tableName; |
141 |
0 |
this.columnName = columnName; |
142 |
0 |
this.idColumnName = idColumnName; |
143 |
0 |
this.dataType = dataType; |
144 |
|
} |
145 |
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 5 |
Complexity Density: 0.2 |
|
146 |
0 |
@Override... |
147 |
|
public void execute(Connection connection) throws SQLException |
148 |
|
{ |
149 |
0 |
Statement stmt = connection.createStatement(); |
150 |
0 |
ResultSet lobs = stmt.executeQuery("SELECT " + this.columnName + ", " + this.idColumnName + " FROM " |
151 |
|
+ this.tableName + ";"); |
152 |
0 |
Map<String, Long> lobsToProcess = new HashMap<String, Long>(); |
153 |
0 |
while (lobs.next()) { |
154 |
|
|
155 |
0 |
if (StringUtils.isNumeric(lobs.getString(1))) { |
156 |
0 |
lobsToProcess.put(lobs.getString(1), lobs.getLong(2)); |
157 |
|
} |
158 |
|
} |
159 |
0 |
PreparedStatement inlineLob = connection.prepareStatement(MessageFormat.format("UPDATE {0} SET {1} =" |
160 |
|
+ " convert_from(loread(lo_open({1}::int, 262144), 10000000), ''LATIN1'') WHERE {1} = ?", |
161 |
|
this.tableName, this.columnName)); |
162 |
0 |
PreparedStatement emptyLob = connection.prepareStatement(MessageFormat.format("UPDATE {0} SET {1} = ''''" |
163 |
|
+ " WHERE {1} = ?", this.tableName, this.columnName)); |
164 |
0 |
PreparedStatement removeLob = connection.prepareStatement("select lo_unlink(?)"); |
165 |
0 |
for (Entry<String, Long> lob : lobsToProcess.entrySet()) { |
166 |
0 |
try { |
167 |
0 |
inlineLob.setString(1, lob.getKey()); |
168 |
0 |
inlineLob.executeUpdate(); |
169 |
|
|
170 |
0 |
removeLob.setLong(1, Long.valueOf(lob.getKey())); |
171 |
0 |
removeLob.execute(); |
172 |
0 |
connection.commit(); |
173 |
|
} catch (SQLException ex) { |
174 |
0 |
if (ex.getMessage().contains("0x00")) { |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
0 |
connection.rollback(); |
181 |
0 |
this.logger.warn(this.dataType + " [{}] cannot be recovered", |
182 |
|
lob.getValue()); |
183 |
0 |
emptyLob.setString(1, lob.getKey()); |
184 |
0 |
emptyLob.executeUpdate(); |
185 |
0 |
removeLob.setLong(1, Long.valueOf(lob.getKey())); |
186 |
0 |
removeLob.execute(); |
187 |
0 |
connection.commit(); |
188 |
|
} else { |
189 |
0 |
throw ex; |
190 |
|
} |
191 |
|
} |
192 |
|
} |
193 |
|
} |
194 |
|
} |
195 |
|
} |