Changeset 1657 for trunk/grails-app/conf/DatabaseUpgrade.groovy
- Timestamp:
- Mar 22, 2011, 2:03:03 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/conf/DatabaseUpgrade.groovy
r1635 r1657 36 36 makeMappingColumnValueNullable(sql, db) // r1525 37 37 alterStudyAndAssay(sql, db) // r1594 38 fixDateCreatedAndLastUpdated(sql, db) 38 39 } 39 40 … … 254 255 } 255 256 } 257 258 /** 259 * make sure all date_created and last_updated columns are NOT nullable, and 260 * set values to now() of they are null 261 * @param sql 262 * @param db 263 */ 264 public static void fixDateCreatedAndLastUpdated(sql, db) { 265 // are we running PostgreSQL? 266 if (db == "org.postgresql.Driver") { 267 // see if we need to modify anything? 268 sql.eachRow("SELECT table_name,column_name FROM information_schema.columns WHERE column_name IN ('last_updated', 'date_created') AND is_nullable='YES'") { row -> 269 // grom what we are doing 270 if (String.metaClass.getMetaMethod("grom")) "fixing nullable for ${row.table_name}:${row.column_name}".grom() 271 272 // fix database 273 try { 274 // setting all null values to now() 275 sql.execute(sprintf("UPDATE %s SET %s=now() WHERE %s IS NULL",row.table_name,row.column_name,row.column_name)) 276 277 // and alter the table to disallow null values 278 sql.execute(sprintf("ALTER TABLE %s ALTER COLUMN %s SET NOT NULL",row.table_name,row.column_name)) 279 } catch (Exception e) { 280 println "fixDateCreatedAndLastUpdated database upgrade failed: " + e.getMessage() 281 } 282 } 283 } 284 } 256 285 }
Note: See TracChangeset
for help on using the changeset viewer.