Changeset 1776


Ignore:
Timestamp:
Apr 19, 2011, 4:35:56 PM (12 years ago)
Author:
work@…
Message:
  • resolves #401, study title should be unique
Location:
trunk/grails-app
Files:
2 edited

Legend:

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

    r1689 r1776  
    3838                fixDateCreatedAndLastUpdated(sql, db)
    3939                dropAssayModulePlatform(sql, db)                        // r1689
     40                makeStudyTitleUnique(sql, db)                           // #401
    4041        }
    4142
     
    306307                }
    307308        }
     309
     310        /**
     311         * Make sure the study title is unique
     312         * @param sql
     313         * @param db
     314         */
     315        public static void makeStudyTitleUnique(sql, db) {
     316                def titleCount,title,newTitle
     317
     318                // are we running postgreSQL?
     319                if (db == "org.postgresql.Driver") {
     320                        // yes, find all duplicate study titles
     321                        sql.eachRow("SELECT DISTINCT a.title FROM Study a WHERE (SELECT count(*) FROM Study b WHERE b.title=a.title) > 1") { row ->
     322                                // grom what we are doing
     323                                if (String.metaClass.getMetaMethod("grom")) "making study title '${row.title}' unique".grom()
     324
     325                                // iterate through studies that use this duplicate title
     326                                titleCount      = 1
     327                                title           = row.title.replace("'","\'")
     328                                sql.eachRow(sprintf("SELECT id FROM Study WHERE title='%s'", title)) { studyRow ->
     329                                        newTitle = "${title} - ${titleCount}"
     330                                        sql.execute(sprintf("UPDATE Study SET title='%s' WHERE id=%d",newTitle,studyRow.id))
     331                                        titleCount++
     332                                }
     333                        }
     334                }
     335        }
    308336}
  • trunk/grails-app/domain/dbnp/studycapturing/Study.groovy

    r1775 r1776  
    5252
    5353        static constraints = {
    54                 title(nullable:false, blank: false, maxSize: 255)
     54                title(nullable:false, blank: false, unique:true, maxSize: 255)
    5555                owner(nullable: true, blank: true)
    5656                code(nullable: true, blank: true, unique: true, maxSize: 255)
Note: See TracChangeset for help on using the changeset viewer.