Skip to content

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

chore: less message uid (#15220)

Danny Xu3a6dbe888f62
diff --git a/backend/api/v1/vcs_connector_service.go b/backend/api/v1/vcs_connector_service.go
index 720bc66d1f..842c7d8021 100644
--- a/backend/api/v1/vcs_connector_service.go
+++ b/backend/api/v1/vcs_connector_service.go
@@ -263,8 +263,8 @@ func (s *VCSConnectorService) UpdateVCSConnector(ctx context.Context, request *v
}
update := &store.UpdateVCSConnectorMessage{
- ProjectID: project.ResourceID,
- UID: vcsConnector.UID,
+ ProjectID: project.ResourceID,
+ ResourceID: vcsConnector.ResourceID,
}
for _, path := range request.UpdateMask.Paths {
@@ -338,7 +338,7 @@ func (s *VCSConnectorService) DeleteVCSConnector(ctx context.Context, request *v
return nil, status.Errorf(codes.Internal, "failed to find vcs: %s", err.Error())
}
if vcsProvider == nil {
- return nil, status.Errorf(codes.NotFound, "vcs provider %d not found", vcsConnector.UID)
+ return nil, status.Errorf(codes.NotFound, "vcs provider %s not found", vcsConnector.VCSID)
}
if err := s.store.DeleteVCSConnector(ctx, project.ResourceID, vcsConnectorID); err != nil {
diff --git a/backend/api/v1/vcs_provider_service.go b/backend/api/v1/vcs_provider_service.go
index 1d09f8042e..ce1157015d 100644
--- a/backend/api/v1/vcs_provider_service.go
+++ b/backend/api/v1/vcs_provider_service.go
@@ -104,7 +104,7 @@ func (s *VCSProviderService) UpdateVCSProvider(ctx context.Context, request *v1p
}
}
- vcsProvider, err = s.store.UpdateVCSProvider(ctx, vcsProvider.ID, update)
+ vcsProvider, err = s.store.UpdateVCSProvider(ctx, vcsProvider.ResourceID, update)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
@@ -118,7 +118,7 @@ func (s *VCSProviderService) DeleteVCSProvider(ctx context.Context, request *v1p
return nil, err
}
- if err := s.store.DeleteVCSProvider(ctx, vcsProvider.ID); err != nil {
+ if err := s.store.DeleteVCSProvider(ctx, vcsProvider.ResourceID); err != nil {
return nil, status.Errorf(codes.Internal, "Failed to delete vcs provider: %v", err)
}
return &emptypb.Empty{}, nil
diff --git a/backend/store/data_source.go b/backend/store/data_source.go
index bc4ee451e6..155905ae8b 100644
--- a/backend/store/data_source.go
+++ b/backend/store/data_source.go
@@ -42,8 +42,6 @@ type DataSourceMessage struct {
SASLConfig *storepb.SASLConfig
// Authentication
AuthenticationPrivateKeyObfuscated string
- // (deprecated) Output only.
- UID int
// external secret
ExternalSecret *storepb.DataSourceExternalSecret
AuthenticationType storepb.DataSourceOptions_AuthenticationType
@@ -90,7 +88,6 @@ func (m *DataSourceMessage) Copy() *DataSourceMessage {
SSHUser: m.SSHUser,
SSHObfuscatedPassword: m.SSHObfuscatedPassword,
SSHObfuscatedPrivateKey: m.SSHObfuscatedPrivateKey,
- UID: m.UID,
AuthenticationPrivateKeyObfuscated: m.AuthenticationPrivateKeyObfuscated,
ExternalSecret: m.ExternalSecret,
AuthenticationType: m.AuthenticationType,
@@ -172,7 +169,6 @@ func (*Store) listInstanceDataSourceMap(ctx context.Context, tx *Tx, find *FindD
instanceDataSourcesMap := make(map[string][]*DataSourceMessage)
rows, err := tx.QueryContext(ctx, `
SELECT
- id,
instance,
name,
type,
@@ -198,7 +194,6 @@ func (*Store) listInstanceDataSourceMap(ctx context.Context, tx *Tx, find *FindD
var instanceID string
var dataSourceMessage DataSourceMessage
if err := rows.Scan(
- &dataSourceMessage.UID,
&instanceID,
&dataSourceMessage.ID,
&dataSourceMessage.Type,
diff --git a/backend/store/deploy.go b/backend/store/deploy.go
index 9082f38ce5..48713437b3 100644
--- a/backend/store/deploy.go
+++ b/backend/store/deploy.go
@@ -18,10 +18,6 @@ import (
type DeploymentConfigMessage struct {
Name string
Config *storepb.DeploymentConfig
-
- // Output only fields.
- // ID is the ID of the deployment config.
- UID int
}
// GetDeploymentConfigV2 returns the deployment config.
@@ -45,13 +41,12 @@ func (s *Store) GetDeploymentConfigV2(ctx context.Context, projectID string) (*D
if err := tx.QueryRowContext(ctx, `
SELECT
- id,
name,
config
FROM deployment_config
WHERE `+strings.Join(where, " AND "),
args...,
- ).Scan(&deploymentConfig.UID, &deploymentConfig.Name, &configB); err != nil {
+ ).Scan(&deploymentConfig.Name, &configB); err != nil {
if err == sql.ErrNoRows {
// Return default deployment config.
return s.getDefaultDeploymentConfigV2(ctx)
@@ -88,7 +83,7 @@ func (s *Store) UpsertDeploymentConfigV2(ctx context.Context, projectID string,
ON CONFLICT(project) DO UPDATE SET
name = excluded.name,
config = excluded.config
- RETURNING id, name, config
+ RETURNING name, config
`
deploymentConfig := DeploymentConfigMessage{
Config: &storepb.DeploymentConfig{},
@@ -105,7 +100,7 @@ func (s *Store) UpsertDeploymentConfigV2(ctx context.Context, projectID string,
projectID,
upsert.Name,
configB,
- ).Scan(&deploymentConfig.UID, &deploymentConfig.Name, &newConfigB); err != nil {
+ ).Scan(&deploymentConfig.Name, &newConfigB); err != nil {
if err == sql.ErrNoRows {
return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
@@ -144,7 +139,6 @@ func (s *Store) getDefaultDeploymentConfigV2(ctx context.Context) (*DeploymentCo
})
}
return &DeploymentConfigMessage{
- UID: 0,
Config: &storepb.DeploymentConfig{
Schedule: &storepb.Schedule{
Deployments: deployments,
diff --git a/backend/store/idp.go b/backend/store/idp.go
index 279117a08d..873ab1cd64 100644
--- a/backend/store/idp.go
+++ b/backend/store/idp.go
@@ -20,9 +20,7 @@ type IdentityProviderMessage struct {
Domain string
Type storepb.IdentityProviderType
Config *storepb.IdentityProviderConfig
- // The following fields are output only and not used for creating.
- UID int
- Deleted bool
+ Deleted bool
}
func getConfigBytes(config *storepb.IdentityProviderConfig) ([]byte, error) {
@@ -77,28 +75,22 @@ func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProv
if err != nil {
return nil, errors.Wrap(err, "failed to marshal identity provider config")
}
- if err := tx.QueryRowContext(ctx, `
- INSERT INTO idp (
- resource_id,
- name,
- domain,
- type,
- config
- )
- VALUES ($1, $2, $3, $4, $5)
- RETURNING id
+ if _, err := tx.ExecContext(ctx, `
+ INSERT INTO idp (
+ resource_id,
+ name,
+ domain,
+ type,
+ config
+ )
+ VALUES ($1, $2, $3, $4, $5)
`,
create.ResourceID,
create.Title,
create.Domain,
create.Type.String(),
configBytes,
- ).Scan(
- &identityProvider.UID,
); err != nil {
- if err == sql.ErrNoRows {
- return nil, common.FormatDBErrorEmptyRowWithQuery("failed to create identity provider")
- }
return nil, err
}
@@ -216,7 +208,6 @@ func (*Store) updateIdentityProviderImpl(ctx context.Context, tx *Tx, patch *Upd
SET `+strings.Join(set, ", ")+`
WHERE resource_id = $%d
RETURNING
- id,
resource_id,
name,
domain,
@@ -226,7 +217,6 @@ func (*Store) updateIdentityProviderImpl(ctx context.Context, tx *Tx, patch *Upd
`, len(args)),
args...,
).Scan(
- &identityProvider.UID,
&identityProvider.ResourceID,
&identityProvider.Title,
&identityProvider.Domain,
@@ -260,7 +250,6 @@ func (*Store) listIdentityProvidersImpl(ctx context.Context, tx *Tx, find *FindI
rows, err := tx.QueryContext(ctx, `
SELECT
- id,
resource_id,
name,
domain,
@@ -282,7 +271,6 @@ func (*Store) listIdentityProvidersImpl(ctx context.Context, tx *Tx, find *FindI
var identityProviderType string
var identityProviderConfig string
if err := rows.Scan(
- &identityProviderMessage.UID,
&identityProviderMessage.ResourceID,
&identityProviderMessage.Title,
&identityProviderMessage.Domain,
diff --git a/backend/store/store.go b/backend/store/store.go
index 0e0966a90d..8e1f2e26c9 100644
--- a/backend/store/store.go
+++ b/backend/store/store.go
@@ -37,7 +37,7 @@ type Store struct {
risksCache *lru.Cache[int, []*RiskMessage] // Use 0 as the key.
databaseGroupCache *lru.Cache[string, *DatabaseGroupMessage]
databaseGroupIDCache *lru.Cache[int64, *DatabaseGroupMessage]
- vcsIDCache *lru.Cache[int, *VCSProviderMessage]
+ vcsIDCache *lru.Cache[string, *VCSProviderMessage]
rolesCache *lru.Cache[string, *RoleMessage]
groupCache *lru.Cache[string, *GroupMessage]
sheetCache *lru.Cache[int, *SheetMessage]
@@ -125,7 +125,7 @@ func New(db *DB, profile *config.Profile) (*Store, error) {
if err != nil {
return nil, err
}
- vcsIDCache, err := lru.New[int, *VCSProviderMessage](1024)
+ vcsIDCache, err := lru.New[string, *VCSProviderMessage](1024)
if err != nil {
return nil, err
}
diff --git a/backend/store/vcs_connector.go b/backend/store/vcs_connector.go
index 9e610b35a0..89abae777a 100644
--- a/backend/store/vcs_connector.go
+++ b/backend/store/vcs_connector.go
@@ -21,9 +21,6 @@ type VCSConnectorMessage struct {
ResourceID string
Payload *storepb.VCSConnector
-
- // Output only fields
- UID int
}
// FindVCSConnectorMessage is the API message for finding VCS connectors.
@@ -35,8 +32,8 @@ type FindVCSConnectorMessage struct {
// UpdateVCSConnectorMessage is the message to update a VCS connector.
type UpdateVCSConnectorMessage struct {
- ProjectID string
- UID int
+ ProjectID string
+ ResourceID string
// Domain specific fields
Branch *string
@@ -81,7 +78,6 @@ func (s *Store) ListVCSConnectors(ctx context.Context, find *FindVCSConnectorMes
rows, err := tx.QueryContext(ctx, `
SELECT
- id,
vcs,
project,
resource_id,
@@ -100,7 +96,6 @@ func (s *Store) ListVCSConnectors(ctx context.Context, find *FindVCSConnectorMes
var vcsConnector VCSConnectorMessage
var payloadStr string
if err := rows.Scan(
- &vcsConnector.UID,
&vcsConnector.VCSID,
&vcsConnector.ProjectID,
&vcsConnector.ResourceID,
@@ -151,15 +146,12 @@ func (s *Store) CreateVCSConnector(ctx context.Context, create *VCSConnectorMess
payload
)
VALUES ($1, $2, $3, $4)
- RETURNING id
`
- if err := tx.QueryRowContext(ctx, query,
+ if _, err := tx.ExecContext(ctx, query,
create.VCSID,
project.ResourceID,
create.ResourceID,
payload,
- ).Scan(
- &create.UID,
); err != nil {
return nil, err
}
@@ -191,7 +183,7 @@ func (s *Store) UpdateVCSConnector(ctx context.Context, update *UpdateVCSConnect
}
where := []string{}
- where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, update.UID)
+ where, args = append(where, fmt.Sprintf("resource_id = $%d", len(args)+1)), append(args, update.ResourceID)
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
diff --git a/backend/store/vcs_provider.go b/backend/store/vcs_provider.go
index a36ab65145..c04b6fee24 100644
--- a/backend/store/vcs_provider.go
+++ b/backend/store/vcs_provider.go
@@ -8,7 +8,6 @@ import (
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -24,11 +23,6 @@ type VCSProviderMessage struct {
Title string
// AccessToken is the access token for the VCS provider.
AccessToken string
-
- // Output only fields.
- //
- // ID is the unique identifier of the message.
- ID int
}
// UpdateVCSProviderMessage is a message for updating an VCS provider.
@@ -40,15 +34,13 @@ type UpdateVCSProviderMessage struct {
}
type FindVCSProviderMessage struct {
- // If specified, only VCS providers with the given ID will be returned.
- ID *int
ResourceID *string
}
// GetVCSProvider gets an VCS provider by ID.
func (s *Store) GetVCSProvider(ctx context.Context, find *FindVCSProviderMessage) (*VCSProviderMessage, error) {
- if find.ID != nil {
- if v, ok := s.vcsIDCache.Get(*find.ID); ok {
+ if find.ResourceID != nil {
+ if v, ok := s.vcsIDCache.Get(*find.ResourceID); ok {
return v, nil
}
}
@@ -73,7 +65,7 @@ func (s *Store) GetVCSProvider(ctx context.Context, find *FindVCSProviderMessage
}
vcs := vcsProviders[0]
- s.vcsIDCache.Add(vcs.ID, vcs)
+ s.vcsIDCache.Add(vcs.ResourceID, vcs)
return vcs, nil
}
@@ -94,7 +86,7 @@ func (s *Store) ListVCSProviders(ctx context.Context) ([]*VCSProviderMessage, er
}
for _, vcs := range vcsProviders {
- s.vcsIDCache.Add(vcs.ID, vcs)
+ s.vcsIDCache.Add(vcs.ResourceID, vcs)
}
return vcsProviders, nil
}
@@ -118,18 +110,13 @@ func (s *Store) CreateVCSProvider(ctx context.Context, create *VCSProviderMessag
VALUES ($1, $2, $3, $4, $5)
RETURNING id
`
- if err := tx.QueryRowContext(ctx, query,
+ if _, err := tx.ExecContext(ctx, query,
create.ResourceID,
create.Title,
create.Type.String(),
create.InstanceURL,
create.AccessToken,
- ).Scan(
- &create.ID,
); err != nil {
- if err == sql.ErrNoRows {
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
- }
return nil, err
}
@@ -137,12 +124,12 @@ func (s *Store) CreateVCSProvider(ctx context.Context, create *VCSProviderMessag
return nil, errors.Wrapf(err, "failed to commit transaction")
}
- s.vcsIDCache.Add(create.ID, create)
+ s.vcsIDCache.Add(create.ResourceID, create)
return create, nil
}
// UpdateVCSProvider updates an VCS provider.
-func (s *Store) UpdateVCSProvider(ctx context.Context, vcsProviderUID int, update *UpdateVCSProviderMessage) (*VCSProviderMessage, error) {
+func (s *Store) UpdateVCSProvider(ctx context.Context, vcsProviderID string, update *UpdateVCSProviderMessage) (*VCSProviderMessage, error) {
set, args := []string{}, []any{}
if v := update.Name; v != nil {
set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)
@@ -150,7 +137,7 @@ func (s *Store) UpdateVCSProvider(ctx context.Context, vcsProviderUID int, updat
if v := update.AccessToken; v != nil {
set, args = append(set, fmt.Sprintf("access_token = $%d", len(args)+1)), append(args, *v)
}
- args = append(args, vcsProviderUID)
+ args = append(args, vcsProviderID)
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
@@ -163,12 +150,11 @@ func (s *Store) UpdateVCSProvider(ctx context.Context, vcsProviderUID int, updat
if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE vcs
SET `+strings.Join(set, ", ")+`
- WHERE id = $%d
- RETURNING id, resource_id, name, type, instance_url, access_token
+ WHERE resource_id = $%d
+ RETURNING resource_id, name, type, instance_url, access_token
`, len(args)),
args...,
).Scan(
- &vcsProvider.ID,
&vcsProvider.ResourceID,
&vcsProvider.Title,
&vcsType,
@@ -186,19 +172,19 @@ func (s *Store) UpdateVCSProvider(ctx context.Context, vcsProviderUID int, updat
if err := tx.Commit(); err != nil {
return nil, errors.Wrapf(err, "failed to commit transaction")
}
- s.vcsIDCache.Add(vcsProvider.ID, &vcsProvider)
+ s.vcsIDCache.Add(vcsProvider.ResourceID, &vcsProvider)
return &vcsProvider, nil
}
// DeleteVCSProvider deletes an VCS provider.
-func (s *Store) DeleteVCSProvider(ctx context.Context, vcsProviderUID int) error {
+func (s *Store) DeleteVCSProvider(ctx context.Context, vcsProviderID string) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return errors.Wrapf(err, "failed to begin transaction")
}
defer tx.Rollback()
- if _, err := tx.ExecContext(ctx, `DELETE FROM vcs WHERE id = $1`, vcsProviderUID); err != nil {
+ if _, err := tx.ExecContext(ctx, `DELETE FROM vcs WHERE resource_id = $1`, vcsProviderID); err != nil {
return err
}
@@ -206,22 +192,18 @@ func (s *Store) DeleteVCSProvider(ctx context.Context, vcsProviderUID int) error
return errors.Wrapf(err, "failed to commit transaction")
}
- s.vcsIDCache.Remove(vcsProviderUID)
+ s.vcsIDCache.Remove(vcsProviderID)
return nil
}
func (*Store) findVCSProvidersImpl(ctx context.Context, tx *Tx, find *FindVCSProviderMessage) ([]*VCSProviderMessage, error) {
where, args := []string{"TRUE"}, []any{}
- 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, `
SELECT
- id,
resource_id,
name,
type,
@@ -240,7 +222,6 @@ func (*Store) findVCSProvidersImpl(ctx context.Context, tx *Tx, find *FindVCSPro
var vcsProvider VCSProviderMessage
var vcsType string
if err := rows.Scan(
- &vcsProvider.ID,
&vcsProvider.ResourceID,
&vcsProvider.Title,
&vcsType,