Updated after second round of review comments

- Move the artifactDAO from flowDAO. This will make code "clean" and
    @maheshkelkar happy
  - Fixes #163
This commit is contained in:
Giri Dandu 2015-05-08 11:30:26 -04:00
parent 58ad5ba062
commit 0b468ca070
4 changed files with 41 additions and 19 deletions

View File

@ -2,6 +2,7 @@ package deploydb
import com.codahale.metrics.annotation.Timed
import com.google.common.collect.ImmutableMultimap
import deploydb.models.Artifact
import io.dropwizard.servlets.tasks.Task
import org.slf4j.Logger
import org.slf4j.LoggerFactory
@ -33,7 +34,14 @@ class ModelCleanupTask extends Task {
this.workFlow.deployDBApp.withHibernateSession() {
try {
this.workFlow.flowDAO.deleteFlowByArtifactNameGroupVersion(group, name, version)
// Lets fetch the artifact using name, group and version
Artifact artifact = this.workFlow.artifactDAO.findArtifactByGroupNameVersion(
group, name,version)
if(artifact == null) {
return
}
// Now delete flow using the artifact id
this.workFlow.flowDAO.deleteFlowByArtifactId(artifact.id)
output.println("Done!")
} catch (Exception e) {
logger.error("Cleanup of the model failed with an exception: ", e)

View File

@ -69,9 +69,27 @@ class ArtifactDAO extends AbstractDAO<Artifact> {
return artifacts
}
boolean artifactExists(String group, String name, String version) {
/**
* Fetch the artifact using group, name, version
* @param group Artifact group
* @param name Artifact name
* @param version Artifact version
* @return Artifact returned by the search if available otherwise null
*/
Artifact findArtifactByGroupNameVersion(String group, String name, String version) {
return criteria().add(Restrictions.eq('group', group))
.add(Restrictions.eq('name', name))
.add(Restrictions.eq('version', version)).uniqueResult() == null ? false: true
.add(Restrictions.eq('name', name))
.add(Restrictions.eq('version', version)).uniqueResult()
}
/**
* Check if the artifact exists based group, name, version
* @param group Artifact group
* @param name Artifact name
* @param version Artifact version
* @return true if Artifact exists otherwise false
*/
boolean artifactExists(String group, String name, String version) {
return findArtifactByGroupNameVersion(group, name, version) == null ? false: true
}
}

View File

@ -1,6 +1,5 @@
package deploydb.dao
import deploydb.models.Artifact
import deploydb.models.Flow
import groovy.transform.InheritConstructors
import io.dropwizard.hibernate.AbstractDAO
@ -13,20 +12,14 @@ import org.hibernate.criterion.Restrictions
@InheritConstructors
class FlowDAO extends AbstractDAO<Flow> {
void deleteFlowByArtifactNameGroupVersion(String group,
String name,
String version) {
/**
* Delete the flow based on Artifact id. This function will delete the assocaited models -
* Artifact, Deployments and PromotionResults
* @param id Artifact id of the flow
*/
void deleteFlowByArtifactId(long id) {
Session session = currentSession()
// We can't find the flow using artifact name, group and version, so
// we need to fetch the artifact, and use artifact id to fetch the flow
ArtifactDAO artifactDAO = new ArtifactDAO(currentSession().getSessionFactory())
Artifact artifact = artifactDAO.criteria().add(Restrictions.eq('group', group))
.add(Restrictions.eq('name', name)).add(Restrictions.eq('version', version)).uniqueResult()
if(artifact == null) {
return
}
Flow flow = criteria().add(Restrictions.eq('artifact.id', artifact.id)).uniqueResult()
Flow flow = criteria().add(Restrictions.eq('artifact.id', id)).uniqueResult()
// now delete the found flow
session.delete(flow)

View File

@ -3,6 +3,7 @@ package deploydb.models
import deploydb.IntegrationTestAppHelper
import deploydb.Status
import deploydb.dao.PromotionResultDAO
import spock.lang.Ignore
import spock.lang.Specification
import dropwizardintegtest.IntegrationModelHelper
import dropwizardintegtest.IntegrationRestApiClient
@ -333,8 +334,10 @@ class FlowCleanupSpec extends Specification {
// remove the flow
integAppHelper.runner.getApplication().withHibernateSession {
Artifact artifact = integAppHelper.runner.getApplication().workFlow.artifactDAO.
findArtifactByGroupNameVersion("basic.group.1", "bg1", "1.2.345")
integAppHelper.runner.getApplication().workFlow.flowDAO.
deleteFlowByArtifactNameGroupVersion("basic.group.1", "bg1", "1.2.345")
deleteFlowByArtifactId(artifact.id)
}