Skip to content

Commit 458dd5f

Browse files
author
Muhammad Luthfi Fahlevi
committed
fix: soft delete by ID succeeded
1 parent 19d274c commit 458dd5f

File tree

9 files changed

+172
-58
lines changed

9 files changed

+172
-58
lines changed

core/asset/asset.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ type Asset struct {
4949
RefreshedAt *time.Time `json:"refreshed_at" diff:"-"`
5050
Version string `json:"version" diff:"-"`
5151
UpdatedBy user.User `json:"updated_by" diff:"-"`
52-
IsDeleted bool `json:"is_deleted"`
52+
IsDeleted bool `json:"is_deleted" diff:"is_deleted"`
5353
Changelog diff.Changelog `json:"changelog,omitempty" diff:"-"`
5454
Probes []Probe `json:"probes,omitempty"`
5555
}
5656

5757
type SoftDeleteAsset struct {
58-
URN string `json:"urn"`
59-
UpdatedAt time.Time `json:"updated_at"`
60-
RefreshedAt time.Time `json:"refreshed_at"`
61-
Version string `json:"version"`
62-
UpdatedBy string `json:"updated_by"`
63-
IsDeleted bool `json:"is_deleted"`
64-
Changelog diff.Changelog `json:"changelog"`
58+
URN string `json:"urn" diff:"-"`
59+
UpdatedAt time.Time `json:"updated_at" diff:"-"`
60+
RefreshedAt time.Time `json:"refreshed_at" diff:"-"`
61+
Version string `json:"version" diff:"-"`
62+
UpdatedBy string `json:"updated_by" diff:"-"`
63+
IsDeleted bool `json:"is_deleted" diff:"is_deleted"`
64+
Changelog diff.Changelog `json:"changelog,omitempty" diff:"-"`
6565
}
6666

