Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: add SheetRaw to represent store model for Sheet (#917)
Yilong Lid5354bee2fe7
diff --git a/api/sheet.go b/api/sheet.goindex def0d98063..fa7b57e445 100644--- a/api/sheet.go+++ b/api/sheet.go@@ -29,6 +29,56 @@ func (v SheetVisibility) String() string {return ""}+// SheetRaw is the store model for an Sheet.+// Fields have exactly the same meanings as Sheet.+type SheetRaw struct {+ ID int++ // Standard fields+ CreatorID int+ CreatedTs int64+ UpdaterID int+ UpdatedTs int64++ // Related fields+ ProjectID int+ // The DatabaseID is optional.+ // If not NULL, the sheet ProjectID should always be equal to the id of the database related project.+ // A project must remove all linked sheets for a particular database before that database can be transferred to a different project.+ DatabaseID *int++ // Domain specific fields+ Name string+ Statement string+ Visibility SheetVisibility+}++// ToSheet creates an instance of Sheet based on the SheetRaw.+// This is intended to be called when we need to compose an Sheet relationship.+func (raw *SheetRaw) ToSheet() *Sheet {+ return &Sheet{+ ID: raw.ID,++ // Standard fields+ CreatorID: raw.CreatorID,+ CreatedTs: raw.CreatedTs,+ UpdaterID: raw.UpdaterID,+ UpdatedTs: raw.UpdatedTs,++ // Related fields+ ProjectID: raw.ProjectID,+ // The DatabaseID is optional.+ // If not NULL, the sheet ProjectID should always be equal to the id of the database related project.+ // A project must remove all linked sheets for a particular database before that database can be transferred to a different project.+ DatabaseID: raw.DatabaseID,++ // Domain specific fields+ Name: raw.Name,+ Statement: raw.Statement,+ Visibility: raw.Visibility,+ }+}+// Sheet is the API message for a sheet.type Sheet struct {ID int `jsonapi:"primary,sheet"`@@ -126,9 +176,9 @@ type SheetDelete struct {// SheetService is the service for sheet.type SheetService interface {- CreateSheet(ctx context.Context, create *SheetCreate) (*Sheet, error)- PatchSheet(ctx context.Context, patch *SheetPatch) (*Sheet, error)- FindSheetList(ctx context.Context, find *SheetFind) ([]*Sheet, error)- FindSheet(ctx context.Context, find *SheetFind) (*Sheet, error)+ CreateSheet(ctx context.Context, create *SheetCreate) (*SheetRaw, error)+ PatchSheet(ctx context.Context, patch *SheetPatch) (*SheetRaw, error)+ FindSheetList(ctx context.Context, find *SheetFind) ([]*SheetRaw, error)+ FindSheet(ctx context.Context, find *SheetFind) (*SheetRaw, error)DeleteSheet(ctx context.Context, delete *SheetDelete) error}diff --git a/server/sheet.go b/server/sheet.goindex 18251e5406..e2d6d10e8b 100644--- a/server/sheet.go+++ b/server/sheet.go@@ -51,13 +51,13 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {sheetCreate.CreatorID = c.Get(getPrincipalIDContextKey()).(int)- sheet, err := s.SheetService.CreateSheet(ctx, sheetCreate)+ sheetRaw, err := s.SheetService.CreateSheet(ctx, sheetCreate)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create sheet").SetInternal(err)}-- if err := s.composeSheetRelationship(ctx, sheet); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch created sheet relationship").SetInternal(err)+ sheet, err := s.composeSheetRelationship(ctx, sheetRaw)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose sheet with ID %d", sheetRaw.ID)).SetInternal(err)}c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)@@ -98,15 +98,17 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {sheetFind.Visibility = &visibility}- sheetList, err := s.SheetService.FindSheetList(ctx, sheetFind)+ sheetRawList, err := s.SheetService.FindSheetList(ctx, sheetFind)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch sheet list").SetInternal(err)}-- for _, sheet := range sheetList {- if err := s.composeSheetRelationship(ctx, sheet); err != nil {+ var sheetList []*api.Sheet+ for _, sheetRaw := range sheetRawList {+ sheet, err := s.composeSheetRelationship(ctx, sheetRaw)+ if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch sheet relationship: %v", sheet.Name)).SetInternal(err)}+ sheetList = append(sheetList, sheet)}c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)@@ -127,15 +129,16 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {ID: &id,}- sheet, err := s.SheetService.FindSheet(ctx, sheetFind)+ sheetRaw, err := s.SheetService.FindSheet(ctx, sheetFind)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch sheet ID: %v", id)).SetInternal(err)}- if sheet == nil {+ if sheetRaw == nil {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("sheet ID not found: %d", id))}- if err := s.composeSheetRelationship(ctx, sheet); err != nil {+ sheet, err := s.composeSheetRelationship(ctx, sheetRaw)+ if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch sheet relationship: %v", sheet.ID)).SetInternal(err)}@@ -161,7 +164,7 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch sheet request").SetInternal(err)}- sheet, err := s.SheetService.PatchSheet(ctx, sheetPatch)+ sheetRaw, err := s.SheetService.PatchSheet(ctx, sheetPatch)if err != nil {if common.ErrorCode(err) == common.NotFound {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("sheet ID not found: %d", id))@@ -169,7 +172,8 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch sheet ID: %v", id)).SetInternal(err)}- if err := s.composeSheetRelationship(ctx, sheet); err != nil {+ sheet, err := s.composeSheetRelationship(ctx, sheetRaw)+ if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch updated sheet relationship: %v", sheet.ID)).SetInternal(err)}@@ -202,32 +206,36 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {})}-func (s *Server) composeSheetRelationship(ctx context.Context, sheet *api.Sheet) error {- var err error+func (s *Server) composeSheetRelationship(ctx context.Context, raw *api.SheetRaw) (*api.Sheet, error) {+ sheet := raw.ToSheet()- sheet.Creator, err = s.composePrincipalByID(ctx, sheet.CreatorID)+ creator, err := s.composePrincipalByID(ctx, sheet.CreatorID)if err != nil {- return err+ return nil, err}+ sheet.Creator = creator- sheet.Updater, err = s.composePrincipalByID(ctx, sheet.UpdaterID)+ updater, err := s.composePrincipalByID(ctx, sheet.UpdaterID)if err != nil {- return err+ return nil, err}+ sheet.Updater = updater- sheet.Project, err = s.composeProjectByID(ctx, sheet.ProjectID)+ project, err := s.composeProjectByID(ctx, sheet.ProjectID)if err != nil {- return err+ return nil, err}+ sheet.Project = projectif sheet.DatabaseID != nil {- sheet.Database, err = s.composeDatabaseByFind(ctx, &api.DatabaseFind{+ database, err := s.composeDatabaseByFind(ctx, &api.DatabaseFind{ID: sheet.DatabaseID,})if err != nil {- return err+ return nil, err}+ sheet.Database = database}- return nil+ return sheet, nil}diff --git a/store/sheet.go b/store/sheet.goindex baf20abbb4..bb0f753cde 100644--- a/store/sheet.go+++ b/store/sheet.go@@ -27,7 +27,7 @@ func NewSheetService(logger *zap.Logger, db *DB) *SheetService {}// CreateSheet creates a new sheet.-func (s *SheetService) CreateSheet(ctx context.Context, create *api.SheetCreate) (*api.Sheet, error) {+func (s *SheetService) CreateSheet(ctx context.Context, create *api.SheetCreate) (*api.SheetRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -47,7 +47,7 @@ func (s *SheetService) CreateSheet(ctx context.Context, create *api.SheetCreate)}// PatchSheet updates an existing sheet by ID.-func (s *SheetService) PatchSheet(ctx context.Context, patch *api.SheetPatch) (*api.Sheet, error) {+func (s *SheetService) PatchSheet(ctx context.Context, patch *api.SheetPatch) (*api.SheetRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -67,7 +67,7 @@ func (s *SheetService) PatchSheet(ctx context.Context, patch *api.SheetPatch) (*}// FindSheetList retrieves a list of sheet based on find.-func (s *SheetService) FindSheetList(ctx context.Context, find *api.SheetFind) ([]*api.Sheet, error) {+func (s *SheetService) FindSheetList(ctx context.Context, find *api.SheetFind) ([]*api.SheetRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -84,7 +84,7 @@ func (s *SheetService) FindSheetList(ctx context.Context, find *api.SheetFind) (// FindSheet retrieves a single sheet based on find.// Returns ECONFLICT if finding more than 1 matching records.-func (s *SheetService) FindSheet(ctx context.Context, find *api.SheetFind) (*api.Sheet, error) {+func (s *SheetService) FindSheet(ctx context.Context, find *api.SheetFind) (*api.SheetRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -125,7 +125,7 @@ func (s *SheetService) DeleteSheet(ctx context.Context, delete *api.SheetDelete)}// createSheet creates a new sheet.-func createSheet(ctx context.Context, tx *sql.Tx, create *api.SheetCreate) (*api.Sheet, error) {+func createSheet(ctx context.Context, tx *sql.Tx, create *api.SheetCreate) (*api.SheetRaw, error) {row, err := tx.QueryContext(ctx, `INSERT INTO sheet (creator_id,@@ -154,33 +154,33 @@ func createSheet(ctx context.Context, tx *sql.Tx, create *api.SheetCreate) (*apidefer row.Close()row.Next()- var sheet api.Sheet+ var sheetRaw api.SheetRawdatabaseID := sql.NullInt32{}if err := row.Scan(- &sheet.ID,- &sheet.CreatorID,- &sheet.CreatedTs,- &sheet.UpdaterID,- &sheet.UpdatedTs,- &sheet.ProjectID,+ &sheetRaw.ID,+ &sheetRaw.CreatorID,+ &sheetRaw.CreatedTs,+ &sheetRaw.UpdaterID,+ &sheetRaw.UpdatedTs,+ &sheetRaw.ProjectID,&databaseID,- &sheet.Name,- &sheet.Statement,- &sheet.Visibility,+ &sheetRaw.Name,+ &sheetRaw.Statement,+ &sheetRaw.Visibility,); err != nil {return nil, FormatError(err)}if databaseID.Valid {value := int(databaseID.Int32)- sheet.DatabaseID = &value+ sheetRaw.DatabaseID = &value}- return &sheet, nil+ return &sheetRaw, nil}// patchSheet updates a sheet's name/statement/visibility.-func patchSheet(ctx context.Context, tx *sql.Tx, patch *api.SheetPatch) (*api.Sheet, error) {+func patchSheet(ctx context.Context, tx *sql.Tx, patch *api.SheetPatch) (*api.SheetRaw, error) {set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}if v := patch.Name; v != nil {set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, api.RowStatus(*v))@@ -209,35 +209,35 @@ func patchSheet(ctx context.Context, tx *sql.Tx, patch *api.SheetPatch) (*api.Shdefer row.Close()if row.Next() {- var sheet api.Sheet+ var sheetRaw api.SheetRawdatabaseID := sql.NullInt32{}if err := row.Scan(- &sheet.ID,- &sheet.CreatorID,- &sheet.CreatedTs,- &sheet.UpdaterID,- &sheet.UpdatedTs,- &sheet.ProjectID,+ &sheetRaw.ID,+ &sheetRaw.CreatorID,+ &sheetRaw.CreatedTs,+ &sheetRaw.UpdaterID,+ &sheetRaw.UpdatedTs,+ &sheetRaw.ProjectID,&databaseID,- &sheet.Name,- &sheet.Statement,- &sheet.Visibility,+ &sheetRaw.Name,+ &sheetRaw.Statement,+ &sheetRaw.Visibility,); err != nil {return nil, FormatError(err)}if databaseID.Valid {value := int(databaseID.Int32)- sheet.DatabaseID = &value+ sheetRaw.DatabaseID = &value}- return &sheet, nil+ return &sheetRaw, nil}return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("sheet ID not found: %d", patch.ID)}}-func findSheetList(ctx context.Context, tx *sql.Tx, find *api.SheetFind) ([]*api.Sheet, error) {+func findSheetList(ctx context.Context, tx *sql.Tx, find *api.SheetFind) ([]*api.SheetRaw, error) {where, args := []string{"1 = 1"}, []interface{}{}// Standard fieldsif v := find.ID; v != nil {@@ -284,37 +284,37 @@ func findSheetList(ctx context.Context, tx *sql.Tx, find *api.SheetFind) ([]*api}defer rows.Close()- var sheetList []*api.Sheet+ var sheetRawList []*api.SheetRawfor rows.Next() {- var sheet api.Sheet+ var sheetRaw api.SheetRawdatabaseID := sql.NullInt32{}if err := rows.Scan(- &sheet.ID,- &sheet.CreatorID,- &sheet.CreatedTs,- &sheet.UpdaterID,- &sheet.UpdatedTs,- &sheet.ProjectID,+ &sheetRaw.ID,+ &sheetRaw.CreatorID,+ &sheetRaw.CreatedTs,+ &sheetRaw.UpdaterID,+ &sheetRaw.UpdatedTs,+ &sheetRaw.ProjectID,&databaseID,- &sheet.Name,- &sheet.Statement,- &sheet.Visibility,+ &sheetRaw.Name,+ &sheetRaw.Statement,+ &sheetRaw.Visibility,); err != nil {return nil, FormatError(err)}if databaseID.Valid {value := int(databaseID.Int32)- sheet.DatabaseID = &value+ sheetRaw.DatabaseID = &value}- sheetList = append(sheetList, &sheet)+ sheetRawList = append(sheetRawList, &sheetRaw)}if err := rows.Err(); err != nil {return nil, FormatError(err)}- return sheetList, nil+ return sheetRawList, nil}// deleteSheet permanently deletes a sheet by ID.