github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/operation/repository.go (about) 1 package operation 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/kyma-incubator/compass/components/director/internal/model" 8 "github.com/kyma-incubator/compass/components/director/internal/repo" 9 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 10 "github.com/kyma-incubator/compass/components/director/pkg/log" 11 "github.com/kyma-incubator/compass/components/director/pkg/resource" 12 ) 13 14 const operationTable = `public.operation` 15 16 var ( 17 operationTypeColumn = "op_type" 18 statusColumn = "status" 19 finishedAtColumn = "finished_at" 20 operationColumns = []string{"id", operationTypeColumn, statusColumn, "data", "error", "priority", "created_at", finishedAtColumn} 21 ) 22 23 // EntityConverter missing godoc 24 // 25 //go:generate mockery --name=EntityConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 26 type EntityConverter interface { 27 ToEntity(in *model.Operation) *Entity 28 FromEntity(entity *Entity) *model.Operation 29 } 30 31 type pgRepository struct { 32 globalCreator repo.CreatorGlobal 33 globalDeleter repo.DeleterGlobal 34 conv EntityConverter 35 } 36 37 // NewRepository creates new operation repository 38 func NewRepository(conv EntityConverter) *pgRepository { 39 return &pgRepository{ 40 globalCreator: repo.NewCreatorGlobal(resource.Operation, operationTable, operationColumns), 41 globalDeleter: repo.NewDeleterGlobal(resource.Operation, operationTable), 42 conv: conv, 43 } 44 } 45 46 // Create creates operation entity 47 func (r *pgRepository) Create(ctx context.Context, model *model.Operation) error { 48 if model == nil { 49 return apperrors.NewInternalError("model can not be empty") 50 } 51 52 log.C(ctx).Debugf("Converting Operation model with id %s to entity", model.ID) 53 operationEnt := r.conv.ToEntity(model) 54 55 log.C(ctx).Debugf("Persisting Operation entity with id %s to db", model.ID) 56 return r.globalCreator.Create(ctx, operationEnt) 57 } 58 59 // DeleteOlderThan deletes all operations of type `opType` with status `status` older than `date` 60 func (r *pgRepository) DeleteOlderThan(ctx context.Context, opType string, status model.OperationStatus, date time.Time) error { 61 log.C(ctx).Infof("Deleting all operations of type %s with status %s older than %v", opType, status, date) 62 return r.globalDeleter.DeleteManyGlobal(ctx, repo.Conditions{ 63 repo.NewNotNullCondition(finishedAtColumn), 64 repo.NewEqualCondition(operationTypeColumn, opType), 65 repo.NewEqualCondition(statusColumn, string(status)), 66 repo.NewLessThanCondition(finishedAtColumn, date), 67 }) 68 }