github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/tombstone/repository.go (about) 1 package tombstone 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/internal/model" 7 "github.com/kyma-incubator/compass/components/director/internal/repo" 8 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 9 "github.com/kyma-incubator/compass/components/director/pkg/log" 10 "github.com/kyma-incubator/compass/components/director/pkg/resource" 11 "github.com/pkg/errors" 12 ) 13 14 const tombstoneTable string = `public.tombstones` 15 16 var ( 17 tombstoneColumns = []string{"ord_id", "app_id", "app_template_version_id", "removal_date", "id"} 18 updatableColumns = []string{"removal_date"} 19 ) 20 21 // EntityConverter missing godoc 22 // 23 //go:generate mockery --name=EntityConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 24 type EntityConverter interface { 25 ToEntity(in *model.Tombstone) *Entity 26 FromEntity(entity *Entity) (*model.Tombstone, error) 27 } 28 29 type pgRepository struct { 30 conv EntityConverter 31 existQuerier repo.ExistQuerier 32 singleGetter repo.SingleGetter 33 singleGetterGlobal repo.SingleGetterGlobal 34 lister repo.Lister 35 listerGlobal repo.ListerGlobal 36 deleter repo.Deleter 37 creator repo.Creator 38 creatorGlobal repo.CreatorGlobal 39 updater repo.Updater 40 updaterGlobal repo.UpdaterGlobal 41 } 42 43 // NewRepository missing godoc 44 func NewRepository(conv EntityConverter) *pgRepository { 45 return &pgRepository{ 46 conv: conv, 47 existQuerier: repo.NewExistQuerier(tombstoneTable), 48 singleGetter: repo.NewSingleGetter(tombstoneTable, tombstoneColumns), 49 singleGetterGlobal: repo.NewSingleGetterGlobal(resource.Tombstone, tombstoneTable, tombstoneColumns), 50 lister: repo.NewLister(tombstoneTable, tombstoneColumns), 51 listerGlobal: repo.NewListerGlobal(resource.Tombstone, tombstoneTable, tombstoneColumns), 52 deleter: repo.NewDeleter(tombstoneTable), 53 creator: repo.NewCreator(tombstoneTable, tombstoneColumns), 54 creatorGlobal: repo.NewCreatorGlobal(resource.Tombstone, tombstoneTable, tombstoneColumns), 55 updater: repo.NewUpdater(tombstoneTable, updatableColumns, []string{"id"}), 56 updaterGlobal: repo.NewUpdaterGlobal(resource.Tombstone, tombstoneTable, updatableColumns, []string{"id"}), 57 } 58 } 59 60 // Create missing godoc 61 func (r *pgRepository) Create(ctx context.Context, tenant string, model *model.Tombstone) error { 62 if model == nil { 63 return apperrors.NewInternalError("model can not be nil") 64 } 65 66 log.C(ctx).Debugf("Persisting Tombstone entity with id %q", model.ID) 67 return r.creator.Create(ctx, resource.Tombstone, tenant, r.conv.ToEntity(model)) 68 } 69 70 // CreateGlobal creates a tombstone without tenant isolation 71 func (r *pgRepository) CreateGlobal(ctx context.Context, model *model.Tombstone) error { 72 if model == nil { 73 return apperrors.NewInternalError("model can not be nil") 74 } 75 76 log.C(ctx).Debugf("Persisting Tombstone entity with id %q", model.ID) 77 return r.creatorGlobal.Create(ctx, r.conv.ToEntity(model)) 78 } 79 80 // Update missing godoc 81 func (r *pgRepository) Update(ctx context.Context, tenant string, model *model.Tombstone) error { 82 if model == nil { 83 return apperrors.NewInternalError("model can not be nil") 84 } 85 log.C(ctx).Debugf("Updating Tombstone entity with id %q", model.ID) 86 return r.updater.UpdateSingle(ctx, resource.Tombstone, tenant, r.conv.ToEntity(model)) 87 } 88 89 // UpdateGlobal updates a given tombstone without tenant isolation 90 func (r *pgRepository) UpdateGlobal(ctx context.Context, model *model.Tombstone) error { 91 if model == nil { 92 return apperrors.NewInternalError("model can not be nil") 93 } 94 log.C(ctx).Debugf("Updating Tombstone entity with id %q", model.ID) 95 return r.updaterGlobal.UpdateSingleGlobal(ctx, r.conv.ToEntity(model)) 96 } 97 98 // Delete missing godoc 99 func (r *pgRepository) Delete(ctx context.Context, tenant, id string) error { 100 log.C(ctx).Debugf("Deleting Tombstone entity with id %q", id) 101 return r.deleter.DeleteOne(ctx, resource.Tombstone, tenant, repo.Conditions{repo.NewEqualCondition("id", id)}) 102 } 103 104 // Exists missing godoc 105 func (r *pgRepository) Exists(ctx context.Context, tenant, id string) (bool, error) { 106 return r.existQuerier.Exists(ctx, resource.Tombstone, tenant, repo.Conditions{repo.NewEqualCondition("id", id)}) 107 } 108 109 // GetByID missing godoc 110 func (r *pgRepository) GetByID(ctx context.Context, tenant, id string) (*model.Tombstone, error) { 111 log.C(ctx).Debugf("Getting Tombstone entity with id %q", id) 112 var tombstoneEnt Entity 113 if err := r.singleGetter.Get(ctx, resource.Tombstone, tenant, repo.Conditions{repo.NewEqualCondition("id", id)}, repo.NoOrderBy, &tombstoneEnt); err != nil { 114 return nil, err 115 } 116 117 tombstoneModel, err := r.conv.FromEntity(&tombstoneEnt) 118 if err != nil { 119 return nil, errors.Wrap(err, "while converting Tombstone from Entity") 120 } 121 122 return tombstoneModel, nil 123 } 124 125 // GetByIDGlobal gets a tombstone by id without tenant isolation 126 func (r *pgRepository) GetByIDGlobal(ctx context.Context, id string) (*model.Tombstone, error) { 127 log.C(ctx).Debugf("Getting Tombstone entity with id %q", id) 128 var tombstoneEnt Entity 129 if err := r.singleGetterGlobal.GetGlobal(ctx, repo.Conditions{repo.NewEqualCondition("id", id)}, repo.NoOrderBy, &tombstoneEnt); err != nil { 130 return nil, err 131 } 132 133 tombstoneModel, err := r.conv.FromEntity(&tombstoneEnt) 134 if err != nil { 135 return nil, errors.Wrap(err, "while converting Tombstone from Entity") 136 } 137 138 return tombstoneModel, nil 139 } 140 141 // ListByResourceID lists tombstones by resource ID and type 142 func (r *pgRepository) ListByResourceID(ctx context.Context, tenantID, resourceID string, resourceType resource.Type) ([]*model.Tombstone, error) { 143 tombstoneCollection := tombstoneCollection{} 144 145 var condition repo.Condition 146 var err error 147 if resourceType == resource.Application { 148 condition = repo.NewEqualCondition("app_id", resourceID) 149 err = r.lister.ListWithSelectForUpdate(ctx, resource.Tombstone, tenantID, &tombstoneCollection, condition) 150 } else { 151 condition = repo.NewEqualCondition("app_template_version_id", resourceID) 152 err = r.listerGlobal.ListGlobalWithSelectForUpdate(ctx, &tombstoneCollection, condition) 153 } 154 if err != nil { 155 return nil, err 156 } 157 158 tombstones := make([]*model.Tombstone, 0, tombstoneCollection.Len()) 159 for _, tombstone := range tombstoneCollection { 160 tombstoneModel, err := r.conv.FromEntity(&tombstone) 161 if err != nil { 162 return nil, err 163 } 164 tombstones = append(tombstones, tombstoneModel) 165 } 166 return tombstones, nil 167 } 168 169 type tombstoneCollection []Entity 170 171 // Len missing godoc 172 func (pc tombstoneCollection) Len() int { 173 return len(pc) 174 }