Changeset 1365


Ignore:
Timestamp:
Jan 11, 2011, 6:26:47 PM (12 years ago)
Author:
work@…
Message:
  • resolves issue #269, performing database upgrade if required (change study::description to text)
File:
1 edited

Legend:

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

    r1255 r1365  
    11import groovy.sql.Sql
    22import dbnp.studycapturing.Study
     3import org.codehaus.groovy.grails.commons.ConfigurationHolder
    34
    45/**
     
    2324                groovy.sql.Sql sql = new groovy.sql.Sql(dataSource)
    2425
     26                // get configuration
     27                def config = ConfigurationHolder.config
     28                def db = config.dataSource.driverClassName
     29
    2530                // execute per-change check and upgrade code
    26                 changeStudyDescription(sql)                     // r1245 / r1246
     31                changeStudyDescription(sql, db)                 // r1245 / r1246
     32                changeStudyDescriptionToText(sql, db)   // r1327
    2733        }
    2834
     
    3137         * @param sql
    3238         */
    33         public static void changeStudyDescription(sql) {
     39        public static void changeStudyDescription(sql, db) {
    3440                // check if we need to perform this upgrade
    3541                if (sql.firstRow("SELECT count(*) as total FROM template_field WHERE templatefieldentity='dbnp.studycapturing.Study' AND templatefieldname='Description'").total > 0) {
     
    7278                }
    7379        }
     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        }
    74103}
Note: See TracChangeset for help on using the changeset viewer.