1 | import groovy.sql.Sql |
---|
2 | import dbnp.studycapturing.Study |
---|
3 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
4 | |
---|
5 | /** |
---|
6 | * A script to automatically perform database changes |
---|
7 | * |
---|
8 | * @Author Jeroen Wesbeek |
---|
9 | * @Since 20101209 |
---|
10 | * |
---|
11 | * Revision information: |
---|
12 | * $Rev: 1365 $ |
---|
13 | * $Author: business@keesvanbochove.nl $ |
---|
14 | * $Date: 2011-01-11 17:26:47 +0000 (di, 11 jan 2011) $ |
---|
15 | */ |
---|
16 | class DatabaseUpgrade { |
---|
17 | /** |
---|
18 | * handle database upgrades |
---|
19 | * |
---|
20 | * @param dataSource |
---|
21 | */ |
---|
22 | public static void handleUpgrades(dataSource) { |
---|
23 | // get a sql instance |
---|
24 | groovy.sql.Sql sql = new groovy.sql.Sql(dataSource) |
---|
25 | |
---|
26 | // get configuration |
---|
27 | def config = ConfigurationHolder.config |
---|
28 | def db = config.dataSource.driverClassName |
---|
29 | |
---|
30 | // execute per-change check and upgrade code |
---|
31 | changeStudyDescription(sql, db) // r1245 / r1246 |
---|
32 | changeStudyDescriptionToText(sql, db) // r1327 |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * execute database change r1245 / r1246 if required |
---|
37 | * @param sql |
---|
38 | */ |
---|
39 | public static void changeStudyDescription(sql, db) { |
---|
40 | // check if we need to perform this upgrade |
---|
41 | if (sql.firstRow("SELECT count(*) as total FROM template_field WHERE templatefieldentity='dbnp.studycapturing.Study' AND templatefieldname='Description'").total > 0) { |
---|
42 | // grom that we are performing the upgrade |
---|
43 | "performing database upgrade: study description".grom() |
---|
44 | |
---|
45 | // database upgrade required |
---|
46 | try { |
---|
47 | // get the template field id |
---|
48 | def id = sql.firstRow("SELECT id FROM template_field WHERE templatefieldentity='dbnp.studycapturing.Study' AND templatefieldname='Description'").id |
---|
49 | |
---|
50 | // iterate through all obsolete study descriptions |
---|
51 | sql.eachRow("SELECT study_id, template_text_fields_elt as description FROM study_template_text_fields WHERE template_text_fields_idx='Description'") { row -> |
---|
52 | // migrate the template description to the study object itself |
---|
53 | // so we don't have to bother with sql injections, etc |
---|
54 | def study = Study.findById( row.study_id ) |
---|
55 | study.setFieldValue('description', row.description) |
---|
56 | if (!(study.validate() && study.save())) { |
---|
57 | throw new Exception("could not save study with id ${row.study_id}") |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | // delete all obsolete descriptions |
---|
62 | sql.execute("DELETE FROM study_template_text_fields WHERE template_text_fields_idx='Description'") |
---|
63 | |
---|
64 | // find all template id's where this field is used |
---|
65 | sql.eachRow("SELECT DISTINCT template_fields_id, fields_idx FROM template_template_field WHERE template_field_id=${id}") { row -> |
---|
66 | // delete the template_template_field reference |
---|
67 | sql.execute("DELETE FROM template_template_field WHERE template_field_id=${id} AND template_fields_id=${row.template_fields_id}") |
---|
68 | |
---|
69 | // and lower the idx-es of the remaining fields |
---|
70 | sql.execute("UPDATE template_template_field SET fields_idx=fields_idx-1 WHERE fields_idx>${row.fields_idx} AND template_fields_id=${row.template_fields_id}") |
---|
71 | } |
---|
72 | |
---|
73 | // and delete the obsolete template field |
---|
74 | sql.execute("DELETE FROM template_field WHERE id=${id}") |
---|
75 | } catch (Exception e) { |
---|
76 | "changeStudyDescription database upgrade failed: " + e.getMessage() |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * execute database change r1327 if required |
---|
83 | * @param sql |
---|
84 | */ |
---|
85 | public static void changeStudyDescriptionToText(sql, db) { |
---|
86 | // are we running postgreSQL ? |
---|
87 | if (db == "org.postgresql.Driver") { |
---|
88 | // check if column 'description' in table 'study' is not of type 'text' |
---|
89 | if (sql.firstRow("SELECT count(*) as total FROM information_schema.columns WHERE columns.table_schema::text = 'public'::text AND columns.table_name='study' AND column_name='description' AND data_type != 'text'").total > 0) { |
---|
90 | // grom that we are performing the upgrade |
---|
91 | "performing database upgrade: study description to text".grom() |
---|
92 | |
---|
93 | // database upgrade required |
---|
94 | try { |
---|
95 | // change the datatype of study::description to text |
---|
96 | sql.execute("ALTER TABLE study ALTER COLUMN description TYPE text") |
---|
97 | } catch (Exception e) { |
---|
98 | "changeStudyDescriptionToText database upgrade failed: " + e.getMessage() |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|