Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: use SetViewList for syncing the database's view list (#1870)

Danny Xu3edc057bc5be
diff --git a/api/view.go b/api/view.go
index e0ef51ca8d..38c40a1f78 100644
--- a/api/view.go
+++ b/api/view.go
@@ -64,6 +64,5 @@ func (find *ViewFind) String() string {
// ViewDelete is the API message for deleting a view.
type ViewDelete struct {
- // Related fields
- DatabaseID int
+ ID int
}
diff --git a/server/sql.go b/server/sql.go
index 01c54fce58..2fc24f5279 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -683,18 +683,9 @@ func syncTableSchema(ctx context.Context, store *store.Store, database *api.Data
}
func syncViewSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {
- var createView = func(database *api.Database, viewCreate *api.ViewCreate) error {
- if _, err := store.CreateView(ctx, viewCreate); err != nil {
- if common.ErrorCode(err) == common.Conflict {
- return fmt.Errorf("failed to sync view for instance: %s, database: %s. View name already exists: %s", database.Instance.Name, database.Name, viewCreate.Name)
- }
- return fmt.Errorf("failed to sync view for instance: %s, database: %s. Failed to import new view: %s. Error %w", database.Instance.Name, database.Name, viewCreate.Name, err)
- }
- return nil
- }
- var recreateViewSchema = func(database *api.Database, view db.View) error {
- // View
- viewCreate := &api.ViewCreate{
+ var creates []*api.ViewCreate
+ for _, view := range schema.ViewList {
+ creates = append(creates, &api.ViewCreate{
CreatorID: api.SystemBotID,
CreatedTs: view.CreatedTs,
UpdatedTs: view.UpdatedTs,
@@ -702,25 +693,9 @@ func syncViewSchema(ctx context.Context, store *store.Store, database *api.Datab
Name: view.Name,
Definition: view.Definition,
Comment: view.Comment,
- }
- if err := createView(database, viewCreate); err != nil {
- return err
- }
- return nil
- }
-
- viewDelete := &api.ViewDelete{
- DatabaseID: database.ID,
- }
- if err := store.DeleteView(ctx, viewDelete); err != nil {
- return fmt.Errorf("failed to sync view for instance: %s, database %s. Failed to reset view info. Error %w", database.Instance.Name, database.Name, err)
- }
- for _, view := range schema.ViewList {
- if err := recreateViewSchema(database, view); err != nil {
- return err
- }
+ })
}
- return nil
+ return store.SetViewList(ctx, creates, database.ID, api.SystemBotID)
}
func syncDBExtensionSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {
diff --git a/store/view.go b/store/view.go
index 0194197e75..51e01a2fd0 100644
--- a/store/view.go
+++ b/store/view.go
@@ -52,19 +52,6 @@ func (raw *viewRaw) toView() *api.View {
}
}
-// CreateView creates an instance of View
-func (s *Store) CreateView(ctx context.Context, create *api.ViewCreate) (*api.View, error) {
- viewRaw, err := s.createViewRaw(ctx, create)
- if err != nil {
- return nil, fmt.Errorf("failed to create View with ViewCreate[%+v], error: %w", create, err)
- }
- view, err := s.composeView(ctx, viewRaw)
- if err != nil {
- return nil, fmt.Errorf("failed to compose View with viewRaw[%+v], error: %w", viewRaw, err)
- }
- return view, nil
-}
-
// FindView finds a list of View instances
func (s *Store) FindView(ctx context.Context, find *api.ViewFind) ([]*api.View, error) {
viewRawList, err := s.findViewRaw(ctx, find)
@@ -82,18 +69,42 @@ func (s *Store) FindView(ctx context.Context, find *api.ViewFind) ([]*api.View,
return viewList, nil
}
-// DeleteView deletes an existing view by ID.
-func (s *Store) DeleteView(ctx context.Context, delete *api.ViewDelete) error {
+// SetViewList sets the views for a database.
+func (s *Store) SetViewList(ctx context.Context, viewCreateList []*api.ViewCreate, databaseID int, updaterID int) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return FormatError(err)
}
defer tx.PTx.Rollback()
- if err := deleteViewImpl(ctx, tx.PTx, delete); err != nil {
+ oldViewRawList, err := s.findViewImpl(ctx, tx.PTx, &api.ViewFind{
+ DatabaseID: &databaseID,
+ })
+ if err != nil {
return FormatError(err)
}
+ oldViewMap := make(map[string]*viewRaw)
+ for _, v := range oldViewRawList {
+ oldViewMap[v.Name] = v
+ }
+ newViewMap := make(map[string]*api.ViewCreate)
+ for _, v := range viewCreateList {
+ newViewMap[v.Name] = v
+ }
+
+ deletes, creates := generateViewActions(oldViewMap, newViewMap)
+ for _, d := range deletes {
+ if err := deleteViewImpl(ctx, tx.PTx, d); err != nil {
+ return err
+ }
+ }
+ for _, c := range creates {
+ if _, err := s.createViewImpl(ctx, tx.PTx, c); err != nil {
+ return err
+ }
+ }
+
if err := tx.PTx.Commit(); err != nil {
return FormatError(err)
}
@@ -104,6 +115,25 @@ func (s *Store) DeleteView(ctx context.Context, delete *api.ViewDelete) error {
//
// private functions
//
+func generateViewActions(oldViewMap map[string]*viewRaw, newViewMap map[string]*api.ViewCreate) ([]*api.ViewDelete, []*api.ViewCreate) {
+ var deletes []*api.ViewDelete
+ var creates []*api.ViewCreate
+ for k, oldValue := range oldViewMap {
+ newValue, ok := newViewMap[k]
+ if !ok {
+ deletes = append(deletes, &api.ViewDelete{ID: oldValue.ID})
+ } else if ok && (oldValue.Definition != newValue.Definition || oldValue.Comment != newValue.Comment) {
+ deletes = append(deletes, &api.ViewDelete{ID: oldValue.ID})
+ creates = append(creates, newValue)
+ }
+ }
+ for k, newValue := range newViewMap {
+ if _, ok := oldViewMap[k]; !ok {
+ creates = append(creates, newValue)
+ }
+ }
+ return deletes, creates
+}
func (s *Store) composeView(ctx context.Context, raw *viewRaw) (*api.View, error) {
view := raw.toView()
@@ -129,26 +159,6 @@ func (s *Store) composeView(ctx context.Context, raw *viewRaw) (*api.View, error
return view, nil
}
-// createViewRaw creates a new view.
-func (s *Store) createViewRaw(ctx context.Context, create *api.ViewCreate) (*viewRaw, error) {
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.PTx.Rollback()
-
- view, err := s.createViewImpl(ctx, tx.PTx, create)
- if err != nil {
- return nil, err
- }
-
- if err := tx.PTx.Commit(); err != nil {
- return nil, FormatError(err)
- }
-
- return view, nil
-}
-
// findViewRaw retrieves a list of views based on find.
func (s *Store) findViewRaw(ctx context.Context, find *api.ViewFind) ([]*viewRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -275,7 +285,7 @@ func (s *Store) findViewImpl(ctx context.Context, tx *sql.Tx, find *api.ViewFind
// deleteViewImpl permanently deletes views from a database.
func deleteViewImpl(ctx context.Context, tx *sql.Tx, delete *api.ViewDelete) error {
// Remove row from database.
- if _, err := tx.ExecContext(ctx, `DELETE FROM vw WHERE database_id = $1`, delete.DatabaseID); err != nil {
+ if _, err := tx.ExecContext(ctx, `DELETE FROM vw WHERE id = $1`, delete.ID); err != nil {
return FormatError(err)
}
return nil
diff --git a/store/view_test.go b/store/view_test.go
new file mode 100644
index 0000000000..59c11cf563
--- /dev/null
+++ b/store/view_test.go
@@ -0,0 +1,75 @@
+package store
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/stretchr/testify/require"
+)
+
+func TestGenerateViewActions(t *testing.T) {
+ tests := []struct {
+ oldViewMap map[string]*viewRaw
+ newViewMap map[string]*api.ViewCreate
+ wantDeletes []*api.ViewDelete
+ wantCreates []*api.ViewCreate
+ }{
+ {
+ oldViewMap: map[string]*viewRaw{
+ "view1": {ID: 123, Definition: "def1", Comment: "comment1"},
+ "view2": {ID: 124, Definition: "def2", Comment: "comment2"},
+ },
+ newViewMap: map[string]*api.ViewCreate{
+ "view1": {Name: "view1", Definition: "def1-change", Comment: "comment1"},
+ "view2": {Name: "view2", Definition: "def2", Comment: "comment2-change"},
+ "view3": {Name: "view3", Definition: "def3", Comment: "comment3"},
+ },
+ wantDeletes: []*api.ViewDelete{
+ {ID: 123},
+ {ID: 124},
+ },
+ wantCreates: []*api.ViewCreate{
+ {Name: "view1", Definition: "def1-change", Comment: "comment1"},
+ {Name: "view2", Definition: "def2", Comment: "comment2-change"},
+ {Name: "view3", Definition: "def3", Comment: "comment3"},
+ },
+ },
+ {
+ oldViewMap: map[string]*viewRaw{
+ "view1": {ID: 123, Definition: "def1", Comment: "comment1"},
+ },
+ newViewMap: nil,
+ wantDeletes: []*api.ViewDelete{
+ {ID: 123},
+ },
+ },
+ {
+ oldViewMap: nil,
+ newViewMap: map[string]*api.ViewCreate{
+ "view1": {Name: "view1", Definition: "def1", Comment: "comment1"},
+ "view2": {Name: "view2", Definition: "def2", Comment: "comment2"},
+ },
+ wantDeletes: nil,
+ wantCreates: []*api.ViewCreate{
+ {Name: "view1", Definition: "def1", Comment: "comment1"},
+ {Name: "view2", Definition: "def2", Comment: "comment2"},
+ },
+ },
+ {
+ oldViewMap: map[string]*viewRaw{
+ "view1": {ID: 123, Definition: "def1", Comment: "comment1"},
+ },
+ newViewMap: map[string]*api.ViewCreate{
+ "view1": {Name: "view1", Definition: "def1", Comment: "comment1"},
+ },
+ wantDeletes: nil,
+ wantCreates: nil,
+ },
+ }
+
+ for _, test := range tests {
+ deletes, creates := generateViewActions(test.oldViewMap, test.newViewMap)
+ require.Equal(t, test.wantDeletes, deletes)
+ require.Equal(t, test.wantCreates, creates)
+ }
+}