Skip to content

Commit 495c666

Browse files
authored
fix: store, retrieve asset.url (#209)
1 parent c96e577 commit 495c666

File tree

10 files changed

+74
-18
lines changed

10 files changed

+74
-18
lines changed

core/asset/asset.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Asset struct {
3535
Name string `json:"name" diff:"name"`
3636
Description string `json:"description" diff:"description"`
3737
Data map[string]interface{} `json:"data" diff:"data"`
38+
URL string `json:"url" diff:"url"`
3839
Labels map[string]string `json:"labels" diff:"labels"`
3940
Owners []user.User `json:"owners,omitempty" diff:"owners"`
4041
CreatedAt time.Time `json:"created_at" diff:"-"`

core/asset/asset_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ func TestAssetPatch(t *testing.T) {
292292
"service": "firehose",
293293
"description": "new-description",
294294
"name": "new-name",
295+
"url": "https://sample-url.com",
295296
"labels": map[string]string{
296297
"bar": "foo",
297298
"bar2": "foo2",
@@ -307,6 +308,7 @@ func TestAssetPatch(t *testing.T) {
307308
Service: "firehose",
308309
Description: "new-description",
309310
Name: "new-name",
311+
URL: "https://sample-url.com",
310312
Labels: map[string]string{
311313
"bar": "foo",
312314
"bar2": "foo2",
@@ -325,6 +327,7 @@ func TestAssetPatch(t *testing.T) {
325327
Service: "optimus",
326328
Description: "sample-description",
327329
Name: "old-name",
330+
URL: "https://sample-url-old.com",
328331
Labels: map[string]string{
329332
"foo": "bar",
330333
},
@@ -338,6 +341,7 @@ func TestAssetPatch(t *testing.T) {
338341
"service": "firehose",
339342
"description": "new-description",
340343
"name": "new-name",
344+
"url": "https://sample-url.com",
341345
"labels": map[string]string{
342346
"bar": "foo",
343347
"bar2": "foo2",
@@ -353,6 +357,7 @@ func TestAssetPatch(t *testing.T) {
353357
Service: "firehose",
354358
Description: "new-description",
355359
Name: "new-name",
360+
URL: "https://sample-url.com",
356361
Labels: map[string]string{
357362
"bar": "foo",
358363
"bar2": "foo2",

core/asset/patch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func patchAsset(a *Asset, patchData map[string]interface{}) {
1212
a.Service = patchString("service", patchData, a.Service)
1313
a.Name = patchString("name", patchData, a.Name)
1414
a.Description = patchString("description", patchData, a.Description)
15+
a.URL = patchString("url", patchData, a.URL)
1516

1617
labels, exists := patchData["labels"]
1718
if exists {

internal/server/v1beta1/asset.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ func (server *APIServer) buildAsset(baseAsset *compassv1beta1.UpsertAssetRequest
422422
Name: baseAsset.GetName(),
423423
Description: baseAsset.GetDescription(),
424424
Data: baseAsset.GetData().AsMap(),
425+
URL: baseAsset.Url,
425426
Labels: baseAsset.GetLabels(),
426427
}
427428

@@ -500,6 +501,9 @@ func decodePatchAssetToMap(pb *compassv1beta1.UpsertPatchAssetRequest_Asset) map
500501
if pb.GetData() != nil {
501502
m["data"] = pb.GetData().AsMap()
502503
}
504+
if len(pb.Url) > 0 {
505+
m["url"] = pb.Url
506+
}
503507
if pb.GetLabels() != nil {
504508
m["labels"] = pb.GetLabels()
505509
}
@@ -578,6 +582,7 @@ func assetToProto(a asset.Asset, withChangelog bool) (assetPB *compassv1beta1.As
578582
Name: a.Name,
579583
Description: a.Description,
580584
Data: data,
585+
Url: a.URL,
581586
Labels: a.Labels,
582587
Owners: owners,
583588
Version: a.Version,

internal/server/v1beta1/asset_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func TestUpsertAsset(t *testing.T) {
332332
Name: "new-name",
333333
Service: "kafka",
334334
Data: &structpb.Struct{},
335+
Url: "https://sample-url.com",
335336
Owners: []*compassv1beta1.User{{Id: "id", Uuid: "", Email: "[email protected]", Provider: "provider"}},
336337
},
337338
Upstreams: []*compassv1beta1.LineageNode{
@@ -450,6 +451,7 @@ func TestUpsertAsset(t *testing.T) {
450451
Service: "kafka",
451452
UpdatedBy: user.User{ID: userID},
452453
Data: map[string]interface{}{},
454+
URL: "https://sample-url.com",
453455
Owners: []user.User{{ID: "id", UUID: "", Email: "[email protected]", Provider: "provider"}},
454456
}
455457
upstreams := []string{"upstream-1"}
@@ -520,6 +522,7 @@ func TestUpsertPatchAsset(t *testing.T) {
520522
Name: wrapperspb.String("new-name"),
521523
Service: "kafka",
522524
Data: &structpb.Struct{},
525+
Url: "https://sample-url.com",
523526
Owners: []*compassv1beta1.User{{Id: "id", Uuid: "", Email: "[email protected]", Provider: "provider"}},
524527
},
525528
Upstreams: []*compassv1beta1.LineageNode{
@@ -549,6 +552,7 @@ func TestUpsertPatchAsset(t *testing.T) {
549552
Service: "kafka",
550553
UpdatedBy: user.User{ID: userID},
551554
Data: map[string]interface{}{},
555+
URL: "https://sample-url-old.com",
552556
Owners: []user.User{{ID: "id", UUID: "", Email: "[email protected]", Provider: "provider"}},
553557
}
554558
)
@@ -652,6 +656,7 @@ func TestUpsertPatchAsset(t *testing.T) {
652656
Service: "kafka",
653657
UpdatedBy: user.User{ID: userID},
654658
Data: map[string]interface{}{},
659+
URL: "https://sample-url.com",
655660
Owners: []user.User{{ID: "id", UUID: "", Email: "[email protected]", Provider: "provider"}},
656661
}
657662
upstreams := []string{"upstream-1"}
@@ -688,6 +693,7 @@ func TestUpsertPatchAsset(t *testing.T) {
688693
Service: "kafka",
689694
UpdatedBy: user.User{ID: userID},
690695
Data: map[string]interface{}{},
696+
URL: "https://sample-url-old.com",
691697
Owners: []user.User{{ID: "id", UUID: "", Email: "[email protected]", Provider: "provider"}},
692698
}
693699

@@ -733,6 +739,7 @@ func TestUpsertPatchAsset(t *testing.T) {
733739
Service: "kafka",
734740
UpdatedBy: user.User{ID: userID},
735741
Data: map[string]interface{}{},
742+
URL: "https://sample-url-old.com",
736743
Owners: []user.User{{ID: "id", UUID: "", Email: "[email protected]", Provider: "provider"}},
737744
}
738745

internal/store/postgres/asset_model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type AssetModel struct {
2121
Service string `db:"service"`
2222
Description string `db:"description"`
2323
Data JSONMap `db:"data"`
24+
URL string `db:"url"`
2425
Labels JSONMap `db:"labels"`
2526
Version string `db:"version"`
2627
UpdatedBy UserModel `db:"updated_by"`
@@ -41,6 +42,7 @@ func (a *AssetModel) toAsset(owners []user.User) asset.Asset {
4142
Service: a.Service,
4243
Description: a.Description,
4344
Data: a.Data,
45+
URL: a.URL,
4446
Labels: a.buildLabels(),
4547
Owners: owners,
4648
Version: a.Version,

internal/store/postgres/asset_repository.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ func (r *AssetRepository) deleteWithPredicate(ctx context.Context, pred sq.Eq) (
453453
func (r *AssetRepository) insert(ctx context.Context, ast *asset.Asset) (id string, err error) {
454454
err = r.client.RunWithinTx(ctx, func(tx *sqlx.Tx) error {
455455
query, args, err := sq.Insert("assets").
456-
Columns("urn", "type", "service", "name", "description", "data", "labels", "updated_by", "version").
457-
Values(ast.URN, ast.Type, ast.Service, ast.Name, ast.Description, ast.Data, ast.Labels, ast.UpdatedBy.ID, asset.BaseVersion).
456+
Columns("urn", "type", "service", "name", "description", "data", "url", "labels", "updated_by", "version").
457+
Values(ast.URN, ast.Type, ast.Service, ast.Name, ast.Description, ast.Data, ast.URL, ast.Labels, ast.UpdatedBy.ID, asset.BaseVersion).
458458
Suffix("RETURNING \"id\"").
459459
PlaceholderFormat(sq.Dollar).
460460
ToSql()
@@ -490,7 +490,6 @@ func (r *AssetRepository) insert(ctx context.Context, ast *asset.Asset) (id stri
490490
}
491491

492492
func (r *AssetRepository) update(ctx context.Context, assetID string, newAsset *asset.Asset, oldAsset *asset.Asset, clog diff.Changelog) error {
493-
494493
if !isValidUUID(assetID) {
495494
return asset.InvalidError{AssetID: assetID}
496495
}
@@ -508,22 +507,24 @@ func (r *AssetRepository) update(ctx context.Context, assetID string, newAsset *
508507
newAsset.Version = newVersion
509508
newAsset.ID = oldAsset.ID
510509

511-
err = r.execContext(ctx, tx,
512-
`UPDATE assets
513-
SET urn = $1,
514-
type = $2,
515-
service = $3,
516-
name = $4,
517-
description = $5,
518-
data = $6,
519-
labels = $7,
520-
updated_at = $8,
521-
updated_by = $9,
522-
version = $10
523-
WHERE id = $11;
524-
`,
525-
newAsset.URN, newAsset.Type, newAsset.Service, newAsset.Name, newAsset.Description, newAsset.Data, newAsset.Labels, time.Now(), newAsset.UpdatedBy.ID, newAsset.Version, assetID)
510+
query, args, err := r.buildSQL(sq.Update("assets").
511+
Set("urn", newAsset.URN).
512+
Set("type", newAsset.Type).
513+
Set("service", newAsset.Service).
514+
Set("name", newAsset.Name).
515+
Set("description", newAsset.Description).
516+
Set("data", newAsset.Data).
517+
Set("url", newAsset.URL).
518+
Set("labels", newAsset.Labels).
519+
Set("updated_at", time.Now()).
520+
Set("updated_by", newAsset.UpdatedBy.ID).
521+
Set("version", newAsset.Version).
522+
Where(sq.Eq{"id": assetID}))
526523
if err != nil {
524+
return fmt.Errorf("build query: %w", err)
525+
}
526+
527+
if err := r.execContext(ctx, tx, query, args...); err != nil {
527528
return fmt.Errorf("error running update asset query: %w", err)
528529
}
529530

@@ -775,6 +776,7 @@ func (r *AssetRepository) getAssetSQL() sq.SelectBuilder {
775776
a.service as service,
776777
a.description as description,
777778
a.data as data,
779+
COALESCE(a.url, '') as url,
778780
a.labels as labels,
779781
a.version as version,
780782
a.created_at as created_at,

internal/store/postgres/asset_repository_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ func (r *AssetRepositoryTestSuite) TestUpsert() {
930930
Type: "table",
931931
Service: "bigquery",
932932
Version: "0.1",
933+
URL: "https://sample-url.com",
933934
UpdatedBy: r.users[0],
934935
}
935936
id, err := r.repository.Upsert(r.ctx, &ast)
@@ -1019,6 +1020,36 @@ func (r *AssetRepositoryTestSuite) TestUpsert() {
10191020
r.Equal(ast.ID, identicalAsset.ID)
10201021
})
10211022

1023+
r.Run("should update the asset if asset is not identical", func() {
1024+
ast := asset.Asset{
1025+
URN: "urn-u-2",
1026+
Type: "table",
1027+
Service: "bigquery",
1028+
URL: "https://sample-url-old.com",
1029+
UpdatedBy: r.users[0],
1030+
}
1031+
1032+
id, err := r.repository.Upsert(r.ctx, &ast)
1033+
r.Require().NoError(err)
1034+
r.NotEmpty(id)
1035+
ast.ID = id
1036+
1037+
updated := ast
1038+
updated.URL = "https://sample-url.com"
1039+
1040+
id, err = r.repository.Upsert(r.ctx, &updated)
1041+
r.Require().NoError(err)
1042+
r.NotEmpty(id)
1043+
updated.ID = id
1044+
1045+
r.Equal(ast.ID, updated.ID)
1046+
1047+
actual, err := r.repository.GetByID(r.ctx, ast.ID)
1048+
r.NoError(err)
1049+
1050+
r.Equal(updated.URL, actual.URL)
1051+
})
1052+
10221053
r.Run("should delete old owners if it does not exist on new asset", func() {
10231054
ast := asset.Asset{
10241055
URN: "urn-u-4",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE assets DROP COLUMN IF EXISTS url;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE assets ADD COLUMN IF NOT EXISTS url TEXT;

0 commit comments

Comments
 (0)