Skip to content

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

refactor: split data model object for api.VCS (#792)

Yilong Liee3d65e9adf2
diff --git a/api/project.go b/api/project.go
index 55ca3f71ed..9496c36292 100644
--- a/api/project.go
+++ b/api/project.go
@@ -65,6 +65,7 @@ const (
)
// ProjectRaw is the store model for a project.
+// Fields have exactly the same meanings as Project.
type ProjectRaw struct {
ID int
@@ -76,36 +77,35 @@ type ProjectRaw struct {
UpdatedTs int64
// Domain specific fields
- Name string
- Key string
- WorkflowType ProjectWorkflowType
- Visibility ProjectVisibility
- TenantMode ProjectTenantMode
- // DBNameTemplate is only used when a project is in tenant mode.
- // Empty value means {{DB_NAME}}.
+ Name string
+ Key string
+ WorkflowType ProjectWorkflowType
+ Visibility ProjectVisibility
+ TenantMode ProjectTenantMode
DBNameTemplate string
RoleProvider ProjectRoleProvider
}
-// CopyToProject copies fields of ProjectRaw to an instance of Project.
-// This is intented to be used when we need to compose a Project relationship.
-func (raw *ProjectRaw) CopyToProject(p *Project) {
- p.ID = raw.ID
-
- p.RowStatus = raw.RowStatus
- p.CreatorID = raw.CreatorID
- p.CreatedTs = raw.CreatedTs
- p.UpdaterID = raw.UpdaterID
- p.UpdatedTs = raw.UpdatedTs
-
- p.Name = raw.Name
- p.Key = raw.Key
- p.WorkflowType = raw.WorkflowType
- p.Visibility = raw.Visibility
- p.TenantMode = raw.TenantMode
-
- p.DBNameTemplate = raw.DBNameTemplate
- p.RoleProvider = raw.RoleProvider
+// ToProject creates an instance of Project based on the ProjectRaw.
+// This is intended to be used when we need to compose a Project relationship.
+func (raw *ProjectRaw) ToProject() *Project {
+ return &Project{
+ ID: raw.ID,
+
+ RowStatus: raw.RowStatus,
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ Name: raw.Name,
+ Key: raw.Key,
+ WorkflowType: raw.WorkflowType,
+ Visibility: raw.Visibility,
+ TenantMode: raw.TenantMode,
+ DBNameTemplate: raw.DBNameTemplate,
+ RoleProvider: raw.RoleProvider,
+ }
}
// Project is the API message for a project.
diff --git a/api/vcs.go b/api/vcs.go
index 1c09733f78..6ab9d29733 100644
--- a/api/vcs.go
+++ b/api/vcs.go
@@ -7,7 +7,47 @@ import (
"github.com/bytebase/bytebase/plugin/vcs"
)
-// VCS is the API message for VCS.
+// VCSRaw is the store model for a VCS (Version Control System).
+// Fields have exactly the same meanings as VCS.
+type VCSRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Domain specific fields
+ Name string
+ Type vcs.Type
+ InstanceURL string
+ APIURL string
+ ApplicationID string
+ Secret string
+}
+
+// ToVCS creates an instance of VCS based on the VCSRaw.
+// This is intended to be used when we need to compose a VCS relationship.
+func (raw *VCSRaw) ToVCS() *VCS {
+ return &VCS{
+ ID: raw.ID,
+
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ Name: raw.Name,
+ Type: raw.Type,
+ InstanceURL: raw.InstanceURL,
+ APIURL: raw.APIURL,
+ ApplicationID: raw.ApplicationID,
+ Secret: raw.Secret,
+ }
+}
+
+// VCS is the API message for a VCS (Version Control System).
type VCS struct {
ID int `jsonapi:"primary,vcs"`
@@ -84,9 +124,9 @@ type VCSDelete struct {
// VCSService is the service for VCSs.
type VCSService interface {
- CreateVCS(ctx context.Context, create *VCSCreate) (*VCS, error)
- FindVCSList(ctx context.Context, find *VCSFind) ([]*VCS, error)
- FindVCS(ctx context.Context, find *VCSFind) (*VCS, error)
- PatchVCS(ctx context.Context, patch *VCSPatch) (*VCS, error)
+ CreateVCS(ctx context.Context, create *VCSCreate) (*VCSRaw, error)
+ FindVCSList(ctx context.Context, find *VCSFind) ([]*VCSRaw, error)
+ FindVCS(ctx context.Context, find *VCSFind) (*VCSRaw, error)
+ PatchVCS(ctx context.Context, patch *VCSPatch) (*VCSRaw, error)
DeleteVCS(ctx context.Context, delete *VCSDelete) error
}
diff --git a/server/activity_manager.go b/server/activity_manager.go
index b14862622b..4bbb292e1d 100644
--- a/server/activity_manager.go
+++ b/server/activity_manager.go
@@ -79,7 +79,7 @@ func (m *ActivityManager) CreateActivity(ctx context.Context, create *api.Activi
return nil, fmt.Errorf("failed to find project ID %v for posting webhook event after changing the issue status %q", meta.issue.ProjectID, meta.issue.Name)
}
// TODO(dragonly): revisit the necessity of this function to depend on ActivityMeta.
- projectRaw.CopyToProject(meta.issue.Project)
+ meta.issue.Project = projectRaw.ToProject()
}
principalFind := &api.PrincipalFind{
diff --git a/server/project.go b/server/project.go
index 1b1e518304..e991aaf3ce 100644
--- a/server/project.go
+++ b/server/project.go
@@ -603,26 +603,26 @@ func (s *Server) composeProjectByID(ctx context.Context, id int) (*api.Project,
return project, nil
}
-func (s *Server) composeProjectRelationship(ctx context.Context, projectRaw *api.ProjectRaw) (*api.Project, error) {
- var err error
-
- project := &api.Project{}
- projectRaw.CopyToProject(project)
+func (s *Server) composeProjectRelationship(ctx context.Context, raw *api.ProjectRaw) (*api.Project, error) {
+ project := raw.ToProject()
- project.Creator, err = s.composePrincipalByID(ctx, project.CreatorID)
+ creator, err := s.composePrincipalByID(ctx, project.CreatorID)
if err != nil {
return nil, err
}
+ project.Creator = creator
- project.Updater, err = s.composePrincipalByID(ctx, project.UpdaterID)
+ updater, err := s.composePrincipalByID(ctx, project.UpdaterID)
if err != nil {
return nil, err
}
+ project.Updater = updater
- project.ProjectMemberList, err = s.composeProjectMemberListByProjectID(ctx, project.ID)
+ memberList, err := s.composeProjectMemberListByProjectID(ctx, project.ID)
if err != nil {
return nil, err
}
+ project.ProjectMemberList = memberList
return project, nil
}
diff --git a/server/vcs.go b/server/vcs.go
index 1b8d2b6069..0f4f05da98 100644
--- a/server/vcs.go
+++ b/server/vcs.go
@@ -27,12 +27,13 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
vcsCreate.InstanceURL = strings.TrimRight(vcsCreate.InstanceURL, "/")
vcsCreate.APIURL = vcs.Get(vcs.GitLabSelfHost, vcs.ProviderConfig{Logger: s.l}).APIURL(vcsCreate.InstanceURL)
- vcs, err := s.VCSService.CreateVCS(ctx, vcsCreate)
+ vcsRaw, err := s.VCSService.CreateVCS(ctx, vcsCreate)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create VCS").SetInternal(err)
}
- if err := s.composeVCSRelationship(ctx, vcs); err != nil {
+ vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch created VCS relationship").SetInternal(err)
}
@@ -46,15 +47,18 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
g.GET("/vcs", func(c echo.Context) error {
ctx := context.Background()
vcsFind := &api.VCSFind{}
- list, err := s.VCSService.FindVCSList(ctx, vcsFind)
+ vcsRawList, err := s.VCSService.FindVCSList(ctx, vcsFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch vcs list").SetInternal(err)
}
- for _, vcs := range list {
- if err := s.composeVCSRelationship(ctx, vcs); err != nil {
+ list := make([]*api.VCS, 0, len(vcsRawList))
+ for _, vcsRaw := range vcsRawList {
+ vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch vcs relationship: %v", vcs.ID)).SetInternal(err)
}
+ list = append(list, vcs)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -101,7 +105,7 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted change VCS request").SetInternal(err)
}
- vcs, err := s.VCSService.PatchVCS(ctx, vcsPatch)
+ vcsRaw, err := s.VCSService.PatchVCS(ctx, vcsPatch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS ID not found: %d", id))
@@ -109,7 +113,8 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to change VCS ID: %v", id)).SetInternal(err)
}
- if err := s.composeVCSRelationship(ctx, vcs); err != nil {
+ vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch updated VCS relationship").SetInternal(err)
}
@@ -173,31 +178,35 @@ func (s *Server) composeVCSByID(ctx context.Context, id int) (*api.VCS, error) {
vcsFind := &api.VCSFind{
ID: &id,
}
- vcs, err := s.VCSService.FindVCS(ctx, vcsFind)
+ vcsRaw, err := s.VCSService.FindVCS(ctx, vcsFind)
if err != nil {
return nil, err
}
- if vcs != nil {
- if err := s.composeVCSRelationship(ctx, vcs); err != nil {
+ if vcsRaw != nil {
+ vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
+ if err != nil {
return nil, err
}
+ return vcs, nil
}
- return vcs, nil
+ return nil, nil
}
-func (s *Server) composeVCSRelationship(ctx context.Context, vcs *api.VCS) error {
- var err error
+func (s *Server) composeVCSRelationship(ctx context.Context, raw *api.VCSRaw) (*api.VCS, error) {
+ vcs := raw.ToVCS()
- vcs.Creator, err = s.composePrincipalByID(ctx, vcs.CreatorID)
+ creator, err := s.composePrincipalByID(ctx, vcs.CreatorID)
if err != nil {
- return err
+ return nil, err
}
+ vcs.Creator = creator
- vcs.Updater, err = s.composePrincipalByID(ctx, vcs.UpdaterID)
+ updater, err := s.composePrincipalByID(ctx, vcs.UpdaterID)
if err != nil {
- return err
+ return nil, err
}
+ vcs.Updater = updater
- return nil
+ return vcs, nil
}
diff --git a/store/vcs.go b/store/vcs.go
index 9a5f24b206..f69eeb3cc1 100644
--- a/store/vcs.go
+++ b/store/vcs.go
@@ -27,7 +27,7 @@ func NewVCSService(logger *zap.Logger, db *DB) *VCSService {
}
// CreateVCS creates a new vcs.
-func (s *VCSService) CreateVCS(ctx context.Context, create *api.VCSCreate) (*api.VCS, error) {
+func (s *VCSService) CreateVCS(ctx context.Context, create *api.VCSCreate) (*api.VCSRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -46,8 +46,8 @@ func (s *VCSService) CreateVCS(ctx context.Context, create *api.VCSCreate) (*api
return vcs, nil
}
-// FindVCSList retrieves a list of vcss based on find.
-func (s *VCSService) FindVCSList(ctx context.Context, find *api.VCSFind) ([]*api.VCS, error) {
+// FindVCSList retrieves a list of VCSs based on find conditions.
+func (s *VCSService) FindVCSList(ctx context.Context, find *api.VCSFind) ([]*api.VCSRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -56,7 +56,7 @@ func (s *VCSService) FindVCSList(ctx context.Context, find *api.VCSFind) ([]*api
list, err := findVCSList(ctx, tx.PTx, find)
if err != nil {
- return []*api.VCS{}, err
+ return nil, err
}
return list, nil
@@ -64,7 +64,7 @@ func (s *VCSService) FindVCSList(ctx context.Context, find *api.VCSFind) ([]*api
// FindVCS retrieves a single vcs based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *VCSService) FindVCS(ctx context.Context, find *api.VCSFind) (*api.VCS, error) {
+func (s *VCSService) FindVCS(ctx context.Context, find *api.VCSFind) (*api.VCSRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -79,14 +79,14 @@ func (s *VCSService) FindVCS(ctx context.Context, find *api.VCSFind) (*api.VCS,
if len(list) == 0 {
return nil, nil
} else if len(list) > 1 {
- return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d vcss with filter %+v, expect 1", len(list), find)}
+ return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d VCSs with filter %+v, expect 1", len(list), find)}
}
return list[0], nil
}
// PatchVCS updates an existing vcs by ID.
// Returns ENOTFOUND if vcs does not exist.
-func (s *VCSService) PatchVCS(ctx context.Context, patch *api.VCSPatch) (*api.VCS, error) {
+func (s *VCSService) PatchVCS(ctx context.Context, patch *api.VCSPatch) (*api.VCSRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -125,7 +125,7 @@ func (s *VCSService) DeleteVCS(ctx context.Context, delete *api.VCSDelete) error
}
// createVCS creates a new vcs.
-func createVCS(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*api.VCS, error) {
+func createVCS(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*api.VCSRaw, error) {
// Insert row into database.
row, err := tx.QueryContext(ctx, `
INSERT INTO vcs (
@@ -157,7 +157,7 @@ func createVCS(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*api.VCS
defer row.Close()
row.Next()
- var vcs api.VCS
+ var vcs api.VCSRaw
if err := row.Scan(
&vcs.ID,
&vcs.CreatorID,
@@ -177,7 +177,7 @@ func createVCS(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*api.VCS
return &vcs, nil
}
-func findVCSList(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*api.VCS, error) {
+func findVCSList(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*api.VCSRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -207,9 +207,9 @@ func findVCSList(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*api.VCS
defer rows.Close()
// Iterate over result set and deserialize rows into list.
- list := make([]*api.VCS, 0)
+ var list []*api.VCSRaw
for rows.Next() {
- var vcs api.VCS
+ var vcs api.VCSRaw
if err := rows.Scan(
&vcs.ID,
&vcs.CreatorID,
@@ -236,7 +236,7 @@ func findVCSList(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*api.VCS
}
// patchVCS updates a vcs by ID. Returns the new state of the vcs after update.
-func patchVCS(ctx context.Context, tx *sql.Tx, patch *api.VCSPatch) (*api.VCS, error) {
+func patchVCS(ctx context.Context, tx *sql.Tx, patch *api.VCSPatch) (*api.VCSRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.Name; v != nil {
@@ -265,7 +265,7 @@ func patchVCS(ctx context.Context, tx *sql.Tx, patch *api.VCSPatch) (*api.VCS, e
defer row.Close()
if row.Next() {
- var vcs api.VCS
+ var vcs api.VCSRaw
if err := row.Scan(
&vcs.ID,
&vcs.CreatorID,