add nextUpdater to 331_34 updater
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / v33_34 / SchemaUpdater_331_34.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.cdm.database.update.v33_34;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.database.update.ColumnAdder;
19 import eu.etaxonomy.cdm.database.update.ColumnNameChanger;
20 import eu.etaxonomy.cdm.database.update.ColumnRemover;
21 import eu.etaxonomy.cdm.database.update.ColumnTypeChanger;
22 import eu.etaxonomy.cdm.database.update.ISchemaUpdater;
23 import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
24 import eu.etaxonomy.cdm.database.update.SchemaUpdaterBase;
25 import eu.etaxonomy.cdm.database.update.SimpleSchemaUpdaterStep;
26 import eu.etaxonomy.cdm.database.update.TableCreator;
27 import eu.etaxonomy.cdm.database.update.TableDroper;
28 import eu.etaxonomy.cdm.database.update.v31_33.SchemaUpdater_33_331;
29
30 /**
31 * @author a.mueller
32 * @created Jan 14, 2014
33 */
34 public class SchemaUpdater_331_34 extends SchemaUpdaterBase {
35
36 @SuppressWarnings("unused")
37 private static final Logger logger = Logger.getLogger(SchemaUpdater_331_34.class);
38 private static final String startSchemaVersion = "3.3.1.0.201401140000";
39 private static final String endSchemaVersion = "3.4.0.0.201407010000";
40
41 // ********************** FACTORY METHOD *************************************
42
43 public static SchemaUpdater_331_34 NewInstance() {
44 return new SchemaUpdater_331_34();
45 }
46
47 /**
48 * @param startSchemaVersion
49 * @param endSchemaVersion
50 */
51 protected SchemaUpdater_331_34() {
52 super(startSchemaVersion, endSchemaVersion);
53 }
54
55 @Override
56 protected List<ISchemaUpdaterStep> getUpdaterList() {
57
58 String stepName;
59 String tableName;
60 ISchemaUpdaterStep step;
61 String columnName;
62 String newColumnName;
63 String oldColumnName;
64
65 List<ISchemaUpdaterStep> stepList = new ArrayList<ISchemaUpdaterStep>();
66
67 //TODO H2 / PostGreSQL / SQL Server
68 //UserAccount unique
69 stepName = "Update User unique indexes";
70 tableName = "UserAccount";
71 columnName = "username";
72 step = UsernameConstraintUpdater.NewInstance(stepName, tableName, columnName);
73 stepList.add(step);
74
75 //TODO H2 / PostGreSQL / SQL Server
76 //PermissionGroup unique
77 stepName = "Update Group unique indexes";
78 tableName = "PermissionGroup";
79 columnName = "name";
80 step = UsernameConstraintUpdater.NewInstance(stepName, tableName, columnName);
81 stepList.add(step);
82
83 //TODO H2 / PostGreSQL / SQL Server
84 //GrantedAuthority unique
85 stepName = "Update User unique indexes";
86 tableName = "GrantedAuthorityImpl";
87 columnName = "authority";
88 step = UsernameConstraintUpdater.NewInstance(stepName, tableName, columnName);
89 stepList.add(step);
90
91 //TODO H2 / PostGreSQL / SQL Server
92 stepName = "Add label column to derived unit";
93 tableName = "SpecimenOrObservationBase";
94 columnName = "originalLabelInfo";
95 step = ColumnAdder.NewClobInstance(stepName, tableName, columnName, INCLUDE_AUDIT);
96 stepList.add(step);
97
98
99 //TODO test with data and H2 / PostGreSQL / SQL Server
100 //set default value to true where required
101 stepName = "Set publish to true if null";
102 String query = " UPDATE @@TaxonBase@@ " +
103 " SET publish = @TRUE@ " +
104 " WHERE DTYPE IN ('Synonym') AND publish IS NULL ";
105 step = SimpleSchemaUpdaterStep.NewAuditedInstance(stepName, query, "TaxonBase", 99);
106 stepList.add(step);
107
108
109 addIdentifierTables(stepList);
110
111
112 //remove series from Reference #4293
113 stepName = "Copy series to series part";
114 String sql = " UPDATE Reference r " +
115 " SET r.seriespart = r.series " +
116 " WHERE r.series is NOT NULL AND r.seriesPart IS NULL ";
117 step = SimpleSchemaUpdaterStep.NewAuditedInstance(stepName, sql, "Reference", 99);
118 stepList.add(step);
119
120 stepName = "Set series to NULL";
121 sql = " UPDATE Reference r " +
122 " SET r.series = NULL " +
123 " WHERE r.series = r.seriesPart ";
124 step = SimpleSchemaUpdaterStep.NewAuditedInstance(stepName, sql, "Reference", 99);
125 stepList.add(step);
126
127 //TODO check all series are null
128
129 stepName = "Remove series column";
130 tableName = "Reference";
131 oldColumnName = "series";
132 step = ColumnRemover.NewInstance(stepName, tableName, oldColumnName, INCLUDE_AUDIT);
133 stepList.add(step);
134
135 //authorTeam -> authorship
136 stepName = "Rename Reference.authorTeam column";
137 tableName = "Reference";
138 oldColumnName = "authorTeam_id";
139 newColumnName = "authorship_id";
140 step = ColumnNameChanger.NewIntegerInstance(stepName, tableName, oldColumnName, newColumnName, INCLUDE_AUDIT);
141 stepList.add(step);
142 //
143 //remove CDM_VIEW #4316
144 stepName = "Remove CDM_VIEW_CDM_VIEW table";
145 tableName = "CDM_VIEW_CDM_VIEW";
146 boolean ifExists = true;
147 step = TableDroper.NewInstance(stepName, tableName, ! INCLUDE_AUDIT, ifExists);
148 stepList.add(step);
149
150 stepName = "Remove CDM_VIEW table";
151 tableName = "CDM_VIEW";
152 ifExists = true;
153 step = TableDroper.NewInstance(stepName, tableName, ! INCLUDE_AUDIT, ifExists);
154 stepList.add(step);
155
156
157 //TODO not null on username, groupname and authority #4382
158
159 //DnaQuality #4434
160 //Identifier
161 stepName = "Create dna quality";
162 boolean includeCdmBaseAttributes = true;
163 tableName = "DnaQuality";
164 String[] columnNames = new String[]{"purificationmethod","concentration","ratioofabsorbance260_230", "ratioofabsorbance260_280","qualitycheckdate","concentrationunit_id","qualityterm_id"};
165 String[] columnTypes = new String[]{"string_255","double","double","double","datetime","int","int"};
166 String[] referencedTables = new String[]{null,null,null,null,null,"DefinedTermBase","DefinedTermBase"};
167 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
168 stepList.add(step);
169
170 //DnaQuality in TissueSample
171 //TODO H2 / PostGreSQL / SQL Server
172 stepName = "Add foreign key to dna quality";
173 tableName = "SpecimenOrObservationBase";
174 newColumnName = "dnaQuality_id";
175 boolean notNull = false;
176 String referencedTable = "DnaQuality";
177 step = ColumnAdder.NewIntegerInstance(stepName, tableName, newColumnName, INCLUDE_AUDIT, notNull, referencedTable);
178 stepList.add(step);
179
180 //time scope for classifications
181 //TODO H2 / PostGreSQL / SQL Server
182 stepName = "Add time scope (start) for classifications";
183 tableName = "Classification";
184 newColumnName = "timeperiod_start";
185 int length = 255;
186 step = ColumnAdder.NewStringInstance(stepName, tableName, newColumnName, length, INCLUDE_AUDIT);
187 stepList.add(step);
188
189 //TODO H2 / PostGreSQL / SQL Server
190 stepName = "Add time scope (end) for classifications";
191 tableName = "Classification";
192 newColumnName = "timeperiod_end";
193 length = 255;
194 step = ColumnAdder.NewStringInstance(stepName, tableName, newColumnName, length, INCLUDE_AUDIT);
195 stepList.add(step);
196
197 //TODO H2 / PostGreSQL / SQL Server
198 stepName = "Add time scope (freetext) for classifications";
199 tableName = "Classification";
200 newColumnName = "timeperiod_freetext";
201 length = 255;
202 step = ColumnAdder.NewStringInstance(stepName, tableName, newColumnName, length, INCLUDE_AUDIT);
203 stepList.add(step);
204
205 //Classification_GeoScope
206 stepName = "Create Classification_GeoScope table";
207 includeCdmBaseAttributes = false;
208 tableName = "Classification_GeoScope";
209 columnNames = new String[]{"Classification_id","geoScopes_id"};
210 columnTypes = new String[]{"int","int"};
211 referencedTables = new String[]{"Classification","DefinedTermBase"};
212 TableCreator creator = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
213 creator.setPrimaryKeyParams("Classification_id,geoScopes_id", "REV,Classification_id,geoScopes_id");
214 stepList.add(creator);
215
216 //Classification_Description
217 stepName = "Create Classification_Description table";
218 includeCdmBaseAttributes = false;
219 tableName = "Classification_Description";
220 columnNames = new String[]{"Classification_id","description_id","description_mapkey_id"};
221 columnTypes = new String[]{"int","int","int"};
222 referencedTables = new String[]{"Classification","LanguageString","DefinedTermBase"};
223 creator = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
224 creator.setPrimaryKeyParams("Classification_id", "REV,Classification_id,description_id,description_mapkey_id");
225 stepList.add(creator);
226
227 //Primer.sequence type #4139
228 stepName = "Add sequence string column to primer";
229 tableName = "Primer";
230 newColumnName = "sequence_string";
231 step = ColumnAdder.NewClobInstance(stepName, tableName, newColumnName,
232 INCLUDE_AUDIT);
233 stepList.add(step);
234
235 //Primer.sequence length #4139
236 stepName = "Add sequence length column to primer";
237 tableName = "Primer";
238 newColumnName = "sequence_length";
239 notNull = false;
240 referencedTable = null;
241 step = ColumnAdder.NewIntegerInstance(stepName, tableName, newColumnName, INCLUDE_AUDIT, null, notNull);
242 stepList.add(step);
243
244 //EntityValidationResult
245 stepName = "Create EntityValidationResult table";
246 includeCdmBaseAttributes = true;
247 tableName = "EntityValidationResult";
248 columnNames = new String[]{"crudeventtype","userfriendlydescription","userfriendlytypename",
249 "validatedentityclass","validatedentityid","validatedentityuuid"};
250 columnTypes = new String[]{"string_255","string_255","string_255","string_255","int","string_36"};
251 referencedTables = new String[]{null,null,null,null,null,null};
252 creator = TableCreator.NewNonVersionableInstance(stepName, tableName, columnNames, columnTypes, referencedTables, ! INCLUDE_AUDIT);
253 stepList.add(creator);
254
255 //EntityConstraintViolation
256 stepName = "Create EntityConstraintViolation table";
257 includeCdmBaseAttributes = true;
258 tableName = "EntityConstraintViolation";
259 columnNames = new String[]{"invalidvalue","message","propertypath","severity","userfriendlyfieldname",
260 "validator","entityvalidationresult_id"};
261 columnTypes = new String[]{"string_255","string_255","string_255","string_255","string_255","string_255","int"};
262 referencedTables = new String[]{null,null,null,null,null,null,"EntityValidationResult"};
263 creator = TableCreator.NewNonVersionableInstance(stepName, tableName, columnNames, columnTypes, referencedTables, ! INCLUDE_AUDIT);
264 stepList.add(creator);
265
266 //make OriginalSourceBase.sourceType allow NULL
267 stepName = "Remove NOT NULL from sourceType";
268 tableName = "OriginalSourceBase_AUD";
269 oldColumnName = "sourceType";
270 // query = "ALTER TABLE OriginalSourceBase_AUD " +
271 // " CHANGE COLUMN sourceType sourceType VARCHAR(4) NULL ";
272 step = ColumnTypeChanger.NewStringSizeInstance(stepName, tableName, oldColumnName, 4, ! INCLUDE_AUDIT);
273 stepList.add(step);
274
275
276 //remove sequence_id column //we do not move data as we do not expect data available yet #4139
277 //we put this to the end as it seems to fail with INNODB
278 stepName = "Remove sequence_id column from primer";
279 tableName = "Primer";
280 oldColumnName = "sequence_id";
281 step = ColumnRemover.NewInstance(stepName, tableName, oldColumnName, INCLUDE_AUDIT);
282 stepList.add(step);
283
284
285 // WE REMOVED THIS FROM THE SCRIPT BECAUSE IT FAILS WITH INNODB
286 // //change size of AgentBase_contact_urls.contact_urls_element #3920
287 // stepName = "Change length of AgentBase_contact_urls.contact_urls_element";
288 // tableName = "AgentBase_contact_urls";
289 // columnName = "contact_urls_element";
290 // step = ColumnTypeChanger.NewStringSizeInstance(stepName, tableName,
291 // columnName, 330, INCLUDE_AUDIT);
292 //// stepList.add(step);
293
294
295 return stepList;
296
297 }
298
299 private void addIdentifierTables(List<ISchemaUpdaterStep> stepList) {
300
301 //Identifier
302 String stepName = "Create Identifier table";
303 boolean includeCdmBaseAttributes = true;
304 String tableName = "Identifier";
305 String[] columnNames = new String[]{"identifier","identifiedObj_type", "identifiedObj_id","type_id"};
306 String[] columnTypes = new String[]{"string_800","string_255","int","int"};
307 String[] referencedTables = new String[]{null,null,null,"DefinedTermBase"};
308 TableCreator step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
309 stepList.add(step);
310
311 //AgentBase_Identifier
312 stepName = "Create AgentBase_Identifier table";
313 includeCdmBaseAttributes = false;
314 tableName = "AgentBase_Identifier";
315 columnNames = new String[]{"AgentBase_id","identifiers_id","sortIndex"};
316 columnTypes = new String[]{"int","int","int"};
317 referencedTables = new String[]{"AgentBase","Identifier",null};
318 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
319 step.setPrimaryKeyParams("AgentBase_id,identifiers_id", "REV,AgentBase_id,identifiers_id");
320 stepList.add(step);
321
322 //Classification_Identifier
323 stepName = "Create Classification_Identifier table";
324 includeCdmBaseAttributes = false;
325 tableName = "Classification_Identifier";
326 columnNames = new String[]{"Classification_id","identifiers_id","sortIndex"};
327 columnTypes = new String[]{"int","int","int"};
328 referencedTables = new String[]{"Classification","Identifier",null};
329 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
330 step.setPrimaryKeyParams("Classification_id,identifiers_id", "REV,Classification_id,identifiers_id");
331 stepList.add(step);
332
333 //Collection_Identifier
334 stepName = "Create Collection_Identifier table";
335 includeCdmBaseAttributes = false;
336 tableName = "Collection_Identifier";
337 columnNames = new String[]{"Collection_id","identifiers_id","sortIndex"};
338 columnTypes = new String[]{"int","int","int"};
339 referencedTables = new String[]{"Collection","Identifier",null};
340 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
341 step.setPrimaryKeyParams("Collection_id,identifiers_id", "REV,Collection_id,identifiers_id");
342 stepList.add(step);
343
344 //DefinedTermBase_Identifier
345 stepName = "Create DefinedTermBase_Identifier table";
346 includeCdmBaseAttributes = false;
347 tableName = "DefinedTermBase_Identifier";
348 columnNames = new String[]{"DefinedTermBase_id","identifiers_id","sortIndex"};
349 columnTypes = new String[]{"int","int","int"};
350 referencedTables = new String[]{"DefinedTermBase","Identifier",null};
351 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
352 step.setPrimaryKeyParams("DefinedTermBase_id,identifiers_id", "REV,DefinedTermBase_id,identifiers_id");
353 stepList.add(step);
354
355 //DescriptionBase_Identifier
356 stepName = "Create DescriptionBase_Identifier table";
357 includeCdmBaseAttributes = false;
358 tableName = "DescriptionBase_Identifier";
359 columnNames = new String[]{"DescriptionBase_id","identifiers_id","sortIndex"};
360 columnTypes = new String[]{"int","int","int"};
361 referencedTables = new String[]{"DescriptionBase","Identifier",null};
362 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
363 step.setPrimaryKeyParams("DescriptionBase_id,identifiers_id", "REV,DescriptionBase_id,identifiers_id");
364 stepList.add(step);
365
366 //FeatureTree_Identifier
367 stepName = "Create FeatureTree_Identifier table";
368 includeCdmBaseAttributes = false;
369 tableName = "FeatureTree_Identifier";
370 columnNames = new String[]{"FeatureTree_id","identifiers_id","sortIndex"};
371 columnTypes = new String[]{"int","int","int"};
372 referencedTables = new String[]{"FeatureTree","Identifier",null};
373 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
374 step.setPrimaryKeyParams("FeatureTree_id,identifiers_id", "REV,FeatureTree_id,identifiers_id");
375 stepList.add(step);
376
377 //Media_Identifier
378 stepName = "Create Media_Identifier table";
379 includeCdmBaseAttributes = false;
380 tableName = "Media_Identifier";
381 columnNames = new String[]{"Media_id","identifiers_id","sortIndex"};
382 columnTypes = new String[]{"int","int","int"};
383 referencedTables = new String[]{"Media","Identifier",null};
384 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
385 step.setPrimaryKeyParams("Media_id,identifiers_id", "REV,Media_id,identifiers_id");
386 stepList.add(step);
387
388 //PolytomousKey_Identifier
389 stepName = "Create PolytomousKey_Identifier table";
390 includeCdmBaseAttributes = false;
391 tableName = "PolytomousKey_Identifier";
392 columnNames = new String[]{"PolytomousKey_id","identifiers_id","sortIndex"};
393 columnTypes = new String[]{"int","int","int"};
394 referencedTables = new String[]{"PolytomousKey","Identifier",null};
395 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
396 step.setPrimaryKeyParams("PolytomousKey_id,identifiers_id", "REV,PolytomousKey_id,identifiers_id");
397 stepList.add(step);
398
399 //Reference_Identifier
400 stepName = "Create Reference_Identifier table";
401 includeCdmBaseAttributes = false;
402 tableName = "Reference_Identifier";
403 columnNames = new String[]{"Reference_id","identifiers_id","sortIndex"};
404 columnTypes = new String[]{"int","int","int"};
405 referencedTables = new String[]{"Reference","Identifier",null};
406 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
407 step.setPrimaryKeyParams("Reference_id,identifiers_id", "REV,Reference_id,identifiers_id");
408 stepList.add(step);
409
410 //SpecimenOrObservationBase_Identifier
411 stepName = "Create SpecimenOrObservationBase_Identifier table";
412 includeCdmBaseAttributes = false;
413 tableName = "SpecimenOrObservationBase_Identifier";
414 columnNames = new String[]{"SpecimenOrObservationBase_id","identifiers_id","sortIndex"};
415 columnTypes = new String[]{"int","int","int"};
416 referencedTables = new String[]{"SpecimenOrObservationBase","Identifier",null};
417 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
418 step.setPrimaryKeyParams("SpecimenOrObservationBase_id,identifiers_id", "REV,SpecimenOrObservationBase_id,identifiers_id");
419 stepList.add(step);
420
421 //TaxonBase_Identifier
422 stepName = "Create TaxonBase_Identifier table";
423 includeCdmBaseAttributes = false;
424 tableName = "TaxonBase_Identifier";
425 columnNames = new String[]{"TaxonBase_id","identifiers_id","sortIndex"};
426 columnTypes = new String[]{"int","int","int"};
427 referencedTables = new String[]{"TaxonBase","Identifier",null};
428 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
429 step.setPrimaryKeyParams("TaxonBase_id,identifiers_id", "REV,TaxonBase_id,identifiers_id");
430 stepList.add(step);
431
432 //TaxonNameBase_Identifier
433 stepName = "Create TaxonNameBase_Identifier table";
434 includeCdmBaseAttributes = false;
435 tableName = "TaxonNameBase_Identifier";
436 columnNames = new String[]{"TaxonNameBase_id","identifiers_id","sortIndex"};
437 columnTypes = new String[]{"int","int","int"};
438 referencedTables = new String[]{"TaxonNameBase","Identifier",null};
439 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
440 step.setPrimaryKeyParams("TaxonNameBase_id,identifiers_id", "REV,TaxonNameBase_id,identifiers_id");
441 stepList.add(step);
442
443 //TermVocabulary_Identifier
444 stepName = "Create TermVocabulary_Identifier table";
445 includeCdmBaseAttributes = false;
446 tableName = "TermVocabulary_Identifier";
447 columnNames = new String[]{"TermVocabulary_id","identifiers_id","sortIndex"};
448 columnTypes = new String[]{"int","int","int"};
449 referencedTables = new String[]{"TermVocabulary","Identifier",null};
450 step = TableCreator.NewInstance(stepName, tableName, columnNames, columnTypes, referencedTables, INCLUDE_AUDIT, includeCdmBaseAttributes);
451 step.setPrimaryKeyParams("TermVocabulary_id,identifiers_id", "REV,TermVocabulary_id,identifiers_id");
452 stepList.add(step);
453
454 }
455
456 @Override
457 public ISchemaUpdater getNextUpdater() {
458 return SchemaUpdater_34_341.NewInstance();
459 }
460
461 @Override
462 public ISchemaUpdater getPreviousUpdater() {
463 return SchemaUpdater_33_331.NewInstance();
464 }
465
466 }