Skip to content

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

chore(backend): update backend for sql review in vcs (#2980)

ecmadao88fec1883068
diff --git a/api/repository.go b/api/repository.go
index 487a322491..77f6e4224e 100644
--- a/api/repository.go
+++ b/api/repository.go
@@ -34,12 +34,19 @@ type Repository struct {
// If empty, then Bytebase won't auto generate it.
SchemaPathTemplate string `jsonapi:"attr,schemaPathTemplate"`
// The file path template for matching the sql files for sheet.
- SheetPathTemplate string `jsonapi:"attr,sheetPathTemplate"`
- ExternalID string `jsonapi:"attr,externalId"`
- ExternalWebhookID string
- WebhookURLHost string
- WebhookEndpointID string
- WebhookSecretToken string
+ SheetPathTemplate string `jsonapi:"attr,sheetPathTemplate"`
+ // Setup CI to do SQL review for all PRs.
+ EnableSQLReviewCI bool `jsonapi:"attr,enableSQLReviewCI"`
+ // SQLReviewCIPullRequestURL is the pull request URL to setup the SQL review CI.
+ // This field is used by the frontend to redirect users to the pull request.
+ // We don't need to persist it in the storage,
+ // only return this value if we need to auto-create the pull request for users.
+ SQLReviewCIPullRequestURL string `jsonapi:"attr,sqlReviewCIPullRequestURL"`
+ ExternalID string `jsonapi:"attr,externalId"`
+ ExternalWebhookID string
+ WebhookURLHost string
+ WebhookEndpointID string
+ WebhookSecretToken string
// These will be exclusively used on the server side and we don't return it to the client.
AccessToken string
ExpiresTs int64
@@ -65,6 +72,7 @@ type RepositoryCreate struct {
FilePathTemplate string `jsonapi:"attr,filePathTemplate"`
SchemaPathTemplate string `jsonapi:"attr,schemaPathTemplate"`
SheetPathTemplate string `jsonapi:"attr,sheetPathTemplate"`
+ EnableSQLReviewCI bool `jsonapi:"attr,enableSQLReviewCI"`
ExternalID string `jsonapi:"attr,externalId"`
// Token belonged by the user linking the project to the VCS repository. We store this token together
// with the refresh token in the new repository record so we can use it to call VCS API on
@@ -115,6 +123,7 @@ type RepositoryPatch struct {
FilePathTemplate *string `jsonapi:"attr,filePathTemplate"`
SchemaPathTemplate *string `jsonapi:"attr,schemaPathTemplate"`
SheetPathTemplate *string `jsonapi:"attr,sheetPathTemplate"`
+ EnableSQLReviewCI *bool `jsonapi:"attr,enableSQLReviewCI"`
AccessToken *string
ExpiresTs *int64
RefreshToken *string
diff --git a/plugin/vcs/github/github.go b/plugin/vcs/github/github.go
index fe28fd9b45..db2f814d4d 100644
--- a/plugin/vcs/github/github.go
+++ b/plugin/vcs/github/github.go
@@ -1000,13 +1000,17 @@ func (p *Provider) CreateBranch(ctx context.Context, oauthCtx common.OauthContex
return nil
}
+type githubPullRequest struct {
+ HTMLURL string `json:"html_url"`
+}
+
// CreatePullRequest creates the pull request in the repository.
//
// Docs: https://docs.github.com/en/rest/pulls/pulls#create-a-pull-request
-func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID string, pullRequestCreate *vcs.PullRequestCreate) error {
+func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID string, pullRequestCreate *vcs.PullRequestCreate) (*vcs.PullRequest, error) {
body, err := json.Marshal(pullRequestCreate)
if err != nil {
- return errors.Wrap(err, "marshal pull request create")
+ return nil, errors.Wrap(err, "marshal pull request create")
}
url := fmt.Sprintf("%s/repos/%s/pulls", p.APIURL(instanceURL), repositoryID)
@@ -1027,20 +1031,27 @@ func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthC
),
)
if err != nil {
- return errors.Wrapf(err, "GET %s", url)
+ return nil, errors.Wrapf(err, "GET %s", url)
}
if code == http.StatusNotFound {
- return common.Errorf(common.NotFound, "failed to create pull request from URL %s", url)
+ return nil, common.Errorf(common.NotFound, "failed to create pull request from URL %s", url)
} else if code >= 300 {
- return errors.Errorf("failed to create pull request from URL %s, status code: %d, body: %s",
+ return nil, errors.Errorf("failed to create pull request from URL %s, status code: %d, body: %s",
url,
code,
resp,
)
}
- return nil
+ var res githubPullRequest
+ if err := json.Unmarshal([]byte(resp), &res); err != nil {
+ return nil, err
+ }
+
+ return &vcs.PullRequest{
+ URL: res.HTMLURL,
+ }, nil
}
type environmentVariable struct {
diff --git a/plugin/vcs/github/github_test.go b/plugin/vcs/github/github_test.go
index d4bdfbfe2f..b4a8794057 100644
--- a/plugin/vcs/github/github_test.go
+++ b/plugin/vcs/github/github_test.go
@@ -1280,7 +1280,7 @@ func TestProvider_CreatePullRequest(t *testing.T) {
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
- },
+ }
},
"base": {
"label": "octocat:master",
@@ -1305,7 +1305,7 @@ func TestProvider_CreatePullRequest(t *testing.T) {
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
- },
+ }
},
"author_association": "OWNER",
"auto_merge": null,
@@ -1331,7 +1331,7 @@ func TestProvider_CreatePullRequest(t *testing.T) {
)
ctx := context.Background()
- err := p.CreatePullRequest(ctx, common.OauthContext{}, githubComURL, "octocat/Hello-World", &vcs.PullRequestCreate{
+ res, err := p.CreatePullRequest(ctx, common.OauthContext{}, githubComURL, "octocat/Hello-World", &vcs.PullRequestCreate{
Title: "Amazing new feature",
Body: "Please pull these awesome changes in!",
Head: "new-topic",
@@ -1339,6 +1339,7 @@ func TestProvider_CreatePullRequest(t *testing.T) {
RemoveHeadAfterMerged: true,
})
require.NoError(t, err)
+ assert.Equal(t, "https://github.com/octocat/Hello-World/pull/1347", res.URL)
}
func TestProvider_UpsertEnvironmentVariable(t *testing.T) {
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index ae56881f01..32d8f45f03 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -932,10 +932,14 @@ func (p *Provider) CreateBranch(ctx context.Context, oauthCtx common.OauthContex
return nil
}
+type gitlabMergeRequest struct {
+ WebURL string `json:"web_url"`
+}
+
// CreatePullRequest creates the pull request in the repository.
//
// Docs: https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
-func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID string, pullRequestCreate *vcs.PullRequestCreate) error {
+func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID string, pullRequestCreate *vcs.PullRequestCreate) (*vcs.PullRequest, error) {
body, err := json.Marshal(
gitlabMergeRequestCreate{
Title: pullRequestCreate.Title,
@@ -946,7 +950,7 @@ func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthC
},
)
if err != nil {
- return errors.Wrap(err, "marshal pull request create")
+ return nil, errors.Wrap(err, "marshal pull request create")
}
url := fmt.Sprintf("%s/projects/%s/merge_requests", p.APIURL(instanceURL), repositoryID)
@@ -967,20 +971,27 @@ func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthC
),
)
if err != nil {
- return errors.Wrapf(err, "GET %s", url)
+ return nil, errors.Wrapf(err, "GET %s", url)
}
if code == http.StatusNotFound {
- return common.Errorf(common.NotFound, "failed to create merge request from URL %s", url)
+ return nil, common.Errorf(common.NotFound, "failed to create merge request from URL %s", url)
} else if code >= 300 {
- return errors.Errorf("failed to create merge request from URL %s, status code: %d, body: %s",
+ return nil, errors.Errorf("failed to create merge request from URL %s, status code: %d, body: %s",
url,
code,
resp,
)
}
- return nil
+ var res gitlabMergeRequest
+ if err := json.Unmarshal([]byte(resp), &res); err != nil {
+ return nil, err
+ }
+
+ return &vcs.PullRequest{
+ URL: res.WebURL,
+ }, nil
}
type environmentVariable struct {
diff --git a/plugin/vcs/gitlab/gitlab_test.go b/plugin/vcs/gitlab/gitlab_test.go
index e13190e05b..65bf5b02a0 100644
--- a/plugin/vcs/gitlab/gitlab_test.go
+++ b/plugin/vcs/gitlab/gitlab_test.go
@@ -999,7 +999,7 @@ func TestProvider_CreatePullRequest(t *testing.T) {
"head_sha": "2be7ddb704c7b6b83732fdd5b9f09d5a397b5f8f",
"start_sha": "c380d3acebd181f13629a25d2e2acca46ffe1e00"
},
- "diverged_commits_count": 2,
+ "diverged_commits_count": 2
}
`)),
}, nil
@@ -1010,7 +1010,7 @@ func TestProvider_CreatePullRequest(t *testing.T) {
)
ctx := context.Background()
- err := p.CreatePullRequest(ctx, common.OauthContext{}, "", "1", &vcs.PullRequestCreate{
+ res, err := p.CreatePullRequest(ctx, common.OauthContext{}, "", "1", &vcs.PullRequestCreate{
Title: "test1",
Body: "fixed login page css paddings",
Head: "test1",
@@ -1018,6 +1018,7 @@ func TestProvider_CreatePullRequest(t *testing.T) {
RemoveHeadAfterMerged: true,
})
require.NoError(t, err)
+ assert.Equal(t, "http://gitlab.example.com/my-group/my-project/merge_requests/1", res.URL)
}
func TestProvider_UpsertEnvironmentVariable(t *testing.T) {
diff --git a/plugin/vcs/vcs.go b/plugin/vcs/vcs.go
index 7fd2ccd6e2..cb71de8b67 100644
--- a/plugin/vcs/vcs.go
+++ b/plugin/vcs/vcs.go
@@ -163,6 +163,11 @@ type PullRequestCreate struct {
RemoveHeadAfterMerged bool `json:"-"`
}
+// PullRequest is the API message for pull request in repository.
+type PullRequest struct {
+ URL string `json:"url"`
+}
+
// Provider is the interface for VCS provider.
type Provider interface {
// Returns the API URL for a given VCS instance URL
@@ -264,7 +269,7 @@ type Provider interface {
// pullRequestID: the pull request id
ListPullRequestFile(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID, pullRequestID string) ([]*PullRequestFile, error)
// pullRequestCreate: the new pull request info
- CreatePullRequest(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID string, pullRequestCreate *PullRequestCreate) error
+ CreatePullRequest(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID string, pullRequestCreate *PullRequestCreate) (*PullRequest, error)
// UpsertEnvironmentVariable creates or updates the environment variable in the repository.
//
// oauthCtx: OAuth context to create the webhook
diff --git a/server/project.go b/server/project.go
index 8bfa23b8a1..0ec5b0020f 100644
--- a/server/project.go
+++ b/server/project.go
@@ -318,10 +318,12 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
// Setup SQL review CI for VCS
- if api.FeatureFlight[api.FeatureVCSSQLReviewWorkflow] && s.feature(api.FeatureVCSSQLReviewWorkflow) {
- if err := s.setupVCSSQLReviewCI(ctx, repository); err != nil {
+ if repository.EnableSQLReviewCI && api.FeatureFlight[api.FeatureVCSSQLReviewWorkflow] && s.feature(api.FeatureVCSSQLReviewWorkflow) {
+ pullRequest, err := s.setupVCSSQLReviewCI(ctx, repository)
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create SQL review CI").SetInternal(err)
}
+ repository.SQLReviewCIPullRequestURL = pullRequest.URL
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -441,6 +443,15 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update repository for project ID: %d", projectID)).SetInternal(err)
}
+ // Setup SQL review CI for VCS
+ if !repo.EnableSQLReviewCI && updatedRepo.EnableSQLReviewCI && api.FeatureFlight[api.FeatureVCSSQLReviewWorkflow] && s.feature(api.FeatureVCSSQLReviewWorkflow) {
+ pullRequest, err := s.setupVCSSQLReviewCI(ctx, updatedRepo)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create SQL review CI").SetInternal(err)
+ }
+ updatedRepo.SQLReviewCIPullRequestURL = pullRequest.URL
+ }
+
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, updatedRepo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal project repository response: %v", projectID)).SetInternal(err)
@@ -590,10 +601,10 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
})
}
-func (s *Server) setupVCSSQLReviewCI(ctx context.Context, repository *api.Repository) error {
+func (s *Server) setupVCSSQLReviewCI(ctx context.Context, repository *api.Repository) (*vcsPlugin.PullRequest, error) {
branch, err := s.setupVCSSQLReviewBranch(ctx, repository)
if err != nil {
- return err
+ return nil, err
}
if err := vcsPlugin.Get(repository.VCS.Type, vcsPlugin.ProviderConfig{}).UpsertEnvironmentVariable(
@@ -610,7 +621,7 @@ func (s *Server) setupVCSSQLReviewCI(ctx context.Context, repository *api.Reposi
vcsPlugin.SQLReviewAPISecretName,
repository.WebhookSecretToken,
); err != nil {
- return err
+ return nil, err
}
sqlReviewEndpoint := fmt.Sprintf("%s/hook/sql-review/%s", s.profile.ExternalURL, repository.WebhookEndpointID)
@@ -618,11 +629,11 @@ func (s *Server) setupVCSSQLReviewCI(ctx context.Context, repository *api.Reposi
switch repository.VCS.Type {
case vcsPlugin.GitHubCom:
if err := s.setupVCSSQLReviewCIForGitHub(ctx, repository, branch, sqlReviewEndpoint); err != nil {
- return err
+ return nil, err
}
case vcsPlugin.GitLabSelfHost:
if err := s.setupVCSSQLReviewCIForGitLab(ctx, repository, branch, sqlReviewEndpoint); err != nil {
- return err
+ return nil, err
}
}
diff --git a/store/migration/dev/20221012000000##add_repository_enable_sql_review_ci.sql b/store/migration/dev/20221012000000##add_repository_enable_sql_review_ci.sql
new file mode 100644
index 0000000000..010b3ebf73
--- /dev/null
+++ b/store/migration/dev/20221012000000##add_repository_enable_sql_review_ci.sql
@@ -0,0 +1 @@
+ALTER TABLE repository ADD COLUMN IF NOT EXISTS enable_sql_review_ci BOOLEAN NOT NULL DEFAULT false;
diff --git a/store/migration/dev/LATEST.sql b/store/migration/dev/LATEST.sql
index 0297383ad9..8ff18d041b 100644
--- a/store/migration/dev/LATEST.sql
+++ b/store/migration/dev/LATEST.sql
@@ -929,6 +929,8 @@ CREATE TABLE repository (
schema_path_template TEXT NOT NULL DEFAULT '',
-- The file path template to match the script file for sheet.
sheet_path_template TEXT NOT NULL DEFAULT '',
+ -- If enable the SQL review CI in VCS repository.
+ enable_sql_review_ci BOOLEAN NOT NULL DEFAULT false,
-- Repository id from the corresponding VCS provider.
-- For GitLab, this is the project id. e.g. 123
external_id TEXT NOT NULL,
diff --git a/store/repository.go b/store/repository.go
index b53f6b6898..e293b0060e 100644
--- a/store/repository.go
+++ b/store/repository.go
@@ -36,6 +36,7 @@ type repositoryRaw struct {
FilePathTemplate string
SchemaPathTemplate string
SheetPathTemplate string
+ EnableSQLReviewCI bool
ExternalID string
ExternalWebhookID string
WebhookURLHost string
@@ -68,6 +69,7 @@ func (raw *repositoryRaw) toRepository() *api.Repository {
FilePathTemplate: raw.FilePathTemplate,
SchemaPathTemplate: raw.SchemaPathTemplate,
SheetPathTemplate: raw.SheetPathTemplate,
+ EnableSQLReviewCI: raw.EnableSQLReviewCI,
ExternalID: raw.ExternalID,
ExternalWebhookID: raw.ExternalWebhookID,
WebhookURLHost: raw.WebhookURLHost,
@@ -223,7 +225,7 @@ func (s *Store) findRepositoryRaw(ctx context.Context, find *api.RepositoryFind)
}
defer tx.Rollback()
- list, err := findRepositoryImpl(ctx, tx, find)
+ list, err := s.findRepositoryImpl(ctx, tx, find)
if err != nil {
return nil, err
}
@@ -240,7 +242,7 @@ func (s *Store) getRepositoryRaw(ctx context.Context, find *api.RepositoryFind)
}
defer tx.Rollback()
- list, err := findRepositoryImpl(ctx, tx, find)
+ list, err := s.findRepositoryImpl(ctx, tx, find)
if err != nil {
return nil, err
}
@@ -262,7 +264,7 @@ func (s *Store) patchRepositoryRaw(ctx context.Context, patch *api.RepositoryPat
}
defer tx.Rollback()
- repository, err := patchRepositoryImpl(ctx, tx, patch)
+ repository, err := s.patchRepositoryImpl(ctx, tx, patch)
if err != nil {
return nil, FormatError(err)
}
@@ -304,6 +306,7 @@ func (s *Store) createRepositoryImpl(ctx context.Context, tx *Tx, create *api.Re
file_path_template,
schema_path_template,
sheet_path_template,
+ enable_sql_review_ci,
external_id,
external_webhook_id,
webhook_url_host,
@@ -313,8 +316,8 @@ func (s *Store) createRepositoryImpl(ctx context.Context, tx *Tx, create *api.Re
expires_ts,
refresh_token
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, branch_filter, base_directory, file_path_template, schema_path_template, sheet_path_template, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, branch_filter, base_directory, file_path_template, schema_path_template, sheet_path_template, enable_sql_review_ci, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
`
if err := tx.QueryRowContext(ctx, query,
create.CreatorID,
@@ -329,6 +332,7 @@ func (s *Store) createRepositoryImpl(ctx context.Context, tx *Tx, create *api.Re
create.FilePathTemplate,
create.SchemaPathTemplate,
create.SheetPathTemplate,
+ create.EnableSQLReviewCI,
create.ExternalID,
create.ExternalWebhookID,
create.WebhookURLHost,
@@ -353,6 +357,7 @@ func (s *Store) createRepositoryImpl(ctx context.Context, tx *Tx, create *api.Re
&repository.FilePathTemplate,
&repository.SchemaPathTemplate,
&repository.SheetPathTemplate,
+ &repository.EnableSQLReviewCI,
&repository.ExternalID,
&repository.ExternalWebhookID,
&repository.WebhookURLHost,
@@ -446,7 +451,7 @@ func (s *Store) createRepositoryImpl(ctx context.Context, tx *Tx, create *api.Re
return &repository, nil
}
-func findRepositoryImpl(ctx context.Context, tx *Tx, find *api.RepositoryFind) ([]*repositoryRaw, error) {
+func (s *Store) findRepositoryImpl(ctx context.Context, tx *Tx, find *api.RepositoryFind) ([]*repositoryRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -465,6 +470,84 @@ func findRepositoryImpl(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
where, args = append(where, fmt.Sprintf("web_url = $%d", len(args)+1)), append(args, *v)
}
+ if s.db.mode == common.ReleaseModeDev {
+ rows, err := tx.QueryContext(ctx, `
+ SELECT
+ id,
+ creator_id,
+ created_ts,
+ updater_id,
+ updated_ts,
+ vcs_id,
+ project_id,
+ name,
+ full_path,
+ web_url,
+ branch_filter,
+ base_directory,
+ file_path_template,
+ schema_path_template,
+ sheet_path_template,
+ enable_sql_review_ci,
+ external_id,
+ external_webhook_id,
+ webhook_url_host,
+ webhook_endpoint_id,
+ webhook_secret_token,
+ access_token,
+ expires_ts,
+ refresh_token
+ FROM repository
+ WHERE `+strings.Join(where, " AND "),
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer rows.Close()
+
+ // Iterate over result set and deserialize rows into repoRawList.
+ var repoRawList []*repositoryRaw
+ for rows.Next() {
+ var repository repositoryRaw
+ if err := rows.Scan(
+ &repository.ID,
+ &repository.CreatorID,
+ &repository.CreatedTs,
+ &repository.UpdaterID,
+ &repository.UpdatedTs,
+ &repository.VCSID,
+ &repository.ProjectID,
+ &repository.Name,
+ &repository.FullPath,
+ &repository.WebURL,
+ &repository.BranchFilter,
+ &repository.BaseDirectory,
+ &repository.FilePathTemplate,
+ &repository.SchemaPathTemplate,
+ &repository.SheetPathTemplate,
+ &repository.EnableSQLReviewCI,
+ &repository.ExternalID,
+ &repository.ExternalWebhookID,
+ &repository.WebhookURLHost,
+ &repository.WebhookEndpointID,
+ &repository.WebhookSecretToken,
+ &repository.AccessToken,
+ &repository.ExpiresTs,
+ &repository.RefreshToken,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ repoRawList = append(repoRawList, &repository)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return repoRawList, nil
+ }
+
rows, err := tx.QueryContext(ctx, `
SELECT
id,
@@ -541,7 +624,7 @@ func findRepositoryImpl(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
}
// patchRepositoryImpl updates a repository by ID. Returns the new state of the repository after update.
-func patchRepositoryImpl(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*repositoryRaw, error) {
+func (s *Store) patchRepositoryImpl(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*repositoryRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.BranchFilter; v != nil {
@@ -579,6 +662,54 @@ func patchRepositoryImpl(ctx context.Context, tx *Tx, patch *api.RepositoryPatch
return nil, common.Errorf(common.Invalid, "missing predicate in where clause for patching repository")
}
+ if s.db.mode == common.ReleaseModeDev {
+ if v := patch.EnableSQLReviewCI; v != nil {
+ set, args = append(set, fmt.Sprintf("enable_sql_review_ci = $%d", len(args)+1)), append(args, *v)
+ }
+
+ var repository repositoryRaw
+ // Execute update query with RETURNING.
+ if err := tx.QueryRowContext(ctx, `
+ UPDATE repository
+ SET `+strings.Join(set, ", ")+`
+ WHERE `+strings.Join(where, " AND ")+`
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, branch_filter, base_directory, file_path_template, schema_path_template, sheet_path_template, enable_sql_review_ci, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
+ `,
+ args...,
+ ).Scan(
+ &repository.ID,
+ &repository.CreatorID,
+ &repository.CreatedTs,
+ &repository.UpdaterID,
+ &repository.UpdatedTs,
+ &repository.VCSID,
+ &repository.ProjectID,
+ &repository.Name,
+ &repository.FullPath,
+ &repository.WebURL,
+ &repository.BranchFilter,
+ &repository.BaseDirectory,
+ &repository.FilePathTemplate,
+ &repository.SchemaPathTemplate,
+ &repository.SheetPathTemplate,
+ &repository.EnableSQLReviewCI,
+ &repository.ExternalID,
+ &repository.ExternalWebhookID,
+ &repository.WebhookURLHost,
+ &repository.WebhookEndpointID,
+ &repository.WebhookSecretToken,
+ &repository.AccessToken,
+ &repository.ExpiresTs,
+ &repository.RefreshToken,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("repository ID not found: %d", patch.ID)}
+ }
+ return nil, FormatError(err)
+ }
+ return &repository, nil
+ }
+
var repository repositoryRaw
// Execute update query with RETURNING.
if err := tx.QueryRowContext(ctx, `