Skip to content

Commit

Permalink
feat(metastore): add UpdateRevision method
Browse files Browse the repository at this point in the history
  • Loading branch information
lianxmfor committed Nov 9, 2021
1 parent 673e5b5 commit 5e7a901
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
15 changes: 15 additions & 0 deletions internal/database/metadata/mock_metadata/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions internal/database/metadata/postgres/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,35 @@ func (db *DB) GetRevision(ctx context.Context, opt metadata.GetRevisionOpt) (*ty
return &rs, nil
}

func (db *DB) UpdateRevision(ctx context.Context, opt metadata.UpdateRevisionOpt) (int64, error) {
and := make(map[string]interface{})
if opt.NewRevision != nil {
and["revision"] = *opt.NewRevision
}
if opt.NewAnchored != nil {
and["anchored"] = *opt.NewAnchored
}
cond, args, err := dbutil.BuildConditions(and, nil)
if err != nil {
return 0, err
}
if len(cond) == 0 {
return 0, fmt.Errorf("invliad option: nothing to update")
}
args = append(args, opt.RevisionID)

query := fmt.Sprintf("UPDATE feature_group_revision SET %s WHERE id = ?", strings.Join(cond, ","))
if result, err := db.ExecContext(ctx, db.Rebind(query), args...); err != nil {
return 0, err
} else {
return result.RowsAffected()
}
}

func (db *DB) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (*types.Revision, error) {
query := "INSERT INTO feature_group_revision(group_name, revision, data_table, anchored, description) VALUES ($1, $2, $3, $4, $5) RETURNING *"
var revision types.Revision

err := db.GetContext(ctx, &revision, query, opt.GroupName, opt.Revision, opt.DataTable, opt.Anchored, opt.Description)
if err != nil {
if err := db.GetContext(ctx, &revision, query, opt.GroupName, opt.Revision, opt.DataTable, opt.Anchored, opt.Description); err != nil {
if e2, ok := err.(*pq.Error); ok {
if e2.Code == pgerrcode.UniqueViolation {
return nil, fmt.Errorf("revision %v already exist", opt.Revision)
Expand Down
51 changes: 51 additions & 0 deletions internal/database/metadata/postgres/revision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/oom-ai/oomstore/internal/database/metadata"
"github.com/stretchr/testify/assert"
Expand All @@ -29,6 +30,56 @@ func TestCreateRevision(t *testing.T) {
assert.Nil(t, revision)
}

func TestUpdateRevision(t *testing.T) {
db := initAndOpenDB(t)
defer db.Close()

groupName := "device_baseinfo"
revisionTimestamp := time.Now().Unix()
opt := metadata.CreateRevisionOpt{
GroupName: groupName,
Revision: revisionTimestamp,
DataTable: "device_bastinfo_20211028",
Description: "description",
}
revision, err := db.CreateRevision(context.Background(), opt)
assert.Nil(t, err)
assert.Equal(t, int32(1), revision.ID)

r, err := db.GetRevision(context.Background(), metadata.GetRevisionOpt{
GroupName: &groupName,
Revision: &revisionTimestamp,
})
assert.NoError(t, err)

newRevison := time.Now().Add(time.Second).Unix()
rowsAffected, err := db.UpdateRevision(context.Background(), metadata.UpdateRevisionOpt{
RevisionID: int64(r.ID),
NewRevision: &newRevison,
})
assert.Nil(t, err)
assert.Equal(t, int64(1), rowsAffected)

r, err = db.GetRevision(context.Background(), metadata.GetRevisionOpt{
RevisionId: &r.ID,
})
assert.Nil(t, err)
assert.Equal(t, newRevison, r.Revision)
}

func TestUpdateRevisionWithEmpty(t *testing.T) {
db := initAndOpenDB(t)
defer db.Close()

newRevision := int64(0)
rowsAffected, err := db.UpdateRevision(context.Background(), metadata.UpdateRevisionOpt{
RevisionID: 0,
NewRevision: &newRevision,
})
assert.Nil(t, err)
assert.Equal(t, int64(0), rowsAffected)
}

func TestGetRevision(t *testing.T) {
db := initAndOpenDB(t)
defer db.Close()
Expand Down
1 change: 1 addition & 0 deletions internal/database/metadata/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Store interface {
ListRevision(ctx context.Context, opt ListRevisionOpt) ([]*types.Revision, error)
GetRevision(ctx context.Context, opt GetRevisionOpt) (*types.Revision, error)
GetLatestRevision(ctx context.Context, groupName string) (*types.Revision, error)
UpdateRevision(ctx context.Context, opt UpdateRevisionOpt) (int64, error)
BuildRevisionRanges(ctx context.Context, groupName string) ([]*types.RevisionRange, error)

io.Closer
Expand Down
6 changes: 6 additions & 0 deletions internal/database/metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ type ListRevisionOpt struct {
GroupName *string
DataTables []string
}

type UpdateRevisionOpt struct {
RevisionID int64
NewRevision *int64
NewAnchored *bool
}

0 comments on commit 5e7a901

Please sign in to comment.