Project

General

Profile

Download (10.5 KB) Statistics
| Branch: | Tag: | Revision:
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.v515_518.SchemaUpdater_5185_5186;
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
public class CdmUpdater {
65

    
66
    private static final Logger logger = Logger.getLogger(CdmUpdater.class);
67

    
68
    public static CdmUpdater NewInstance(){
69
        return new CdmUpdater();
70
    }
71

    
72
    private ISchemaUpdater getCurrentSchemaUpdater() {
73
        return SchemaUpdater_5185_5186.NewInstance();
74
    }
75

    
76
    public SchemaUpdateResult updateToCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor){
77
        SchemaUpdateResult result = new SchemaUpdateResult();
78
        if (monitor == null){
79
            monitor = DefaultProgressMonitor.NewInstance();
80
        }
81
        CaseType caseType = CaseType.caseTypeOfDatasource(datasource);
82

    
83
        ISchemaUpdater currentSchemaUpdater = getCurrentSchemaUpdater();
84

    
85
        int steps = currentSchemaUpdater.countSteps(datasource, monitor, caseType);
86
        steps++;  //for hibernate_sequences update
87

    
88
        String taskName = "Update to schema version " + currentSchemaUpdater.getTargetVersion();
89
        monitor.beginTask(taskName, steps);
90

    
91
        try {
92
            datasource.startTransaction();
93
            currentSchemaUpdater.invoke(datasource, monitor, caseType, result);
94
            if (result.isSuccess()){
95
                //TODO should not run if no update was necesssary
96
                updateHibernateSequence(datasource, monitor, caseType, result);
97
            }
98
            if (!result.isSuccess()){
99
                datasource.rollback();  //does not work for ddl statements, therefore not really necessary
100
            }else{
101
                datasource.commitTransaction();
102
            }
103
        } catch (Exception e) {
104
            String message = "Stopped schema updater";
105
            result.addException(e, message, "CdmUpdater");
106
            monitor.warning(message);
107
        } finally {
108
            String message = "Update finished " + (result.isSuccess() ? "successfully" : "with ERRORS");
109
            monitor.subTask(message);
110
            if (!result.isSuccess()){
111
                monitor.warning(message);
112
                monitor.setCanceled(true);
113
            }else{
114
                monitor.done();
115
            }
116
            logger.info(message);
117
        }
118

    
119
        return result;
120
    }
121

    
122
    /**
123
     * Updating terms often inserts new terms, vocabularies and representations.
124
     * Therefore the counter in hibernate_sequences must be increased.
125
     * We do this once at the end of term updating.
126
     * @param caseType
127
     * @param result2
128
     * @return true if update was successful, false otherwise
129
     */
130
    private void updateHibernateSequence(ICdmDataSource datasource, IProgressMonitor monitor,
131
            CaseType caseType, SchemaUpdateResult result) {
132
        monitor.subTask("Update hibernate sequences");
133
        try {
134
            String sql = "SELECT * FROM hibernate_sequences ";
135
            ResultSet rs = datasource.executeQuery(sql);
136
            while (rs.next()){
137
                String table = rs.getString("sequence_name");
138
                Integer val = rs.getInt("next_val");
139
                updateSingleValue(datasource, monitor, table, val, caseType, result);
140
            }
141
        } catch (Exception e) {
142
            String message = "Exception occurred when trying to update hibernate_sequences table: " + e.getMessage();
143
            monitor.warning(message, e);
144
            logger.error(message);
145
            result.addException(e, message, "CdmUpdater.updateHibernateSequence");
146
        }finally{
147
            monitor.worked(1);
148
        }
149
        return;
150
    }
151

    
152
    private void updateSingleValue(ICdmDataSource datasource, IProgressMonitor monitor, String table,
153
                Integer oldVal, CaseType caseType, SchemaUpdateResult result){
154
        if (table.equals("default")){  //found in flora central africa test database
155
            return;
156
        }
157
        try {
158
            Integer newVal;
159
            try {
160
                String id = table.equalsIgnoreCase("AuditEvent")? "revisionNumber" : "id";
161
                String sql = " SELECT max(%s) FROM %s ";
162
                newVal = (Integer)datasource.getSingleValue(String.format(sql, id, caseType.transformTo(table)));
163
            } catch (Exception e) {
164
                String message = "Could not retrieve max value for table '%s'. Will not update hibernate_sequence for this table. " +
165
                        "Usually this will not cause problems, however, if new data has been added to " +
166
                        "this table by the update script one may encounter 'unique identifier' " +
167
                        "exceptions when trying to add further data.";
168
                monitor.warning(String.format(message,table), e);
169
                result.addWarning(message, (String)null, "table = " + table);
170
                return;
171
            }
172

    
173
            if (newVal != null){
174
                //This is how {@link PooledOptimizer#generate(org.hibernate.id.enhanced.AccessCallback)} works
175
                //it substracts the increment size from the value in hibernate_sequences to get the initial value.
176
                //Haven't checked why.
177
                //For the correct increment size see eu.etaxonomy.cdm.model.common.package-info.java
178
                int incrementSize = 10;
179
                newVal = newVal + incrementSize;
180
                if (newVal >= oldVal){
181
                    String sql = " UPDATE hibernate_sequences " +
182
                            " SET next_val = %d " +
183
                            " WHERE sequence_name = '%s' ";
184
                    datasource.executeUpdate(String.format(sql, newVal + 1 , table) );
185
                }
186
            }
187
            return;
188
        } catch (Exception e) {
189
            String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + table + ": " + e.getMessage();
190
            monitor.warning(message, e);
191
            logger.error(message);
192
            result.addException(e, message, "CdmUpdater.updateSingleValue(table = " + table + ")");
193
        }
194
    }
195

    
196
    /**
197
     * @param args SERVER DB_NAME1[,DB_NAME2,...] [USER] [PASSWORD] [PORT]
198
     */
199
    public static void main(String[] args) {
200
//        logger.warn("main method not yet fully implemented (only works with mysql!!!)");
201
//        if(args.length < 2){
202
//            logger.error("Arguments missing: server database [username [password]]");
203
//        }
204
        //TODO better implementation
205
        CdmUpdater myUpdater = new CdmUpdater();
206
        System.out.println("CdmUpdater\nArguments: SERVER DB_NAME1[,DB_NAME2,...] [USER] [PASSWORD] [PORT]");
207
        String server = args[0];
208
        String database  = args[1];
209
        String[] databaseNames = StringUtils.split(database, ',');
210
        String username = args.length > 2 ? args[2] : null;
211
        String password  = args.length > 3 ? args[3] : null;
212
        int port  = 3306;
213
        if( args.length > 4){
214
            try {
215
                port = Integer.parseInt(args[4]);
216
            } catch (Exception e) {
217
                // ignore
218
            }
219
        }
220
        System.out.println("Number of databases to update: " + databaseNames.length);
221
        for(String dnName : databaseNames){
222
            System.out.println(dnName + " UPDATE ...");
223
            ICdmDataSource dataSource = CdmDataSource.NewMySqlInstance(server, dnName, port, username, password);
224
            SchemaUpdateResult result = myUpdater.updateToCurrentVersion(dataSource, null);
225
            System.out.println(dnName + " DONE " + (result.isSuccess() ? "successfully" : "with ERRORS"));
226
            System.out.println(result.createReport().toString());
227
            System.out.println("====================================================================");
228
        }
229
    }
230
}
(4-4/40)