Skip to content

Commit

Permalink
fix: populate version in assets_versions for 1st entry (#11)
Browse files Browse the repository at this point in the history
- Set version to 0.1 while inserting the first version of asset into
  assets_versions table.
- Update all rows in assets_versions to have 0.1 as the version if the
  column was not populated.
  • Loading branch information
sudo-suhas authored Mar 28, 2023
1 parent ae42e30 commit 6e50d46
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
27 changes: 15 additions & 12 deletions internal/store/postgres/asset_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,22 @@ func (r *AssetRepository) Upsert(ctx context.Context, ast *asset.Asset) (string,

if fetchedAsset.ID == "" {
// insert flow
fetchedAsset.ID, err = r.insert(ctx, ast)
id, err := r.insert(ctx, ast)
if err != nil {
return fetchedAsset.ID, fmt.Errorf("error inserting asset to DB: %w", err)
}
} else {
// update flow
changelog, err := fetchedAsset.Diff(ast)
if err != nil {
return "", fmt.Errorf("error diffing two assets: %w", err)
return "", fmt.Errorf("error inserting asset to DB: %w", err)
}
return id, nil
}

err = r.update(ctx, fetchedAsset.ID, ast, &fetchedAsset, changelog)
if err != nil {
return "", fmt.Errorf("error updating asset to DB: %w", err)
}
// update flow
changelog, err := fetchedAsset.Diff(ast)
if err != nil {
return "", fmt.Errorf("error diffing two assets: %w", err)
}

err = r.update(ctx, fetchedAsset.ID, ast, &fetchedAsset, changelog)
if err != nil {
return "", fmt.Errorf("error updating asset to DB: %w", err)
}

return fetchedAsset.ID, nil
Expand Down Expand Up @@ -462,6 +463,8 @@ func (r *AssetRepository) insert(ctx context.Context, ast *asset.Asset) (id stri
return fmt.Errorf("error building insert query: %w", err)
}

ast.Version = asset.BaseVersion

err = tx.QueryRowContext(ctx, query, args...).Scan(&id)
if err != nil {
return fmt.Errorf("error running insert query: %w", err)
Expand Down
10 changes: 9 additions & 1 deletion internal/store/postgres/asset_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,11 @@ func (r *AssetRepositoryTestSuite) TestUpsert() {
URN: "urn-u-1",
Type: "table",
Service: "bigquery",
Version: "0.1",
URL: "https://sample-url.com",
UpdatedBy: r.users[0],
}
id, err := r.repository.Upsert(r.ctx, &ast)
r.Equal(asset.BaseVersion, ast.Version)
r.NoError(err)
r.NotEmpty(id)
ast.ID = id
Expand All @@ -943,6 +943,14 @@ func (r *AssetRepositoryTestSuite) TestUpsert() {
r.NotEqual(time.Time{}, assetInDB.CreatedAt)
r.NotEqual(time.Time{}, assetInDB.UpdatedAt)
r.assertAsset(&ast, &assetInDB)

ast2 := ast
ast2.Description = "create a new version" // to force fetch from asset_versions.
_, err = r.repository.Upsert(r.ctx, &ast2)
r.NoError(err)
assetv1, err := r.repository.GetByVersionWithID(r.ctx, ast.ID, asset.BaseVersion)
r.NoError(err)
r.Equal("0.1", assetv1.Version)
})

r.Run("should store owners if any", func() {
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE assets_versions SET version = '0.1' WHERE version = '';

0 comments on commit 6e50d46

Please sign in to comment.