Ignore:
Timestamp:
Mar 22, 2011, 2:03:03 PM (12 years ago)
Author:
work@…
Message:
  • resolved issue #372
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/conf/DatabaseUpgrade.groovy

    r1635 r1657  
    3636                makeMappingColumnValueNullable(sql, db)         // r1525
    3737                alterStudyAndAssay(sql, db)                                     // r1594
     38                fixDateCreatedAndLastUpdated(sql, db)
    3839        }
    3940
     
    254255                }
    255256        }
     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        }
    256285}
Note: See TracChangeset for help on using the changeset viewer.