1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.database.update;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
|
13
|
import org.apache.commons.lang.StringUtils;
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
|
17
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
18
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
19
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
20
|
import eu.etaxonomy.cdm.database.update.v47_49.SchemaUpdater_47_49;
|
21
|
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
|
22
|
|
23
|
/**
|
24
|
* This class launches CDM model updates.
|
25
|
* <BR>
|
26
|
* For each new schema version number there usually exists 1 {@link ISchemaUpdater} which
|
27
|
* represents a list of schema update steps. {@link ISchemaUpdater schema updaters} are linked
|
28
|
* to previous updaters which are called, if relevant, previous to the latest updater.
|
29
|
* So it is possible to upgrade multiple schema version steps in one call.
|
30
|
* <BR><BR>
|
31
|
* As said before each {@link ISchemaUpdater schema updater} creates a list of
|
32
|
* {@link ISchemaUpdaterStep update steps}.
|
33
|
* <BR><BR>
|
34
|
* {@link ISchemaUpdater} support progression monitoring with each update step being one task.
|
35
|
* <BR><BR>
|
36
|
* ATTENTION: Some steps in the schema update are not transactional by nature. E.g. adding or removing a column
|
37
|
* to a table in a SQL database can not be handled in a transaction. Therefore failures in
|
38
|
* certain steps may not lead to a complete rollback of all steps covered by a {@link ISchemaUpdater}.
|
39
|
* This may lead to a situation where the database becomes inconsistent.
|
40
|
* <BR><BR>
|
41
|
* <u>HOW TO ADD A NEW UPDATER?</u><BR>
|
42
|
* Adding a new updater currently still needs adjustment at multiple places.
|
43
|
* <BR>
|
44
|
* <BR>1.) Increment {@link CdmMetaData} schema version number.
|
45
|
* <BR>2.) Create a new class instance of {@link SchemaUpdaterBase} (e.g. by copying an old one).
|
46
|
* <BR>3.) Update startSchemaVersion and endSchemaVersion in this new class, where startSchemaVersion
|
47
|
* is the old schema version and endSchemaVersion is the new schema version.
|
48
|
* <BR>4.) Implement {@link ISchemaUpdater#getPreviousUpdater()} and {@link ISchemaUpdater#getNextUpdater()}
|
49
|
* in a way that the former returns an instance of the previous schema updater and the later returns null (for now).
|
50
|
* <BR>5.) Go to the previous schema updater class and adjust {@link ISchemaUpdater#getNextUpdater()}
|
51
|
* in a way that it returns an instance of the newly created updater.
|
52
|
* <BR>6.) Adjust {@link CdmUpdater#getCurrentSchemaUpdater()} to return
|
53
|
* instances of the newly created updater.
|
54
|
*
|
55
|
* NOTE: Prior to cdmlib version 4.8/schema version 4.7 the CdmUpdater was split into a schema updater
|
56
|
* and a term updater. This architecture caused problems and was therefore removed in 4.8.
|
57
|
*
|
58
|
* @see ISchemaUpdater
|
59
|
* @see ISchemaUpdaterStep
|
60
|
*
|
61
|
* @author a.mueller
|
62
|
* @since 10.09.2010
|
63
|
*
|
64
|
*/
|
65
|
public class CdmUpdater {
|
66
|
private static final Logger logger = Logger.getLogger(CdmUpdater.class);
|
67
|
|
68
|
public static CdmUpdater NewInstance(){
|
69
|
return new CdmUpdater();
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Returns the current CDM updater
|
74
|
* @return
|
75
|
*/
|
76
|
private ISchemaUpdater getCurrentSchemaUpdater() {
|
77
|
return SchemaUpdater_47_49.NewInstance();
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* @param datasource
|
82
|
* @param monitor may be <code>null</code>
|
83
|
* @return
|
84
|
*/
|
85
|
public SchemaUpdateResult updateToCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor){
|
86
|
SchemaUpdateResult result = new SchemaUpdateResult();
|
87
|
if (monitor == null){
|
88
|
monitor = DefaultProgressMonitor.NewInstance();
|
89
|
}
|
90
|
CaseType caseType = CaseType.caseTypeOfDatasource(datasource);
|
91
|
|
92
|
ISchemaUpdater currentSchemaUpdater = getCurrentSchemaUpdater();
|
93
|
|
94
|
int steps = currentSchemaUpdater.countSteps(datasource, monitor, caseType);
|
95
|
steps++; //for hibernate_sequences update
|
96
|
|
97
|
String taskName = "Update to schema version " + currentSchemaUpdater.getTargetVersion();
|
98
|
monitor.beginTask(taskName, steps);
|
99
|
|
100
|
try {
|
101
|
datasource.startTransaction();
|
102
|
currentSchemaUpdater.invoke(datasource, monitor, caseType, result);
|
103
|
if (result.isSuccess()){
|
104
|
//TODO should not run if no update was necesssary
|
105
|
updateHibernateSequence(datasource, monitor, caseType, result);
|
106
|
}
|
107
|
if (!result.isSuccess()){
|
108
|
datasource.rollback(); //does not work for ddl statements, therefore not really necessary
|
109
|
}else{
|
110
|
datasource.commitTransaction();
|
111
|
}
|
112
|
|
113
|
} catch (Exception e) {
|
114
|
String message = "Stopped schema updater";
|
115
|
result.addException(e, message, "CdmUpdater");
|
116
|
monitor.warning(message);
|
117
|
} finally {
|
118
|
String message = "Update finished " + (result.isSuccess() ? "successfully" : "with ERRORS");
|
119
|
monitor.subTask(message);
|
120
|
if (!result.isSuccess()){
|
121
|
monitor.warning(message);
|
122
|
monitor.setCanceled(true);
|
123
|
}else{
|
124
|
monitor.done();
|
125
|
}
|
126
|
logger.info(message);
|
127
|
}
|
128
|
|
129
|
return result;
|
130
|
}
|
131
|
|
132
|
|
133
|
|
134
|
/**
|
135
|
* Updating terms often inserts new terms, vocabularies and representations.
|
136
|
* Therefore the counter in hibernate_sequences must be increased.
|
137
|
* We do this once at the end of term updating.
|
138
|
* @param caseType
|
139
|
* @param result2
|
140
|
* @return true if update was successful, false otherwise
|
141
|
*/
|
142
|
private void updateHibernateSequence(ICdmDataSource datasource, IProgressMonitor monitor,
|
143
|
CaseType caseType, SchemaUpdateResult result) {
|
144
|
monitor.subTask("Update hibernate sequences");
|
145
|
try {
|
146
|
String sql = "SELECT * FROM hibernate_sequences ";
|
147
|
ResultSet rs = datasource.executeQuery(sql);
|
148
|
while (rs.next()){
|
149
|
String table = rs.getString("sequence_name");
|
150
|
Integer val = rs.getInt("next_val");
|
151
|
updateSingleValue(datasource, monitor, table, val, caseType, result);
|
152
|
}
|
153
|
} catch (Exception e) {
|
154
|
String message = "Exception occurred when trying to update hibernate_sequences table: " + e.getMessage();
|
155
|
monitor.warning(message, e);
|
156
|
logger.error(message);
|
157
|
result.addException(e, message, "CdmUpdater.updateHibernateSequence");
|
158
|
}finally{
|
159
|
monitor.worked(1);
|
160
|
}
|
161
|
return;
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
*
|
166
|
* @param datasource
|
167
|
* @param monitor
|
168
|
* @param table
|
169
|
* @param oldVal
|
170
|
* @param caseType
|
171
|
* @param result
|
172
|
* @return
|
173
|
*/
|
174
|
private void updateSingleValue(ICdmDataSource datasource, IProgressMonitor monitor, String table,
|
175
|
Integer oldVal, CaseType caseType, SchemaUpdateResult result){
|
176
|
if (table.equals("default")){ //found in flora central africa test database
|
177
|
return;
|
178
|
}
|
179
|
try {
|
180
|
Integer newVal;
|
181
|
try {
|
182
|
String id = table.equalsIgnoreCase("AuditEvent")? "revisionNumber" : "id";
|
183
|
String sql = " SELECT max(%s) FROM %s ";
|
184
|
newVal = (Integer)datasource.getSingleValue(String.format(sql, id, caseType.transformTo(table)));
|
185
|
} catch (Exception e) {
|
186
|
String message = "Could not retrieve max value for table '%s'. Will not update hibernate_sequence for this table. " +
|
187
|
"Usually this will not cause problems, however, if new data has been added to " +
|
188
|
"this table by the update script one may encounter 'unique identifier' " +
|
189
|
"exceptions when trying to add further data.";
|
190
|
monitor.warning(String.format(message,table), e);
|
191
|
result.addWarning(message, (String)null, "table = " + table);
|
192
|
return;
|
193
|
}
|
194
|
|
195
|
if (newVal != null){
|
196
|
//This is how {@link PooledOptimizer#generate(org.hibernate.id.enhanced.AccessCallback)} works
|
197
|
//it substracts the increment size from the value in hibernate_sequences to get the initial value.
|
198
|
//Haven't checked why.
|
199
|
//For the correct increment size see eu.etaxonomy.cdm.model.common.package-info.java
|
200
|
int incrementSize = 10;
|
201
|
newVal = newVal + incrementSize;
|
202
|
if (newVal >= oldVal){
|
203
|
String sql = " UPDATE hibernate_sequences " +
|
204
|
" SET next_val = %d " +
|
205
|
" WHERE sequence_name = '%s' ";
|
206
|
datasource.executeUpdate(String.format(sql, newVal + 1 , table) );
|
207
|
}
|
208
|
}
|
209
|
return;
|
210
|
} catch (Exception e) {
|
211
|
String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + table + ": " + e.getMessage();
|
212
|
monitor.warning(message, e);
|
213
|
logger.error(message);
|
214
|
result.addException(e, message, "CdmUpdater.updateSingleValue(table = " + table + ")");
|
215
|
}
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
*
|
220
|
*
|
221
|
* @param args SERVER DB_NAME1[,DB_NAME2,...] [USER] [PASSWORD] [PORT]
|
222
|
*/
|
223
|
public static void main(String[] args) {
|
224
|
// logger.warn("main method not yet fully implemented (only works with mysql!!!)");
|
225
|
// if(args.length < 2){
|
226
|
// logger.error("Arguments missing: server database [username [password]]");
|
227
|
// }
|
228
|
//TODO better implementation
|
229
|
CdmUpdater myUpdater = new CdmUpdater();
|
230
|
System.out.println("CdmUpdater\nArguments: SERVER DB_NAME1[,DB_NAME2,...] [USER] [PASSWORD] [PORT]");
|
231
|
String server = args[0];
|
232
|
String database = args[1];
|
233
|
String[] databaseNames = StringUtils.split(database, ',');
|
234
|
String username = args.length > 2 ? args[2] : null;
|
235
|
String password = args.length > 3 ? args[3] : null;
|
236
|
int port = 3306;
|
237
|
if( args.length > 4){
|
238
|
try {
|
239
|
port = Integer.parseInt(args[4]);
|
240
|
} catch (Exception e) {
|
241
|
// ignore
|
242
|
}
|
243
|
}
|
244
|
System.out.println("Number of databases to update: " + databaseNames.length);
|
245
|
for(String dnName : databaseNames){
|
246
|
System.out.println(dnName + " UPDATE ...");
|
247
|
ICdmDataSource dataSource = CdmDataSource.NewMySqlInstance(server, dnName, port, username, password);
|
248
|
SchemaUpdateResult result = myUpdater.updateToCurrentVersion(dataSource, null);
|
249
|
System.out.println(dnName + " DONE " + (result.isSuccess() ? "successfully" : "with ERRORS"));
|
250
|
System.out.println(result.createReport().toString());
|
251
|
System.out.println("====================================================================");
|
252
|
|
253
|
}
|
254
|
}
|
255
|
|
256
|
}
|