Skip to content

Commit

Permalink
feat: enhance GetJobSpecifications to support include deleted jobs fi…
Browse files Browse the repository at this point in the history
…lter
  • Loading branch information
arinda-arif committed Jan 10, 2023
1 parent 2f874ac commit 337c53c
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 158 deletions.
1 change: 1 addition & 0 deletions api/handler/v1beta1/job_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ func (sv *JobSpecServiceServer) GetJobSpecifications(ctx context.Context, req *p
JobName: req.GetJobName(),
ResourceDestination: req.GetResourceDestination(),
NamespaceName: req.GetNamespaceName(),
IncludeDeleted: req.GetIncludeDeleted(),
}
jobSpecs, err := sv.jobSvc.GetByFilter(ctx, jobSpecFilter)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions api/handler/v1beta1/job_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ func (s *JobSpecServiceServerTestSuite) TestGetJobSpecification_Fail_JobServiceG
}

func (s *JobSpecServiceServerTestSuite) TestGetJobSpecifications_Success() {
req := &pb.GetJobSpecificationsRequest{JobName: "job-1", NamespaceName: "namespace-1"}
jobSpecFilter := models.JobSpecFilter{JobName: req.GetJobName(), NamespaceName: req.GetNamespaceName()}
req := &pb.GetJobSpecificationsRequest{JobName: "job-1", NamespaceName: "namespace-1", IncludeDeleted: true}
jobSpecFilter := models.JobSpecFilter{JobName: req.GetJobName(), NamespaceName: req.GetNamespaceName(), IncludeDeleted: req.GetIncludeDeleted()}

execUnit1 := new(mock.YamlMod)
execUnit1.On("PluginInfo").Return(&models.PluginInfoResponse{Name: "task"}, nil)
Expand Down
72 changes: 46 additions & 26 deletions internal/store/postgres/job_spec_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,53 +32,68 @@ func NewJobSpecRepository(db *gorm.DB, adapter *JobSpecAdapter) (store.JobSpecRe
}, nil
}

func (j jobSpecRepository) GetAllByProjectName(ctx context.Context, projectName string) ([]models.JobSpec, error) {
var jobs []Job
if err := j.db.WithContext(ctx).
func (j jobSpecRepository) GetAllByProjectName(ctx context.Context, projectName string, includeDeleted bool) ([]models.JobSpec, error) {
query := j.db.WithContext(ctx).
Preload("Namespace").
Preload("Project").
Joins("Project").
Where(`"Project"."name" = ?`, projectName).
Find(&jobs).Error; err != nil {
Where(`"Project"."name" = ?`, projectName)

if includeDeleted {
query = query.Unscoped()
}

var jobs []Job
if err := query.Find(&jobs).Error; err != nil {
return nil, err
}
return j.toJobSpecs(jobs)
}

func (j jobSpecRepository) GetAllByProjectNameAndNamespaceName(ctx context.Context, projectName, namespaceName string) ([]models.JobSpec, error) {
var jobs []Job
if err := j.db.WithContext(ctx).
func (j jobSpecRepository) GetAllByProjectNameAndNamespaceName(ctx context.Context, projectName, namespaceName string, includeDeleted bool) ([]models.JobSpec, error) {
query := j.db.WithContext(ctx).
Preload("Namespace").
Preload("Project").
Joins("Project").
Joins("Namespace").
Where(`"Project"."name" = ? and "Namespace"."name" = ?`, projectName, namespaceName).
Find(&jobs).Error; err != nil {
Where(`"Project"."name" = ? and "Namespace"."name" = ?`, projectName, namespaceName)

if includeDeleted {
query = query.Unscoped()
}

var jobs []Job
if err := query.Find(&jobs).Error; err != nil {
return nil, err
}
return j.toJobSpecs(jobs)
}

func (j jobSpecRepository) GetByNameAndProjectName(ctx context.Context, name, projectName string) (models.JobSpec, error) {
job, err := j.getByNameAndProjectName(ctx, name, projectName)
func (j jobSpecRepository) GetByNameAndProjectName(ctx context.Context, name, projectName string, includeDeleted bool) (models.JobSpec, error) {
job, err := j.getByNameAndProjectName(ctx, name, projectName, includeDeleted)
if err != nil {
return models.JobSpec{}, err
}
return j.adapter.ToSpec(job)
}

func (j jobSpecRepository) GetByResourceDestinationURN(ctx context.Context, resourceDestinationURN string) ([]models.JobSpec, error) {
var jobs []Job
if err := j.db.WithContext(ctx).
func (j jobSpecRepository) GetByResourceDestinationURN(ctx context.Context, resourceDestinationURN string, includeDeleted bool) ([]models.JobSpec, error) {
query := j.db.WithContext(ctx).
Preload("Namespace").
Preload("Project").
Where("destination = ?", resourceDestinationURN).
Find(&jobs).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return []models.JobSpec{}, store.ErrResourceNotFound
}
Where("destination = ?", resourceDestinationURN)

if includeDeleted {
query = query.Unscoped()
}

var jobs []Job
if err := query.Find(&jobs).Error; err != nil {
return []models.JobSpec{}, err
}
if len(jobs) == 0 {
return []models.JobSpec{}, store.ErrResourceNotFound
}
return j.toJobSpecs(jobs)
}

Expand Down Expand Up @@ -156,7 +171,7 @@ func (j jobSpecRepository) Save(ctx context.Context, incomingJobSpec models.JobS
return err
}

existingJob, err := j.getByNameAndProjectName(ctx, incomingJob.Name, incomingJob.Project.Name)
existingJob, err := j.getByNameAndProjectName(ctx, incomingJob.Name, incomingJob.Project.Name, false)
if errors.Is(err, store.ErrResourceNotFound) {
return j.insert(ctx, incomingJob)
} else if err != nil {
Expand All @@ -181,14 +196,19 @@ func (j jobSpecRepository) DeleteByID(ctx context.Context, id uuid.UUID) error {
})
}

func (j jobSpecRepository) getByNameAndProjectName(ctx context.Context, name, projectName string) (Job, error) {
var job Job
if err := j.db.WithContext(ctx).
func (j jobSpecRepository) getByNameAndProjectName(ctx context.Context, name, projectName string, includeDeleted bool) (Job, error) {
query := j.db.WithContext(ctx).
Preload("Namespace").
Preload("Project").
Joins("Project").
Where(`"job"."name" = ? and "Project"."name" = ?`, name, projectName).
First(&job).Error; err != nil {
Where(`"job"."name" = ? and "Project"."name" = ?`, name, projectName)

if includeDeleted {
query = query.Unscoped()
}

var job Job
if err := query.First(&job).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return Job{}, store.ErrResourceNotFound
}
Expand Down
Loading

0 comments on commit 337c53c

Please sign in to comment.