Changeset 1595 for trunk/grails-app
- Timestamp:
- Mar 7, 2011, 6:31:52 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/conf/DatabaseUpgrade.groovy
r1594 r1595 6 6 * A script to automatically perform database changes 7 7 * 8 * @Author 9 * @Since 8 * @Author Jeroen Wesbeek 9 * @Since 20101209 10 10 * 11 11 * Revision information: … … 32 32 changeStudyDescriptionToText(sql, db) // r1327 33 33 changeTemplateTextFieldSignatures(sql, db) // prevent Grails issue, see http://jira.codehaus.org/browse/GRAILS-6754 34 setAssayModuleDefaultValues(sql, db) // 149035 36 alterStudyAndAssay(sql, db) // 159434 setAssayModuleDefaultValues(sql, db) // r1490 35 dropMappingColumnNameConstraint(sql, db) 36 alterStudyAndAssay(sql, db) // r1594 37 37 } 38 38 … … 57 57 // migrate the template description to the study object itself 58 58 // so we don't have to bother with sql injections, etc 59 def study = Study.findById( row.study_id)59 def study = Study.findById(row.study_id) 60 60 study.setFieldValue('description', row.description) 61 61 if (!(study.validate() && study.save())) { … … 79 79 sql.execute("DELETE FROM template_field WHERE id=${id}") 80 80 } catch (Exception e) { 81 "changeStudyDescription database upgrade failed: " + e.getMessage()81 println "changeStudyDescription database upgrade failed: " + e.getMessage() 82 82 } 83 83 } … … 102 102 sql.execute("ALTER TABLE study ALTER COLUMN description TYPE text") 103 103 } catch (Exception e) { 104 "changeStudyDescriptionToText database upgrade failed: " + e.getMessage()104 println "changeStudyDescriptionToText database upgrade failed: " + e.getMessage() 105 105 } 106 106 } … … 119 119 // check if any TEXT template fields are of type 'text' 120 120 sql.eachRow("SELECT columns.table_name as tablename FROM information_schema.columns WHERE columns.table_schema::text = 'public'::text AND column_name='template_text_fields_elt' AND data_type != 'text';") 121 { row -> 122 "performing database upgrade: ${row.tablename} template_text_fields_string/elt to text".grom() 123 try { 124 // change the datatype of text fields to text 125 sql.execute(sprintf("ALTER TABLE %s ALTER COLUMN template_text_fields_elt TYPE text", row.tablename)) 126 sql.execute(sprintf("ALTER TABLE %s ALTER COLUMN template_text_fields_string TYPE text", row.tablename)) 127 128 } catch (Exception e) { 129 "changeTemplateTextFieldSignatures database upgrade failed: " + e.getMessage() 130 } 131 } 132 } 133 } 134 135 /** 136 * The fields 'notify' and 'openInFrame' have been added to AssayModule. However, there 137 * seems to be no method to setting the default values of these fields in the database. They 138 * are set to NULL by default, so all existing fields have 'NULL' set. 139 * This method sets the default values 140 * @param sql 141 * @param db 142 */ 143 public static void setAssayModuleDefaultValues(sql, db) { 144 "performing database upgrade: assay_module default values for boolean fields".grom() 145 146 // are we running postgreSQL ? 147 if (db == "org.postgresql.Driver") { 148 try { 149 sql.execute("UPDATE assay_module SET notify = FALSE WHERE notify IS NULL") 150 } catch (Exception e) { 151 println "setAssayModuleDefaultValues notify field couldn't be set to default value: " + e.getMessage() 152 } 153 try { 154 sql.execute("UPDATE assay_module SET open_in_frame = TRUE WHERE open_in_frame IS NULL") 155 } catch (Exception e) { 156 println "setAssayModuleDefaultValues openInFrame field couldn't be set to default value: " + e.getMessage() 157 println "Maybe gdt plugin is not updated yet after revision 109" 158 } 159 } 160 161 // Are we running MySQL 162 if( db == "com.mysql.jdbc.Driver" ) { 163 try { 164 sql.execute("UPDATE assay_module SET notify = 0 WHERE notify IS NULL") 165 } catch (Exception e) { 166 println "setAssayModuleDefaultValues notify field couldn't be set to default value: " + e.getMessage() 167 } 168 try { 169 sql.execute("UPDATE assay_module SET open_in_frame = 1 WHERE open_in_frame IS NULL") 170 } catch (Exception e) { 171 println "setAssayModuleDefaultValues openInFrame field couldn't be set to default value: " + e.getMessage() 172 println "Maybe gdt plugin is not updated yet after revision 109" 173 } 174 } 175 } 176 177 /** 121 { row -> 122 "performing database upgrade: ${row.tablename} template_text_fields_string/elt to text".grom() 123 try { 124 // change the datatype of text fields to text 125 sql.execute(sprintf("ALTER TABLE %s ALTER COLUMN template_text_fields_elt TYPE text", row.tablename)) 126 sql.execute(sprintf("ALTER TABLE %s ALTER COLUMN template_text_fields_string TYPE text", row.tablename)) 127 128 } catch (Exception e) { 129 println "changeTemplateTextFieldSignatures database upgrade failed: " + e.getMessage() 130 } 131 } 132 } 133 } 134 135 /** 136 * The fields 'notify' and 'openInFrame' have been added to AssayModule. However, there 137 * seems to be no method to setting the default values of these fields in the database. They 138 * are set to NULL by default, so all existing fields have 'NULL' set. 139 * This method sets the default values 140 * @param sql 141 * @param db 142 */ 143 public static void setAssayModuleDefaultValues(sql, db) { 144 // do we need to perform this upgrade? 145 if ((db == "org.postgresql.Driver" || db == "com.mysql.jdbc.Driver") && 146 (sql.firstRow("SELECT * FROM assay_module WHERE notify IS NULL") || sql.firstRow("SELECT * FROM assay_module WHERE open_in_frame IS NULL")) 147 ) { 148 "performing database upgrade: assay_module default values for boolean fields".grom() 149 150 try { 151 sql.execute("UPDATE assay_module SET notify=" + ((db == "org.postgresql.Driver") ? 'FALSE' : '0') + " WHERE notify IS NULL") 152 } catch (Exception e) { 153 println "setAssayModuleDefaultValues database upgrade failed, notify field couldn't be set to default value: " + e.getMessage() 154 } 155 156 // set open_in_frame? 157 try { 158 sql.execute("UPDATE assay_module SET open_in_frame=" + ((db == "org.postgresql.Driver") ? 'TRUE' : '1') + " WHERE open_in_frame IS NULL") 159 } catch (Exception e) { 160 // Maybe gdt plugin is not updated yet after revision 109 ? 161 println "setAssayModuleDefaultValues database upgrade failed, openInFrame field couldn't be set to default value: " + e.getMessage() 162 } 163 } 164 } 165 166 /** 178 167 * Drop the unique constraint for the "name" column in the MappingColumn domain 179 * 180 * @param sql 181 * @param db 182 */ 183 public static void dropMappingColumnNameConstraint(sql, db) { 184 // are we running postgreSQL ? 185 if (db == "org.postgresql.Driver") { 186 try { 187 // Check if constraint still exists 188 if (sql.firstRow("SELECT * FROM pg_constraint WHERE contype='mapping_column_name_key'")) { 189 println "performing database upgrade: mapping column name constraint" 190 sql.execute("ALTER TABLE mapping_column DROP CONSTRAINT mapping_column_name_key") 191 } 192 } catch (Exception e) { 193 println "changeMappingColumnNameConstraint `name` field unique constraint couldn't be dropped: " + e.getMessage() 194 } 195 } 196 } 197 198 /** 199 * The field study.code has been set to be nullable 200 * The field assay.externalAssayId has been removed 201 * @param sql 202 * @param db 203 */ 204 public static void alterStudyAndAssay(sql, db) { 205 // are we running postgreSQL ? 206 if (db == "org.postgresql.Driver") { 207 try { 208 sql.execute("ALTER TABLE assay DROP COLUMN external_assayid") 209 } catch (Exception e) { 210 println "alterStudyAndAssay externalAssayId could not be removed from assay: " + e.getMessage() 211 } 212 try { 213 sql.execute("ALTER TABLE study ALTER COLUMN code DROP NOT NULL") 214 } catch (Exception e) { 215 println "alterStudyAndAssay study.code could not be set to accept null values: " + e.getMessage() 216 } 217 } 218 219 // Load all studies and save them again. This prevents errors on saving later 220 Study.list().each { 221 it.save(); 222 } 223 } 224 168 * 169 * @param sql 170 * @param db 171 */ 172 public static void dropMappingColumnNameConstraint(sql, db) { 173 // are we running postgreSQL ? 174 if (db == "org.postgresql.Driver") { 175 if (sql.firstRow("SELECT * FROM pg_constraint WHERE contype='mapping_column_name_key'")) { 176 "performing database upgrade: mapping column name constraint".grom() 177 try { 178 // Check if constraint still exists 179 sql.execute("ALTER TABLE mapping_column DROP CONSTRAINT mapping_column_name_key") 180 } catch (Exception e) { 181 println "dropMappingColumnNameConstraint database upgrade failed, `name` field unique constraint couldn't be dropped: " + e.getMessage() 182 } 183 } 184 } 185 } 186 187 /** 188 * The field study.code has been set to be nullable 189 * The field assay.externalAssayId has been removed 190 * @param sql 191 * @param db 192 */ 193 public static void alterStudyAndAssay(sql, db) { 194 def updated = false 195 196 // are we running postgreSQL ? 197 if (db == "org.postgresql.Driver") { 198 // see if table assay contains a column external_assayid 199 if (sql.firstRow("SELECT * FROM information_schema.columns WHERE columns.table_name='assay' AND columns.column_name='external_assayid'")) { 200 "performing database upgrade: dropping column 'external_assayid' from table 'assay'".grom() 201 202 try { 203 sql.execute("ALTER TABLE assay DROP COLUMN external_assayid") 204 updated = true 205 } catch (Exception e) { 206 println "alterStudyAndAssay database upgrade failed, externalAssayId could not be removed from assay: " + e.getMessage() 207 } 208 209 } 210 211 // see if table study contains a column code which is not nullable 212 if (sql.firstRow("SELECT * FROM information_schema.columns WHERE columns.table_name='study' AND columns.column_name='code' AND is_nullable='NO'")) { 213 "performing database upgrade: dropping column 'code' from table 'study'".grom() 214 215 try { 216 sql.execute("ALTER TABLE study ALTER COLUMN code DROP NOT NULL") 217 updated = true 218 } catch (Exception e) { 219 println "alterStudyAndAssay database upgrade failed, study.code could not be set to accept null values: " + e.getMessage() 220 } 221 } 222 223 // Load all studies and save them again. This prevents errors on saving later 224 if (updated) { 225 "re-saving studies...".grom() 226 227 Study.list().each { study -> 228 "re-saving study: ${study}".grom() 229 study.save() 230 } 231 } 232 } 233 } 225 234 }
Note: See TracChangeset
for help on using the changeset viewer.