Skip to content

Commit

Permalink
Added postUpdate hook to delete FileUpload with null owner (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtnordstrom authored and sosguthorpe committed Aug 1, 2019
1 parent b00f5b4 commit 81cb288
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions service/grails-app/domain/org/olf/general/FileUpload.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.olf.general
import grails.gorm.MultiTenant
import grails.gorm.multitenancy.Tenants

class FileUpload implements MultiTenant<FileUpload> {

Expand All @@ -13,17 +14,41 @@ class FileUpload implements MultiTenant<FileUpload> {

static constraints = {
fileContentBytes nullable: true
fileContentType nullable: true, blank: false
fileContentType nullable: true
lastModified nullable: true
'owner' nullable: true
owner nullable: true
}

static mapping = {
id column: 'fu_id', generator: 'uuid2', length: 36
fileContentBytes column: 'fu_bytes', sqlType: 'longblob'
fileName column: 'fu_filename'
fileSize column: 'fu_filesize'
lastModified column: 'fu_last_mod'
owner column: 'fu_owner', cascade: 'save-update'
lastModified column: 'fu_last_mod'
owner column: 'fu_owner', type: 'string', length: 36
}

def afterUpdate() {
log.info("afterUpdate() for FileUpload")
final Serializable currentTenantId = Tenants.currentId()
Tenants.withId(currentTenantId) {
try {
def deleteList = []
def fuList = FileUpload.findAllWhere(owner: null)
fuList.each {
FileUpload upload = it
if(upload.owner == null) {
deleteList.add(upload)
}
}
deleteList.each {
log.info("Deleting fileUpload with id of ${it.id}")
it.delete()
}
} catch(Exception e) {
log.error("Error trying to delete ownerless fileUpload objects: ${e.getMessage()}")
}
}
}

}

0 comments on commit 81cb288

Please sign in to comment.