Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: vcs resource id (store only) (#11261)
Danny Xu816e8eec84a7
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.goindex b7e34d9f5d..264475678f 100644--- a/backend/api/v1/project_service.go+++ b/backend/api/v1/project_service.go@@ -1010,8 +1010,10 @@ func (s *ProjectService) createProjectGitOpsInfo(ctx context.Context, request *v}repositoryCreate := &store.RepositoryMessage{- VCSUID: int(vcsID),- ProjectResourceID: project.ResourceID,+ VCSUID: int(vcsID),+ ProjectResourceID: project.ResourceID,+ // TODO(d): pass in the resource ID from API.+ ResourceID: "default",WebhookURLHost: setting.ExternalUrl,Title: request.ProjectGitopsInfo.Title,FullPath: request.ProjectGitopsInfo.FullPath,diff --git a/backend/api/v1/vcs_provider_service.go b/backend/api/v1/vcs_provider_service.goindex fb02c6c84f..79a8e0f9cc 100644--- a/backend/api/v1/vcs_provider_service.go+++ b/backend/api/v1/vcs_provider_service.go@@ -56,6 +56,8 @@ func (s *VCSProviderService) CreateVCSProvider(ctx context.Context, request *v1pif !ok {return nil, status.Errorf(codes.Internal, "principal ID not found")}+ // TODO(d): pass in the resource ID from API.+ vcsProvider.ResourceID = strings.ToLower(string(vcsProvider.Type))storeVCSProvider, err := s.store.CreateVCSProviderV2(ctx, principalID, vcsProvider)if err != nil {return nil, status.Errorf(codes.Internal, "Failed to create vcs provider: %v", err)diff --git a/backend/migrator/migration/prod/2.15/0005##vcs_resource_id.sql b/backend/migrator/migration/prod/2.15/0005##vcs_resource_id.sqlnew file mode 100644index 0000000000..b5629b2bf1--- /dev/null+++ b/backend/migrator/migration/prod/2.15/0005##vcs_resource_id.sql@@ -0,0 +1,14 @@+DROP INDEX idx_repository_unique_project_id;++ALTER TABLE vcs ADD COLUMN resource_id TEXT;+ALTER TABLE repository ADD COLUMN resource_id TEXT;++UPDATE vcs AS one SET resource_id = LOWER(type) WHERE id IN (SELECT id FROM vcs where one.type = vcs.type ORDER BY id LIMIT 1);+UPDATE vcs SET resource_id=CONCAT('vcs-', LEFT(uuid_in(md5(random()::text || random()::text)::cstring)::TEXT, 8)) WHERE resource_id IS NULL OR resource_id = '';+UPDATE repository SET resource_id='default';++ALTER TABLE vcs ALTER COLUMN resource_id SET NOT NULL;+ALTER TABLE repository ALTER COLUMN resource_id SET NOT NULL;++CREATE UNIQUE INDEX idx_vcs_unique_resource_id ON vcs(resource_id);+CREATE UNIQUE INDEX idx_repository_unique_project_id_resource_id ON repository(project_id, resource_id);diff --git a/backend/migrator/migration/prod/LATEST.sql b/backend/migrator/migration/prod/LATEST.sqlindex ce06fecad9..1f1ea57c1b 100644--- a/backend/migrator/migration/prod/LATEST.sql+++ b/backend/migrator/migration/prod/LATEST.sql@@ -854,12 +854,15 @@ CREATE TABLE vcs (created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),updater_id INTEGER NOT NULL REFERENCES principal (id),updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),+ resource_id TEXT NOT NULL,name TEXT NOT NULL,type TEXT NOT NULL CHECK (type IN ('GITLAB', 'GITHUB', 'BITBUCKET', 'AZURE_DEVOPS')),instance_url TEXT NOT NULL CHECK ((instance_url LIKE 'http://%' OR instance_url LIKE 'https://%') AND instance_url = rtrim(instance_url, '/')),access_token TEXT NOT NULL DEFAULT '');+CREATE UNIQUE INDEX idx_vcs_unique_resource_id ON vcs(resource_id);+ALTER SEQUENCE vcs_id_seq RESTART WITH 101;CREATE TRIGGER update_vcs_updated_ts@@ -880,6 +883,7 @@ CREATE TABLE repository (updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),vcs_id INTEGER NOT NULL REFERENCES vcs (id),project_id INTEGER NOT NULL REFERENCES project (id),+ resource_id TEXT NOT NULL,-- Name from the corresponding VCS provider.-- For GitLab, this is the project name. e.g. project 1name TEXT NOT NULL,@@ -913,7 +917,7 @@ CREATE TABLE repository (webhook_secret_token TEXT NOT NULL);-CREATE UNIQUE INDEX idx_repository_unique_project_id ON repository(project_id);+CREATE UNIQUE INDEX idx_repository_unique_project_id_resource_id ON repository(project_id, resource_id);ALTER SEQUENCE repository_id_seq RESTART WITH 101;diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 07b9daf4f7..e04d70d509 100644--- a/backend/migrator/migrator_test.go+++ b/backend/migrator/migrator_test.go@@ -230,5 +230,5 @@ func TestMigrationCompatibility(t *testing.T) {func TestGetCutoffVersion(t *testing.T) {releaseVersion, err := getProdCutoffVersion()require.NoError(t, err)- require.Equal(t, semver.MustParse("2.15.4"), releaseVersion)+ require.Equal(t, semver.MustParse("2.15.5"), releaseVersion)}diff --git a/backend/store/repository.go b/backend/store/repository.goindex 1c20c6a6c0..92bde48f56 100644--- a/backend/store/repository.go+++ b/backend/store/repository.go@@ -14,12 +14,10 @@ import (// RepositoryMessage is the message for a repository.type RepositoryMessage struct {- // Output only- UID int-// Related fieldsVCSUID intProjectResourceID string+ ResourceID string// Domain specific fieldsTitle string@@ -34,6 +32,9 @@ type RepositoryMessage struct {WebhookURLHost stringWebhookEndpointID stringWebhookSecretToken string++ // Output only+ UID int}// FindRepositoryMessage is the message for finding repositories.@@ -42,13 +43,15 @@ type FindRepositoryMessage struct {WebURL *stringVCSUID *intProjectResourceID *string+ ResourceID *stringWebhookEndpointID *string}// PatchRepositoryMessage is the API message for patching a repository.type PatchRepositoryMessage struct {- UID *int- WebURL *string+ ResourceID *string+ UID *int+ WebURL *string// Domain specific fieldsBranchFilter *string@@ -194,6 +197,7 @@ func (s *Store) createRepositoryImplV2(ctx context.Context, tx *Tx, create *Repoupdater_id,vcs_id,project_id,+ resource_id,name,full_path,web_url,@@ -207,14 +211,15 @@ func (s *Store) createRepositoryImplV2(ctx context.Context, tx *Tx, create *Repowebhook_endpoint_id,webhook_secret_token)- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)- RETURNING id, vcs_id, name, full_path, web_url, branch_filter, base_directory, file_path_template, schema_path_template, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)+ RETURNING id, vcs_id, resource_id, name, full_path, web_url, branch_filter, base_directory, file_path_template, schema_path_template, external_id, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token`if err := tx.QueryRowContext(ctx, query,creatorID,creatorID,create.VCSUID,project.UID,+ create.ResourceID,create.Title,create.FullPath,create.WebURL,@@ -230,6 +235,7 @@ func (s *Store) createRepositoryImplV2(ctx context.Context, tx *Tx, create *Repo).Scan(&repository.UID,&repository.VCSUID,+ &repository.ResourceID,&repository.Title,&repository.FullPath,&repository.WebURL,@@ -260,6 +266,9 @@ func (*Store) listRepositoryImplV2(ctx context.Context, tx *Tx, find *FindReposiif v := find.VCSUID; v != nil {where, args = append(where, fmt.Sprintf("vcs_id = $%d", len(args)+1)), append(args, *v)}+ if v := find.ResourceID; v != nil {+ where, args = append(where, fmt.Sprintf("resource_id = $%d", len(args)+1)), append(args, *v)+ }if v := find.WebURL; v != nil {where, args = append(where, fmt.Sprintf("web_url = $%d", len(args)+1)), append(args, *v)}@@ -275,6 +284,7 @@ func (*Store) listRepositoryImplV2(ctx context.Context, tx *Tx, find *FindReposirepository.id AS id,vcs_id,project.resource_id AS project_resource_id,+ repository.resource_id,repository.name AS name,full_path,web_url,@@ -305,6 +315,7 @@ func (*Store) listRepositoryImplV2(ctx context.Context, tx *Tx, find *FindReposi&repository.UID,&repository.VCSUID,&repository.ProjectResourceID,+ &repository.ResourceID,&repository.Title,&repository.FullPath,&repository.WebURL,@@ -352,6 +363,9 @@ func (*Store) patchRepositoryImplV2(ctx context.Context, tx *Tx, patch *PatchRepif v := patch.UID; v != nil {where, args = append(where, fmt.Sprintf("repository.id = $%d", len(args)+1)), append(args, *v)}+ if v := patch.ResourceID; v != nil {+ where, args = append(where, fmt.Sprintf("repository.resource_id = $%d", len(args)+1)), append(args, *v)+ }if v := patch.WebURL; v != nil {where, args = append(where, fmt.Sprintf("web_url = $%d", len(args)+1)), append(args, *v)}@@ -370,6 +384,7 @@ func (*Store) patchRepositoryImplV2(ctx context.Context, tx *Tx, patch *PatchReprepository.id AS id,vcs_id,project.resource_id AS project_resource_id,+ repository.resource_id,repository.name AS name,full_path,web_url,@@ -388,6 +403,7 @@ func (*Store) patchRepositoryImplV2(ctx context.Context, tx *Tx, patch *PatchRep&repository.UID,&repository.VCSUID,&repository.ProjectResourceID,+ &repository.ResourceID,&repository.Title,&repository.FullPath,&repository.WebURL,diff --git a/backend/store/vcs.go b/backend/store/vcs.goindex 1ccffe3697..fdca4bcf16 100644--- a/backend/store/vcs.go+++ b/backend/store/vcs.go@@ -12,15 +12,17 @@ import ("github.com/bytebase/bytebase/backend/plugin/vcs")-// VCSProviderMessage is a message for external version control.+// VCSProviderMessage is a message for VCS provider.type VCSProviderMessage struct {- // Type is the type of the external version control.+ // ResourceID is the unique resource ID.+ ResourceID string+ // Type is the type of the VCS provider.Type vcs.Type- // InstanceURL is the URL for the external version control instance.+ // InstanceURL is the URL for the VCS provider instance.InstanceURL string- // Name is the name of the external version control.+ // Name is the name of the VCS provider.Name string- // AccessToken is the access token for the external version control.+ // AccessToken is the access token for the VCS provider.AccessToken string// Output only fields.@@ -29,20 +31,21 @@ type VCSProviderMessage struct {ID int}-// UpdateVCSProviderMessage is a message for updating an external version control.+// UpdateVCSProviderMessage is a message for updating an VCS provider.type UpdateVCSProviderMessage struct {- // Name is the name of the external version control.+ // Name is the name of the VCS provider.Name *string- // AccessToken is the secret for the external version control.+ // AccessToken is the secret for the VCS provider.AccessToken *string}-type findVCSProviderMessage struct {- // If specified, only external version controls with the given ID will be returned.- id *int+type FindVCSProviderMessage struct {+ // If specified, only VCS providers with the given ID will be returned.+ ID *int+ ResourceID *string}-// GetVCSProviderV2 gets an external version control by ID.+// GetVCSProviderV2 gets an VCS provider by ID.func (s *Store) GetVCSProviderV2(ctx context.Context, id int) (*VCSProviderMessage, error) {if v, ok := s.vcsIDCache.Get(id); ok {return v, nil@@ -54,7 +57,7 @@ func (s *Store) GetVCSProviderV2(ctx context.Context, id int) (*VCSProviderMessa}defer tx.Rollback()- vcsProviders, err := s.findVCSProvidersImplV2(ctx, tx, &findVCSProviderMessage{id: &id})+ vcsProviders, err := s.findVCSProvidersImplV2(ctx, tx, &FindVCSProviderMessage{ID: &id})if err != nil {return nil, err}@@ -64,7 +67,7 @@ func (s *Store) GetVCSProviderV2(ctx context.Context, id int) (*VCSProviderMessaif len(vcsProviders) == 0 {return nil, nil} else if len(vcsProviders) > 1 {- return nil, errors.Errorf("expected 1 external version control with id %d, got %d", id, len(vcsProviders))+ return nil, errors.Errorf("expected 1 VCS provider with id %d, got %d", id, len(vcsProviders))}vcs := vcsProviders[0]@@ -72,7 +75,7 @@ func (s *Store) GetVCSProviderV2(ctx context.Context, id int) (*VCSProviderMessareturn vcs, nil}-// ListVCSProviders lists all external version controls.+// ListVCSProviders lists all VCS providers.func (s *Store) ListVCSProviders(ctx context.Context) ([]*VCSProviderMessage, error) {tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})if err != nil {@@ -80,7 +83,7 @@ func (s *Store) ListVCSProviders(ctx context.Context) ([]*VCSProviderMessage, er}defer tx.Rollback()- vcsProviders, err := s.findVCSProvidersImplV2(ctx, tx, &findVCSProviderMessage{})+ vcsProviders, err := s.findVCSProvidersImplV2(ctx, tx, &FindVCSProviderMessage{})if err != nil {return nil, err}@@ -94,7 +97,7 @@ func (s *Store) ListVCSProviders(ctx context.Context) ([]*VCSProviderMessage, erreturn vcsProviders, nil}-// CreateVCSProviderV2 creates an external version control.+// CreateVCSProviderV2 creates an VCS provider.func (s *Store) CreateVCSProviderV2(ctx context.Context, principalUID int, create *VCSProviderMessage) (*VCSProviderMessage, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {@@ -106,24 +109,27 @@ func (s *Store) CreateVCSProviderV2(ctx context.Context, principalUID int, creatINSERT INTO vcs (creator_id,updater_id,+ resource_id,name,type,instance_url,access_token)- VALUES ($1, $2, $3, $4, $5, $6)- RETURNING id, name, type, instance_url, access_token+ VALUES ($1, $2, $3, $4, $5, $6, $7)+ RETURNING id, resource_id, name, type, instance_url, access_token`var vcsProvider VCSProviderMessageif err := tx.QueryRowContext(ctx, query,principalUID,principalUID,+ create.ResourceID,create.Name,create.Type,create.InstanceURL,create.AccessToken,).Scan(&vcsProvider.ID,+ &vcsProvider.ResourceID,&vcsProvider.Name,&vcsProvider.Type,&vcsProvider.InstanceURL,@@ -143,7 +149,7 @@ func (s *Store) CreateVCSProviderV2(ctx context.Context, principalUID int, creatreturn &vcsProvider, nil}-// UpdateVCSProviderV2 updates an external version control.+// UpdateVCSProviderV2 updates an VCS provider.func (s *Store) UpdateVCSProviderV2(ctx context.Context, principalUID int, vcsProviderUID int, update *UpdateVCSProviderMessage) (*VCSProviderMessage, error) {// Build UPDATE clause.set, args := []string{"updater_id = $1"}, []any{principalUID}@@ -167,11 +173,12 @@ func (s *Store) UpdateVCSProviderV2(ctx context.Context, principalUID int, vcsPrUPDATE vcsSET `+strings.Join(set, ", ")+`WHERE id = $%d- RETURNING id, name, type, instance_url, access_token+ RETURNING id, resource_id name, type, instance_url, access_token`, len(args)),args...,).Scan(&vcsProvider.ID,+ &vcsProvider.ResourceID,&vcsProvider.Name,&vcsProvider.Type,&vcsProvider.InstanceURL,@@ -190,7 +197,7 @@ func (s *Store) UpdateVCSProviderV2(ctx context.Context, principalUID int, vcsPrreturn &vcsProvider, nil}-// DeleteVCSProviderV2 deletes an external version control.+// DeleteVCSProviderV2 deletes an VCS provider.func (s *Store) DeleteVCSProviderV2(ctx context.Context, vcsProviderUID int) error {tx, err := s.db.BeginTx(ctx, nil)if err != nil {@@ -210,16 +217,19 @@ func (s *Store) DeleteVCSProviderV2(ctx context.Context, vcsProviderUID int) errreturn nil}-func (*Store) findVCSProvidersImplV2(ctx context.Context, tx *Tx, find *findVCSProviderMessage) ([]*VCSProviderMessage, error) {+func (*Store) findVCSProvidersImplV2(ctx context.Context, tx *Tx, find *FindVCSProviderMessage) ([]*VCSProviderMessage, error) {where, args := []string{"TRUE"}, []any{}- if v := find.id; v != nil {- // Build WHERE clause.+ if v := find.ID; v != nil {where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)}+ if v := find.ResourceID; v != nil {+ where, args = append(where, fmt.Sprintf("resource_id = $%d", len(args)+1)), append(args, *v)+ }rows, err := tx.QueryContext(ctx, `SELECTid,+ resource_id,name,type,instance_url,@@ -237,6 +247,7 @@ func (*Store) findVCSProvidersImplV2(ctx context.Context, tx *Tx, find *findVCSPvar vcsProvider VCSProviderMessageif err := rows.Scan(&vcsProvider.ID,+ &vcsProvider.ResourceID,&vcsProvider.Name,&vcsProvider.Type,&vcsProvider.InstanceURL,