I’d like to version a grails domain class by DateTime such that:
- each primary ID + DateTime property results in a new DB row (i.e. a new DB row per version)
- the newest version can be found without having to specify the DateTime value
My knowledge of grails is a little limited right now and I don’t understand how I can achieve this.
Consider the following domain class as an example:
import org.joda.time.DateTime
class Page {
String pageId
DateTime theVersion
String content
static constraints = {
pageId(nullable:false, blank:false, unique:true, matches:"[a-zA-Z\\._-]+")
content(nullable:false, blank:false)
}
static mapping = {
content(type:"text")
}
}
-
What changes are needed to ensure a new DB row is inserted per version?
I’m assuming some form of constraint is required such that pageId+theVersion is unique but I don’t know how to express this in the GORM DSL.
-
How might I get the most recent version without knowing the relevant DateTime value?
I’m envisaging something like:
Page.findByPageIdAndTheVersionLessThanEquals('uniquePageId', new DateTime())I expect this would find many objects not just one. I’d like to be able to also express the equivalent of
ORDER BY theVersion DESC LIMIT 0,1
new Page(), not withget()– a new record will be inserted.To assure uniqueness, put into constraints:
theVersion(unique: 'pageId')Page.findByPageId(pageId, [sort: 'theVersion', order: 'desc', max: 1])You can utilize Grails’
dateCreatedimplicit timestamping feature – it will even work with joda-time plugin.OTOH, why don’t you utilize Grails’ built-in
versionfield? It provides you some features out of box and takes care for optimistic locking.