Project

General

Profile

« Previous | Next » 

Revision 8f952b7f

Added by Andreas Müller about 8 years ago

Fix revisionnumber update for PostgreSQL #5718

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v36_40/RevisionNumberUpdater.java
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.database.update.v36_40;
11

  
12
import java.sql.SQLException;
13
import java.util.List;
14

  
15
import org.apache.log4j.Logger;
16

  
17
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
18
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
19
import eu.etaxonomy.cdm.database.ICdmDataSource;
20
import eu.etaxonomy.cdm.database.update.CaseType;
21
import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
22
import eu.etaxonomy.cdm.database.update.ITermUpdaterStep;
23
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
24

  
25

  
26
/**
27
 * Updates the xxxObj_type field in Annotations, Markers, Extensions, Identifiers.
28
 * Not needed anymore as long as we gave up bidirectionality #5743
29
 *
30
 * @author a.mueller
31
 * @date 25.04.2016
32
 */
33
public class RevisionNumberUpdater extends SchemaUpdaterStepBase<RevisionNumberUpdater> implements ITermUpdaterStep{
34
	private static final Logger logger = Logger.getLogger(RevisionNumberUpdater.class);
35

  
36
	private static final String stepName = "Update referenced obj_type";
37

  
38
// **************************** STATIC METHODS ********************************/
39

  
40
	public static final RevisionNumberUpdater NewInstance(String stepName, List<ISchemaUpdaterStep> stepList){
41
		RevisionNumberUpdater result = new RevisionNumberUpdater(stepName);
42
		stepList.add(result);
43
		return result;
44
	}
45

  
46
	private RevisionNumberUpdater(String stepName) {
47
		super(stepName);
48
	}
49

  
50
	@Override
51
	public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
52

  
53
	    String sql;
54
	    String casedAuditTable = caseType.transformTo("AuditEvent");
55

  
56
	    DatabaseTypeEnum databaseType = datasource.getDatabaseType();
57
	    if (databaseType == DatabaseTypeEnum.MySQL){
58
	        sql = "ALTER TABLE "+casedAuditTable+" ALTER revisionnumber DROP DEFAULT";  //needed? There was no default before
59
	        datasource.executeUpdate(sql);
60
	        sql = "ALTER TABLE "+casedAuditTable+" CHANGE COLUMN revisionnumber revisionnumber INT(11) NOT NULL FIRST";
61
	        datasource.executeUpdate(sql);
62
	    }else if (databaseType == DatabaseTypeEnum.H2){
63
	        sql = "ALTER TABLE "+casedAuditTable+" ALTER COLUMN revisionnumber INT NOT NULL";
64
	        datasource.executeUpdate(sql);
65
        }else if (databaseType == DatabaseTypeEnum.PostgreSQL){
66
	        //NOTHING TO DO
67
        }else if (databaseType == DatabaseTypeEnum.SqlServer2005){
68
            throw new RuntimeException("SQLServer not supported by RevisionNumberUpdater");
69
	    }else{
70
	        throw new RuntimeException("Database type " + databaseType.toString() + " not supported by RevisionNumberUpdater");
71
	    }
72
	    return 0;
73
	}
74

  
75

  
76

  
77
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v36_40/SchemaUpdater_36_40.java
57 57
		String tableName;
58 58
		ISchemaUpdaterStep step;
59 59
//		String columnName;
60
		String query;
60 61
		String newColumnName;
61 62
		String oldColumnName;
62 63
		String columnNames[];
......
83 84
        stepList.add(step);
84 85

  
85 86
        //#5718
86
        //Remove autoincrement from AuditEvent.revisionnumber
87
        //Remove autoincrement from AuditEvent.revisionnumber if necessary
87 88
        stepName = "Remove autoincrement from AuditEvent.revisionnumber";
88
//        String query = "ALTER TABLE @@AuditEvent@@ ALTER revisionnumber DROP DEFAULT";
89
//        step = SimpleSchemaUpdaterStep.NewNonAuditedInstance(stepName, query, -99);
90
//        stepList.add(step);
91
        tableName = "AuditEvent";
92
        oldColumnName = "revisionnumber";
93
        newColumnName = "revisionnumberOld";
94
        step = ColumnNameChanger.NewIntegerInstance(stepName, tableName, oldColumnName, newColumnName, ! INCLUDE_AUDIT);
95
        stepList.add(step);
96

  
97
        tableName = "AuditEvent";
98
        String columnName = oldColumnName;
99
        Integer defaultValue = null;
100
        boolean notNull = false;  //TODO set to true after data has filled, but we are missing a NOT-NULL-Constraint adder.
101
        step = ColumnAdder.NewIntegerInstance(stepName, tableName, columnName, ! INCLUDE_AUDIT, defaultValue, notNull);
102
        stepList.add(step);
89
        RevisionNumberUpdater.NewInstance(stepName, stepList);
103 90

  
104
        String query = "UPDATE @@AuditEvent@@ SET revisionnumber = revisionnumberOld";
105
        step = SimpleSchemaUpdaterStep.NewNonAuditedInstance(stepName, query, -99);
106
        stepList.add(step);
107

  
108
        step = ColumnRemover.NewInstance(stepName, tableName, newColumnName, ! INCLUDE_AUDIT);
109
        stepList.add(step);
110 91

  
111 92
        //#5734
112 93
        //Add symbol to terms
......
130 111
        stepList.add(step);
131 112

  
132 113
        stepName = "Update symbols for terms";
133
        query = "UPDATE DefinedTermBase dtb SET dtb.inverseSymbol = ( " +
114
        query = "UPDATE DefinedTermBase dtb SET inverseSymbol = ( " +
134 115
            " SELECT  r.abbreviatedlabel " +
135 116
            " FROM RelationshipTermBase_inverseRepresentation MN " +
136 117
                " INNER JOIN Representation r ON r.id = MN.inverserepresentations_id " +

Also available in: Unified diff