6767
func NewSoftDeleteAsset(updatedAt, refreshedAt time.Time, updatedBy string) SoftDeleteAsset {

core/asset/service.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,32 +185,28 @@ func (s *Service) DeleteAsset(ctx context.Context, id string) (err error) {
185185
}
186186

187187
// SoftDeleteAsset is soft-deletion that can accept ID or URN of asset
188-
func (s *Service) SoftDeleteAsset(ctx context.Context, id, updatedBy string) (err error) {
188+
func (s *Service) SoftDeleteAsset(ctx context.Context, id, updatedBy string) (urn string, err error) {
189189
defer func() {
190190
s.instrumentAssetOp(ctx, "SoftDeleteAsset", id, err)
191191
}()
192192

193193
currentTime := time.Now()
194194
softDeleteAsset := NewSoftDeleteAsset(currentTime, currentTime, updatedBy)
195195

196-
urn := id
196+
urn = id
197197
if isValidUUID(id) {
198198
urn, err = s.assetRepository.SoftDeleteByID(ctx, id, softDeleteAsset)
199199
if err != nil {
200-
return err
200+
return urn, err
201201
}
202202
} else {
203203
if err := s.assetRepository.SoftDeleteByURN(ctx, urn, softDeleteAsset); err != nil {
204-
return err
204+
return urn, err
205205
}
206206
}
207207

208208
softDeleteAsset.URN = urn
209-
if err := s.worker.EnqueueSoftDeleteAssetJob(ctx, softDeleteAsset); err != nil {
210-
return err
211-
}
212-
213-
return s.lineageRepository.DeleteByURN(ctx, urn)
209+
return urn, s.worker.EnqueueSoftDeleteAssetJob(ctx, softDeleteAsset)
214210
}
215211

216212
func (s *Service) DeleteAssets(ctx context.Context, request DeleteAssetsRequest) (affectedRows uint32, err error) {

internal/server/v1beta1/asset.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type AssetService interface {
3232
UpsertPatchAsset(ctx context.Context, ast *asset.Asset, upstreams, downstreams []string, patchData map[string]interface{}) (string, error)
3333
UpsertPatchAssetWithoutLineage(ctx context.Context, ast *asset.Asset, patchData map[string]interface{}) (string, error)
3434
DeleteAsset(ctx context.Context, id string) error
35-
SoftDeleteAsset(ctx context.Context, id, updatedBy string) error
35+
SoftDeleteAsset(ctx context.Context, id, updatedBy string) (string, error)
3636
DeleteAssets(ctx context.Context, request asset.DeleteAssetsRequest) (uint32, error)
3737

3838
GetLineage(ctx context.Context, urn string, query asset.LineageQuery) (asset.Lineage, error)
@@ -299,7 +299,8 @@ func (server *APIServer) DeleteAsset(ctx context.Context, req *compassv1beta1.De
299299
return nil, err
300300
}
301301

302-
if err := server.assetService.SoftDeleteAsset(ctx, req.GetId(), userID); err != nil {
302+
_, err = server.assetService.SoftDeleteAsset(ctx, req.GetId(), userID)
303+
if err != nil {
303304
if errors.As(err, new(asset.InvalidError)) {
304305
return nil, status.Error(codes.InvalidArgument, err.Error())
305306
}

internal/server/v1beta1/mocks/asset_service.go

Lines changed: 18 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/elasticsearch/discovery_repository.go

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/goto/compass/core/asset"
15+
"github.com/goto/compass/core/user"
1516
"github.com/goto/compass/pkg/queryexpr"
1617
"github.com/goto/salt/log"
1718
)
@@ -216,6 +217,19 @@ func (repo *DiscoveryRepository) softDeleteAsset(ctx context.Context, discoveryO
216217
})
217218
}(time.Now())
218219

220+
// First get the current version
221+
currentVersion, err := repo.getCurrentAssetVersion(ctx, softDeleteAsset.URN)
222+
if err != nil {
223+
return asset.DiscoveryError{
224+
Op: "GetCurrentVersion",
225+
Err: fmt.Errorf("failed to get current version for URN %s: %w", softDeleteAsset.URN, err),
226+
}
227+
}
228+
newVersion, err := asset.IncreaseMinorVersion(currentVersion)
229+
if err != nil {
230+
return err
231+
}
232+
219233
// Create the update request body
220234
body := map[string]interface{}{
221235
"query": map[string]interface{}{
@@ -228,33 +242,36 @@ func (repo *DiscoveryRepository) softDeleteAsset(ctx context.Context, discoveryO
228242
ctx._source.is_deleted = true;
229243
ctx._source.updated_at = params.updated_at;
230244
ctx._source.refreshed_at = params.refreshed_at;
231-
ctx._source.updated_by = params.updated_by
245+
ctx._source.updated_by = params.updated_by;
246+
ctx._source.version = params.version;
232247
`,
233248
"lang": "painless",
234249
"params": map[string]interface{}{
235250
"updated_at": softDeleteAsset.UpdatedAt,
236251
"refreshed_at": softDeleteAsset.RefreshedAt,
237-
"updated_by": softDeleteAsset.UpdatedBy,
252+
"updated_by": user.User{ID: softDeleteAsset.UpdatedBy},
253+
"version": newVersion,
238254
},
239255
},
240256
}
241257

242-
// Convert body to JSON
243-
var buf bytes.Buffer
244-
if err := json.NewEncoder(&buf).Encode(body); err != nil {
258+
buf, err := encodeBodyRequest(body)
259+
if err != nil {
245260
return asset.DiscoveryError{
246261
Op: "SoftDeleteByURN",
247-
Err: fmt.Errorf("failed to encode request body: %w", err),
262+
Err: err,
248263
}
249264
}
250265

251266
// Execute UpdateByQuery
252267
res, err := repo.cli.client.UpdateByQuery(
253268
[]string{defaultSearchIndex},
254269
repo.cli.client.UpdateByQuery.WithContext(ctx),
255-
repo.cli.client.UpdateByQuery.WithBody(&buf),
270+
repo.cli.client.UpdateByQuery.WithBody(buf),
256271
repo.cli.client.UpdateByQuery.WithRefresh(true),
257272
repo.cli.client.UpdateByQuery.WithIgnoreUnavailable(true),
273+
repo.cli.client.UpdateByQuery.WithWaitForCompletion(true),
274+
repo.cli.client.UpdateByQuery.WithConflicts("proceed"),
258275
)
259276
if err != nil {
260277
return asset.DiscoveryError{
@@ -276,6 +293,58 @@ func (repo *DiscoveryRepository) softDeleteAsset(ctx context.Context, discoveryO
276293
return nil
277294
}
278295

296+
// Helper function to get current version
297+
func (repo *DiscoveryRepository) getCurrentAssetVersion(ctx context.Context, urn string) (string, error) {
298+
query := map[string]interface{}{
299+
"query": map[string]interface{}{
300+
"term": map[string]interface{}{
301+
"urn.keyword": urn,
302+
},
303+
},
304+
"size": 1,
305+
"_source": []string{"version"},
306+
}
307+
308+
var buf bytes.Buffer
309+
if err := json.NewEncoder(&buf).Encode(query); err != nil {
310+
return "", fmt.Errorf("failed to encode query: %w", err)
311+
}
312+
313+
res, err := repo.cli.client.Search(
314+
repo.cli.client.Search.WithContext(ctx),
315+
repo.cli.client.Search.WithIndex(defaultSearchIndex),
316+
repo.cli.client.Search.WithBody(&buf),
317+
)
318+
if err != nil {
319+
return "", fmt.Errorf("search failed: %w", err)
320+
}
321+
defer res.Body.Close()
322+
323+
if res.IsError() {
324+
return "", fmt.Errorf("search error: %s", res.String())
325+
}
326+
327+
var result struct {
328+
Hits struct {
329+
Hits []struct {
330+
Source struct {
331+
Version string `json:"version"`
332+
} `json:"_source"`
333+
} `json:"hits"`
334+
} `json:"hits"`
335+
}
336+
337+
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
338+
return "", fmt.Errorf("failed to decode response: %w", err)
339+
}
340+
341+
if len(result.Hits.Hits) == 0 {
342+
return "", fmt.Errorf("asset with URN %s not found", urn)
343+
}
344+
345+
return result.Hits.Hits[0].Source.Version, nil
346+
}
347+
279348
func (repo *DiscoveryRepository) indexAsset(ctx context.Context, ast asset.Asset) (err error) {
280349
defer func(start time.Time) {
281350
const op = "index"
@@ -287,7 +356,7 @@ func (repo *DiscoveryRepository) indexAsset(ctx context.Context, ast asset.Asset
287356
})
288357
}(time.Now())
289358

290-
body, err := createUpsertBody(ast)
359+
body, err := encodeBodyRequest(ast)
291360
if err != nil {
292361
return asset.DiscoveryError{
293362
Op: "EncodeAsset",
@@ -327,10 +396,10 @@ func (repo *DiscoveryRepository) indexAsset(ctx context.Context, ast asset.Asset
327396
return nil
328397
}
329398

330-
func createUpsertBody(ast asset.Asset) (io.Reader, error) {
399+
func encodeBodyRequest(body interface{}) (io.Reader, error) {
331400
var buf bytes.Buffer
332-
if err := json.NewEncoder(&buf).Encode(ast); err != nil {
333-
return nil, fmt.Errorf("encode asset: %w", err)
401+
if err := json.NewEncoder(&buf).Encode(body); err != nil {
402+
return nil, fmt.Errorf("encode request body: %w", err)
334403
}
335404

336405
return &buf, nil

internal/store/postgres/asset_model.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type AssetModel struct {
2323
Data JSONMap `db:"data"`
2424
URL string `db:"url"`
2525
Labels JSONMap `db:"labels"`
26+
IsDeleted bool `db:"is_deleted"`
2627
Version string `db:"version"`
2728
UpdatedBy UserModel `db:"updated_by"`
2829
CreatedAt time.Time `db:"created_at"`
@@ -44,6 +45,7 @@ func (a *AssetModel) toAsset(owners []user.User) asset.Asset {
4445
Data: a.Data,
4546
URL: a.URL,
4647
Labels: a.buildLabels(),
48+
IsDeleted: a.IsDeleted,
4749
Owners: owners,
4850
Version: a.Version,
4951
UpdatedBy: a.UpdatedBy.toUser(),
@@ -65,6 +67,7 @@ func (a *AssetModel) toAssetVersion() (asset.Asset, error) {
6567
URN: a.URN,
6668
Type: asset.Type(a.Type),
6769
Service: a.Service,
70+
IsDeleted: a.IsDeleted,
6871
Version: a.Version,
6972
Changelog: clog,
7073
UpdatedBy: a.UpdatedBy.toUser(),
@@ -96,6 +99,7 @@ func (a *AssetModel) toVersionedAsset(latestAssetVersion asset.Asset) (asset.Ass
9699
Description: a.Description,
97100
Data: a.Data,
98101
Labels: a.buildLabels(),
102+
IsDeleted: a.IsDeleted,
99103
Owners: owners,
100104
Version: a.Version,
101105
UpdatedBy: a.UpdatedBy.toUser(),

0 commit comments

Comments
 (0)