Skip to content

Commit

Permalink
feat(informer): sync latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wfxr committed Nov 10, 2021
1 parent c1ac08b commit edb2734
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
41 changes: 35 additions & 6 deletions internal/database/metadatav2/postgres/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,50 @@ package postgres
import (
"context"
"fmt"
"strings"

"github.com/jackc/pgerrcode"
"github.com/lib/pq"
"github.com/oom-ai/oomstore/internal/database/dbutil"
"github.com/oom-ai/oomstore/internal/database/metadatav2"
"github.com/oom-ai/oomstore/pkg/oomstore/typesv2"
)

func (db *DB) CreateRevision(ctx context.Context, opt metadatav2.CreateRevisionOpt) error {
query := "INSERT INTO feature_group_revision(group_name, revision, data_table, description) VALUES ($1, $2, $3, $4)"
_, err := db.ExecContext(ctx, query, opt.GroupName, opt.Revision, opt.DataTable, opt.Description)
if err != nil {
func (db *DB) CreateRevision(ctx context.Context, opt metadatav2.CreateRevisionOpt) (*typesv2.Revision, error) {
query := "INSERT INTO feature_group_revision(group_name, revision, data_table, anchored, description) VALUES ($1, $2, $3, $4, $5) RETURNING *"
var revision typesv2.Revision
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 fmt.Errorf("revision %v already exist", opt.Revision)
return nil, fmt.Errorf("revision %v already exist", opt.Revision)
}
}
return nil, err
}
return &revision, nil
}

func (db *DB) UpdateRevision(ctx context.Context, opt metadatav2.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()
}
return err
}
1 change: 1 addition & 0 deletions internal/database/metadatav2/postgres/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var META_TABLE_SCHEMAS = map[string]string{
group_id INT NOT NULL,
revision BIGINT NOT NULL,
data_table VARCHAR(64) NOT NULL,
anchored BOOLEAN NOT NULL,
description VARCHAR(64) DEFAULT '',
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
modify_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
Expand Down
3 changes: 2 additions & 1 deletion internal/database/metadatav2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type Store interface {
ListFeatureGroup(ctx context.Context, entityName *string) typesv2.FeatureGroupList

// revision
CreateRevision(ctx context.Context, opt CreateRevisionOpt) error
CreateRevision(ctx context.Context, opt CreateRevisionOpt) (*typesv2.Revision, error)
UpdateRevision(ctx context.Context, opt UpdateRevisionOpt) (int64, error)
ListRevision(ctx context.Context, opt ListRevisionOpt) typesv2.RevisionList
GetRevision(ctx context.Context, opt GetRevisionOpt) (*typesv2.Revision, error)
GetLatestRevision(ctx context.Context, groupName string) *typesv2.Revision
Expand Down
7 changes: 7 additions & 0 deletions internal/database/metadatav2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type CreateRevisionOpt struct {
Revision int64
GroupName string
DataTable string
Anchored bool
Description string
}

Expand All @@ -31,3 +32,9 @@ type ListRevisionOpt struct {
GroupName *string
DataTables []string
}

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

0 comments on commit edb2734

Please sign in to comment.