Skip to content

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

refactor: add ViewRaw to represent store model for View (#925)

Yilong Li28b912035d04
diff --git a/api/view.go b/api/view.go
index 7a6337f264..92b3ca5d7d 100644
--- a/api/view.go
+++ b/api/view.go
@@ -5,6 +5,48 @@ import (
"encoding/json"
)
+// ViewRaw is the store model for an View.
+// Fields have exactly the same meanings as View.
+type ViewRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ DatabaseID int
+
+ // Domain specific fields
+ Name string
+ Definition string
+ Comment string
+}
+
+// ToView creates an instance of View based on the ViewRaw.
+// This is intended to be called when we need to compose an View relationship.
+func (raw *ViewRaw) ToView() *View {
+ return &View{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ DatabaseID: raw.DatabaseID,
+
+ // Domain specific fields
+ Name: raw.Name,
+ Definition: raw.Definition,
+ Comment: raw.Comment,
+ }
+}
+
// View is the API message for a view.
type View struct {
ID int `jsonapi:"primary,view"`
@@ -71,8 +113,8 @@ type ViewDelete struct {
// ViewService is the service for views.
type ViewService interface {
- CreateView(ctx context.Context, create *ViewCreate) (*View, error)
- FindViewList(ctx context.Context, find *ViewFind) ([]*View, error)
- FindView(ctx context.Context, find *ViewFind) (*View, error)
+ CreateView(ctx context.Context, create *ViewCreate) (*ViewRaw, error)
+ FindViewList(ctx context.Context, find *ViewFind) ([]*ViewRaw, error)
+ FindView(ctx context.Context, find *ViewFind) (*ViewRaw, error)
DeleteView(ctx context.Context, delete *ViewDelete) error
}
diff --git a/server/database.go b/server/database.go
index 36715a5e4b..f3cd9b62c0 100644
--- a/server/database.go
+++ b/server/database.go
@@ -507,17 +507,21 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
viewFind := &api.ViewFind{
DatabaseID: &id,
}
- viewList, err := s.ViewService.FindViewList(ctx, viewFind)
+ viewRawList, err := s.ViewService.FindViewList(ctx, viewFind)
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch view list for database id: %d", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch view list for database ID: %d", id)).SetInternal(err)
}
-
+ var viewList []*api.View
+ for _, raw := range viewRawList {
+ view, err := s.composeViewRelationship(ctx, raw)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose view relationship with ID: %d", id)).SetInternal(err)
+ }
+ viewList = append(viewList, view)
+ }
+ // TODO(dragonly): should we do this in composeViewRelationship?
for _, view := range viewList {
view.Database = database
-
- if err := s.composeViewRelationship(ctx, view); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose view relationship").SetInternal(err)
- }
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -1103,19 +1107,21 @@ func (s *Server) composeTableRelationship(ctx context.Context, raw *api.TableRaw
return table, nil
}
-func (s *Server) composeViewRelationship(ctx context.Context, view *api.View) error {
- var err error
+func (s *Server) composeViewRelationship(ctx context.Context, raw *api.ViewRaw) (*api.View, error) {
+ view := raw.ToView()
- view.Creator, err = s.composePrincipalByID(ctx, view.CreatorID)
+ creator, err := s.composePrincipalByID(ctx, view.CreatorID)
if err != nil {
- return err
+ return nil, err
}
+ view.Creator = creator
- view.Updater, err = s.composePrincipalByID(ctx, view.UpdaterID)
+ updater, err := s.composePrincipalByID(ctx, view.UpdaterID)
if err != nil {
- return err
+ return nil, err
}
- return nil
+ view.Updater = updater
+ return view, nil
}
// composeBackupByID will compose the backup by backup ID.
diff --git a/server/sql.go b/server/sql.go
index 7cd60b617e..e265628d6b 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -271,13 +271,17 @@ func (s *Server) syncEngineVersionAndSchema(ctx context.Context, instance *api.I
}
var createView = func(database *api.Database, viewCreate *api.ViewCreate) (*api.View, error) {
- createView, err := s.ViewService.CreateView(ctx, viewCreate)
+ createViewRaw, err := s.ViewService.CreateView(ctx, viewCreate)
if err != nil {
if common.ErrorCode(err) == common.Conflict {
return nil, fmt.Errorf("failed to sync view for instance: %s, database: %s. View name already exists: %s", instance.Name, database.Name, viewCreate.Name)
}
return nil, fmt.Errorf("failed to sync view for instance: %s, database: %s. Failed to import new view: %s. Error %w", instance.Name, database.Name, viewCreate.Name, err)
}
+ createView, err := s.composeViewRelationship(ctx, createViewRaw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose view relationship for instance: %s, database: %s. Failed to import new view: %s. Error %w", instance.Name, database.Name, viewCreate.Name, err)
+ }
return createView, nil
}
diff --git a/store/view.go b/store/view.go
index 238c9aaead..d7d288550c 100644
--- a/store/view.go
+++ b/store/view.go
@@ -27,7 +27,7 @@ func NewViewService(logger *zap.Logger, db *DB) *ViewService {
}
// CreateView creates a new view.
-func (s *ViewService) CreateView(ctx context.Context, create *api.ViewCreate) (*api.View, error) {
+func (s *ViewService) CreateView(ctx context.Context, create *api.ViewCreate) (*api.ViewRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -47,7 +47,7 @@ func (s *ViewService) CreateView(ctx context.Context, create *api.ViewCreate) (*
}
// FindViewList retrieves a list of views based on find.
-func (s *ViewService) FindViewList(ctx context.Context, find *api.ViewFind) ([]*api.View, error) {
+func (s *ViewService) FindViewList(ctx context.Context, find *api.ViewFind) ([]*api.ViewRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -64,7 +64,7 @@ func (s *ViewService) FindViewList(ctx context.Context, find *api.ViewFind) ([]*
// FindView retrieves a single view based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *ViewService) FindView(ctx context.Context, find *api.ViewFind) (*api.View, error) {
+func (s *ViewService) FindView(ctx context.Context, find *api.ViewFind) (*api.ViewRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -104,7 +104,7 @@ func (s *ViewService) DeleteView(ctx context.Context, delete *api.ViewDelete) er
}
// createView creates a new view.
-func (s *ViewService) createView(ctx context.Context, tx *sql.Tx, create *api.ViewCreate) (*api.View, error) {
+func (s *ViewService) createView(ctx context.Context, tx *sql.Tx, create *api.ViewCreate) (*api.ViewRaw, error) {
// Insert row into view.
row, err := tx.QueryContext(ctx, `
INSERT INTO vw (
@@ -136,25 +136,25 @@ func (s *ViewService) createView(ctx context.Context, tx *sql.Tx, create *api.Vi
defer row.Close()
row.Next()
- var view api.View
+ var viewRaw api.ViewRaw
if err := row.Scan(
- &view.ID,
- &view.CreatorID,
- &view.CreatedTs,
- &view.UpdaterID,
- &view.UpdatedTs,
- &view.DatabaseID,
- &view.Name,
- &view.Definition,
- &view.Comment,
+ &viewRaw.ID,
+ &viewRaw.CreatorID,
+ &viewRaw.CreatedTs,
+ &viewRaw.UpdaterID,
+ &viewRaw.UpdatedTs,
+ &viewRaw.DatabaseID,
+ &viewRaw.Name,
+ &viewRaw.Definition,
+ &viewRaw.Comment,
); err != nil {
return nil, FormatError(err)
}
- return &view, nil
+ return &viewRaw, nil
}
-func (s *ViewService) findViewList(ctx context.Context, tx *sql.Tx, find *api.ViewFind) ([]*api.View, error) {
+func (s *ViewService) findViewList(ctx context.Context, tx *sql.Tx, find *api.ViewFind) ([]*api.ViewRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -188,31 +188,31 @@ func (s *ViewService) findViewList(ctx context.Context, tx *sql.Tx, find *api.Vi
}
defer rows.Close()
- // Iterate over result set and deserialize rows into viewList.
- var viewList []*api.View
+ // Iterate over result set and deserialize rows into viewRawList.
+ var viewRawList []*api.ViewRaw
for rows.Next() {
- var view api.View
+ var viewRaw api.ViewRaw
if err := rows.Scan(
- &view.ID,
- &view.CreatorID,
- &view.CreatedTs,
- &view.UpdaterID,
- &view.UpdatedTs,
- &view.DatabaseID,
- &view.Name,
- &view.Definition,
- &view.Comment,
+ &viewRaw.ID,
+ &viewRaw.CreatorID,
+ &viewRaw.CreatedTs,
+ &viewRaw.UpdaterID,
+ &viewRaw.UpdatedTs,
+ &viewRaw.DatabaseID,
+ &viewRaw.Name,
+ &viewRaw.Definition,
+ &viewRaw.Comment,
); err != nil {
return nil, FormatError(err)
}
- viewList = append(viewList, &view)
+ viewRawList = append(viewRawList, &viewRaw)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return viewList, nil
+ return viewRawList, nil
}
// deleteView permanently deletes views from a database.