Skip to content

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

chore: no row status enum (#15212)

Danny Xu1a0e7c9ff9af
diff --git a/backend/api/v1/org_policy_service.go b/backend/api/v1/org_policy_service.go
index 1bf8fcfe73..f38b1a1e5c 100644
--- a/backend/api/v1/org_policy_service.go
+++ b/backend/api/v1/org_policy_service.go
@@ -60,7 +60,7 @@ func (s *OrgPolicyService) ListPolicies(ctx context.Context, request *v1pb.ListP
find := &store.FindPolicyMessage{
ResourceType: &resourceType,
Resource: resource,
- ShowDeleted: request.ShowDeleted,
+ ShowAll: request.ShowDeleted,
}
if v := request.PolicyType; v != nil {
diff --git a/backend/api/v1/review_config_service.go b/backend/api/v1/review_config_service.go
index 540c373b54..9429be032e 100644
--- a/backend/api/v1/review_config_service.go
+++ b/backend/api/v1/review_config_service.go
@@ -183,8 +183,8 @@ func convertToReviewConfigMessage(reviewConfig *v1pb.ReviewConfig) (*store.Revie
func (s *ReviewConfigService) convertToV1ReviewConfig(ctx context.Context, reviewConfigMessage *store.ReviewConfigMessage) (*v1pb.ReviewConfig, error) {
policyType := api.PolicyTypeTag
tagPolicies, err := s.store.ListPoliciesV2(ctx, &store.FindPolicyMessage{
- Type: &policyType,
- ShowDeleted: false,
+ Type: &policyType,
+ ShowAll: false,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list tag policy, error %v", err)
diff --git a/backend/metric/collector/instance_count.go b/backend/metric/collector/instance_count.go
index 7ebd9a54e1..11723cb91b 100644
--- a/backend/metric/collector/instance_count.go
+++ b/backend/metric/collector/instance_count.go
@@ -38,7 +38,6 @@ func (c *instanceCountCollector) Collect(ctx context.Context) ([]*metric.Metric,
Labels: map[string]any{
"engine": instanceCountMetric.Engine.String(),
"environment": instanceCountMetric.EnvironmentID,
- "status": string(instanceCountMetric.RowStatus),
},
})
}
diff --git a/backend/metric/collector/policy_count.go b/backend/metric/collector/policy_count.go
deleted file mode 100644
index fb2ebd0904..0000000000
--- a/backend/metric/collector/policy_count.go
+++ /dev/null
@@ -1,91 +0,0 @@
-package collector
-
-import (
- "context"
-
- "github.com/bytebase/bytebase/backend/common"
- api "github.com/bytebase/bytebase/backend/legacyapi"
- metricapi "github.com/bytebase/bytebase/backend/metric"
- "github.com/bytebase/bytebase/backend/plugin/metric"
- "github.com/bytebase/bytebase/backend/store"
-)
-
-var _ metric.Collector = (*policyCountCollector)(nil)
-
-// policyCountCollector is the metric data collector for policy.
-type policyCountCollector struct {
- store *store.Store
-}
-
-// NewPolicyCountCollector creates a new instance of policyCollector.
-func NewPolicyCountCollector(store *store.Store) metric.Collector {
- return &policyCountCollector{
- store: store,
- }
-}
-
-// Collect will collect the metric for policy.
-func (c *policyCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {
- var res []*metric.Metric
-
- policies, err := c.store.ListPoliciesV2(ctx, &store.FindPolicyMessage{})
- if err != nil {
- return nil, err
- }
-
- policyCountMap := make(map[string]*metricapi.PolicyCountMetric)
-
- for _, policy := range policies {
- var key string
- var value string
- if policy.ResourceType != api.PolicyResourceTypeEnvironment {
- continue
- }
- environmentID, err := common.GetEnvironmentID(policy.Resource)
- if err != nil {
- return nil, err
- }
- environment, err := c.store.GetEnvironmentV2(ctx, &store.FindEnvironmentMessage{ResourceID: &environmentID})
- if err != nil {
- continue
- }
- if environment == nil {
- continue
- }
-
- rowStatus := api.Normal
- if environment.Deleted {
- rowStatus = api.Archived
- }
-
- if key == "" {
- continue
- }
-
- if _, ok := policyCountMap[key]; !ok {
- policyCountMap[key] = &metricapi.PolicyCountMetric{
- Type: policy.Type,
- Value: value,
- EnvironmentName: environment.Title,
- Count: 0,
- RowStatus: rowStatus,
- }
- }
- policyCountMap[key].Count++
- }
-
- for _, policyCountMetric := range policyCountMap {
- res = append(res, &metric.Metric{
- Name: metricapi.PolicyCountMetricName,
- Value: policyCountMetric.Count,
- Labels: map[string]any{
- "type": string(policyCountMetric.Type),
- "environment": policyCountMetric.EnvironmentName,
- "value": policyCountMetric.Value,
- "status": string(policyCountMetric.RowStatus),
- },
- })
- }
-
- return res, nil
-}
diff --git a/backend/metric/collector/project_count.go b/backend/metric/collector/project_count.go
index 8748c1a40f..d3dc8cb184 100644
--- a/backend/metric/collector/project_count.go
+++ b/backend/metric/collector/project_count.go
@@ -24,23 +24,14 @@ func NewProjectCountCollector(store *store.Store) metric.Collector {
// Collect will collect the metric for project.
func (c *projectCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {
- var res []*metric.Metric
-
- projectCountMetricList, err := c.store.CountProjectGroupByWorkflow(ctx)
+ count, err := c.store.CountProjects(ctx)
if err != nil {
return nil, err
}
-
- for _, projectCountMetric := range projectCountMetricList {
- res = append(res, &metric.Metric{
+ return []*metric.Metric{
+ {
Name: metricapi.ProjectCountMetricName,
- Value: projectCountMetric.Count,
- Labels: map[string]any{
- "workflow": projectCountMetric.WorkflowType.String(),
- "status": string(projectCountMetric.RowStatus),
- },
- })
- }
-
- return res, nil
+ Value: count,
+ },
+ }, nil
}
diff --git a/backend/metric/collector/task_count.go b/backend/metric/collector/task_count.go
deleted file mode 100644
index e3490b3e1f..0000000000
--- a/backend/metric/collector/task_count.go
+++ /dev/null
@@ -1,45 +0,0 @@
-package collector
-
-import (
- "context"
-
- metricapi "github.com/bytebase/bytebase/backend/metric"
- "github.com/bytebase/bytebase/backend/plugin/metric"
- "github.com/bytebase/bytebase/backend/store"
-)
-
-var _ metric.Collector = (*taskCountCollector)(nil)
-
-// taskCountCollector is the metric data collector for task.
-type taskCountCollector struct {
- store *store.Store
-}
-
-// NewTaskCountCollector creates a new instance of taskCollector.
-func NewTaskCountCollector(store *store.Store) metric.Collector {
- return &taskCountCollector{
- store: store,
- }
-}
-
-// Collect will collect the metric for task.
-func (c *taskCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {
- var res []*metric.Metric
-
- taskCountMetricList, err := c.store.CountTaskGroupByTypeAndStatus(ctx)
- if err != nil {
- return nil, err
- }
-
- for _, taskCountMetric := range taskCountMetricList {
- res = append(res, &metric.Metric{
- Name: metricapi.TaskCountMetricName,
- Value: taskCountMetric.Count,
- Labels: map[string]any{
- "type": string(taskCountMetric.Type),
- "status": string(taskCountMetric.Status),
- },
- })
- }
- return res, nil
-}
diff --git a/backend/metric/metric.go b/backend/metric/metric.go
index 0db461a6b8..9e575a44ed 100644
--- a/backend/metric/metric.go
+++ b/backend/metric/metric.go
@@ -5,7 +5,6 @@ import (
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/plugin/metric"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
- v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
const (
@@ -13,12 +12,8 @@ const (
InstanceCountMetricName metric.Name = "bb.instance.count"
// IssueCountMetricName is the metric name for issue count.
IssueCountMetricName metric.Name = "bb.issue.count"
- // PolicyCountMetricName is the metric name for policy count.
- PolicyCountMetricName metric.Name = "bb.policy.count"
// ProjectCountMetricName is the metric name for project count.
ProjectCountMetricName metric.Name = "bb.project.count"
- // TaskCountMetricName is the metric name for task count.
- TaskCountMetricName metric.Name = "bb.task.count"
// DatabaseCountMetricName is the metric name for database count.
DatabaseCountMetricName metric.Name = "bb.database.count"
// SheetCountMetricName is the metric name for sheet count.
@@ -53,7 +48,6 @@ const (
type InstanceCountMetric struct {
Engine storepb.Engine
EnvironmentID string
- RowStatus api.RowStatus
Count int
}
@@ -66,9 +60,7 @@ type IssueCountMetric struct {
// ProjectCountMetric is the API message for project count metric.
type ProjectCountMetric struct {
- WorkflowType v1pb.Workflow
- RowStatus api.RowStatus
- Count int
+ Count int
}
// PolicyCountMetric is the API message for policy count metric.
@@ -77,7 +69,6 @@ type PolicyCountMetric struct {
Value string
EnvironmentName string
Count int
- RowStatus api.RowStatus
}
// TaskCountMetric is the API message for task count metric.
diff --git a/backend/migrator/migration/3.4/0012##row_status.sql b/backend/migrator/migration/3.4/0012##row_status.sql
new file mode 100644
index 0000000000..e3e30612d5
--- /dev/null
+++ b/backend/migrator/migration/3.4/0012##row_status.sql
@@ -0,0 +1,36 @@
+ALTER TABLE plan DROP COLUMN IF EXISTS row_status;
+ALTER TABLE issue DROP COLUMN IF EXISTS row_status;
+
+ALTER TABLE idp ADD COLUMN IF NOT EXISTS deleted boolean NOT NULL DEFAULT FALSE;
+UPDATE idp SET deleted = TRUE WHERE row_status = 'ARCHIVED';
+ALTER TABLE idp DROP COLUMN IF EXISTS row_status;
+
+ALTER TABLE principal ADD COLUMN IF NOT EXISTS deleted boolean NOT NULL DEFAULT FALSE;
+UPDATE principal SET deleted = TRUE WHERE row_status = 'ARCHIVED';
+ALTER TABLE principal DROP COLUMN IF EXISTS row_status;
+
+ALTER TABLE environment ADD COLUMN IF NOT EXISTS deleted boolean NOT NULL DEFAULT FALSE;
+UPDATE environment SET deleted = TRUE WHERE row_status = 'ARCHIVED';
+ALTER TABLE environment DROP COLUMN IF EXISTS row_status;
+
+ALTER TABLE project ADD COLUMN IF NOT EXISTS deleted boolean NOT NULL DEFAULT FALSE;
+UPDATE project SET deleted = TRUE WHERE row_status = 'ARCHIVED';
+ALTER TABLE project DROP COLUMN IF EXISTS row_status;
+
+ALTER TABLE instance ADD COLUMN IF NOT EXISTS deleted boolean NOT NULL DEFAULT FALSE;
+UPDATE instance SET deleted = TRUE WHERE row_status = 'ARCHIVED';
+ALTER TABLE instance DROP COLUMN IF EXISTS row_status;
+
+ALTER TABLE release ADD COLUMN IF NOT EXISTS deleted boolean NOT NULL DEFAULT FALSE;
+UPDATE release SET deleted = TRUE WHERE row_status = 'ARCHIVED';
+ALTER TABLE release DROP COLUMN IF EXISTS row_status;
+
+ALTER TABLE policy ADD COLUMN IF NOT EXISTS enforce boolean NOT NULL DEFAULT TRUE;
+UPDATE policy SET enforce = FALSE WHERE row_status = 'ARCHIVED';
+ALTER TABLE policy DROP COLUMN IF EXISTS row_status;
+
+ALTER TABLE review_config ADD COLUMN IF NOT EXISTS enabled boolean NOT NULL DEFAULT TRUE;
+UPDATE review_config SET enabled = FALSE WHERE row_status = 'ARCHIVED';
+ALTER TABLE review_config DROP COLUMN IF EXISTS row_status;
+
+DROP TYPE row_status;
diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sql
index 58ace734c5..90856adbb2 100644
--- a/backend/migrator/migration/LATEST.sql
+++ b/backend/migrator/migration/LATEST.sql
@@ -1,10 +1,7 @@
--- Type
-CREATE TYPE row_status AS ENUM ('NORMAL', 'ARCHIVED');
-
-- idp stores generic identity provider.
CREATE TABLE idp (
id serial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
+ deleted boolean NOT NULL DEFAULT FALSE,
resource_id text NOT NULL,
name text NOT NULL,
domain text NOT NULL,
@@ -20,7 +17,7 @@ ALTER SEQUENCE idp_id_seq RESTART WITH 101;
-- principal
CREATE TABLE principal (
id serial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
+ deleted boolean NOT NULL DEFAULT FALSE,
created_at timestamptz NOT NULL DEFAULT now(),
type text NOT NULL CHECK (type IN ('END_USER', 'SYSTEM_BOT', 'SERVICE_ACCOUNT')),
name text NOT NULL,
@@ -59,7 +56,7 @@ ALTER SEQUENCE role_id_seq RESTART WITH 101;
-- Environment
CREATE TABLE environment (
id serial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
+ deleted boolean NOT NULL DEFAULT FALSE,
name text NOT NULL,
"order" integer NOT NULL CHECK ("order" >= 0),
resource_id text NOT NULL
@@ -73,7 +70,7 @@ ALTER SEQUENCE environment_id_seq RESTART WITH 101;
-- policy stores the policies for each resources.
CREATE TABLE policy (
id serial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
+ enforce boolean NOT NULL DEFAULT TRUE,
updated_at timestamptz NOT NULL DEFAULT now(),
resource_type text NOT NULL CHECK (resource_type IN ('WORKSPACE', 'ENVIRONMENT', 'PROJECT', 'INSTANCE')),
resource TEXT NOT NULL,
@@ -89,7 +86,7 @@ ALTER SEQUENCE policy_id_seq RESTART WITH 101;
-- Project
CREATE TABLE project (
id serial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
+ deleted boolean NOT NULL DEFAULT FALSE,
name text NOT NULL,
resource_id text NOT NULL,
data_classification_config_id text NOT NULL DEFAULT '',
@@ -116,7 +113,7 @@ ALTER SEQUENCE project_webhook_id_seq RESTART WITH 101;
-- Instance
CREATE TABLE instance (
id serial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
+ deleted boolean NOT NULL DEFAULT FALSE,
environment text REFERENCES environment(resource_id),
name text NOT NULL,
engine text NOT NULL,
@@ -312,7 +309,6 @@ ALTER SEQUENCE task_run_log_id_seq RESTART WITH 101;
-- Plan related BEGIN
CREATE TABLE plan (
id bigserial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
creator_id integer NOT NULL REFERENCES principal(id),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
@@ -350,7 +346,6 @@ ALTER SEQUENCE plan_check_run_id_seq RESTART WITH 101;
-- issue
CREATE TABLE issue (
id serial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
creator_id integer NOT NULL REFERENCES principal(id),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
@@ -630,7 +625,7 @@ CREATE TABLE user_group (
-- review config table.
CREATE TABLE review_config (
id text NOT NULL PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
+ enabled boolean NOT NULL DEFAULT TRUE,
name text NOT NULL,
payload jsonb NOT NULL DEFAULT '{}'
);
@@ -685,7 +680,7 @@ CREATE INDEX IF NOT EXISTS idx_changelog_instance_db_name ON changelog (instance
CREATE TABLE IF NOT EXISTS release (
id bigserial PRIMARY KEY,
- row_status row_status NOT NULL DEFAULT 'NORMAL',
+ deleted boolean NOT NULL DEFAULT FALSE,
project text NOT NULL REFERENCES project(resource_id),
creator_id integer NOT NULL REFERENCES principal (id),
created_at timestamptz NOT NULL DEFAULT now(),
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index af31309ca5..eb6910d678 100644
--- a/backend/migrator/migrator_test.go
+++ b/backend/migrator/migrator_test.go
@@ -212,5 +212,5 @@ func TestMigrationCompatibility(t *testing.T) {
func TestGetCutoffVersion(t *testing.T) {
releaseVersion, err := getProdCutoffVersion()
require.NoError(t, err)
- require.Equal(t, semver.MustParse("3.4.11"), releaseVersion)
+ require.Equal(t, semver.MustParse("3.4.12"), releaseVersion)
}
diff --git a/backend/server/init.go b/backend/server/init.go
index 72af5f7f27..750f13c20e 100644
--- a/backend/server/init.go
+++ b/backend/server/init.go
@@ -241,8 +241,6 @@ func (s *Server) initMetricReporter() {
metricReporter.Register(metric.InstanceCountMetricName, metriccollector.NewInstanceCountCollector(s.store))
metricReporter.Register(metric.IssueCountMetricName, metriccollector.NewIssueCountCollector(s.store))
metricReporter.Register(metric.ProjectCountMetricName, metriccollector.NewProjectCountCollector(s.store))
- metricReporter.Register(metric.PolicyCountMetricName, metriccollector.NewPolicyCountCollector(s.store))
- metricReporter.Register(metric.TaskCountMetricName, metriccollector.NewTaskCountCollector(s.store))
metricReporter.Register(metric.SheetCountMetricName, metriccollector.NewSheetCountCollector(s.store))
metricReporter.Register(metric.MemberCountMetricName, metriccollector.NewMemberCountCollector(s.store))
s.metricReporter = metricReporter
diff --git a/backend/store/database.go b/backend/store/database.go
index 7e3b24f0c1..92fce5d968 100644
--- a/backend/store/database.go
+++ b/backend/store/database.go
@@ -419,16 +419,16 @@ func (*Store) listDatabaseImplV2(ctx context.Context, tx *Tx, find *FindDatabase
if !find.ShowDeleted {
where, args = append(where, fmt.Sprintf(`
COALESCE(
- (SELECT environment.row_status AS instance_environment_status FROM environment JOIN instance ON environment.resource_id = instance.environment WHERE instance.resource_id = db.instance),
+ (SELECT environment.deleted AS instance_environment_status FROM environment JOIN instance ON environment.resource_id = instance.environment WHERE instance.resource_id = db.instance),
$%d
- ) = $%d`, len(args)+1, len(args)+2)), append(args, api.Normal, api.Normal)
+ ) = $%d`, len(args)+1, len(args)+2)), append(args, false, false)
where, args = append(where, fmt.Sprintf(`
COALESCE(
- (SELECT environment.row_status AS db_environment_status FROM environment WHERE environment.resource_id = db.environment),
+ (SELECT environment.deleted AS db_environment_status FROM environment WHERE environment.resource_id = db.environment),
$%d
- ) = $%d`, len(args)+1, len(args)+2)), append(args, api.Normal, api.Normal)
+ ) = $%d`, len(args)+1, len(args)+2)), append(args, false, false)
- where, args = append(where, fmt.Sprintf("instance.row_status = $%d", len(args)+1)), append(args, api.Normal)
+ where, args = append(where, fmt.Sprintf("instance.deleted = $%d", len(args)+1)), append(args, false)
// We don't show databases that are deleted by users already.
where, args = append(where, fmt.Sprintf("db.sync_status = $%d", len(args)+1)), append(args, api.OK)
}
diff --git a/backend/store/environment.go b/backend/store/environment.go
index 89eb30e589..247a151894 100644
--- a/backend/store/environment.go
+++ b/backend/store/environment.go
@@ -188,11 +188,7 @@ func (s *Store) UpdateEnvironmentV2(ctx context.Context, patch *UpdateEnvironmen
set, args = append(set, fmt.Sprintf(`"order" = $%d`, len(args)+1)), append(args, *v)
}
if v := patch.Delete; v != nil {
- rowStatus := api.Normal
- if *patch.Delete {
- rowStatus = api.Archived
- }
- set, args = append(set, fmt.Sprintf(`"row_status" = $%d`, len(args)+1)), append(args, rowStatus)
+ set, args = append(set, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, *v)
}
args = append(args, patch.UID)
@@ -299,7 +295,7 @@ func listEnvironmentImplV2(ctx context.Context, tx *Tx, find *FindEnvironmentMes
where, args = append(where, fmt.Sprintf("environment.id = $%d", len(args)+1)), append(args, *v)
}
if !find.ShowDeleted {
- where, args = append(where, fmt.Sprintf("environment.row_status = $%d", len(args)+1)), append(args, api.Normal)
+ where, args = append(where, fmt.Sprintf("environment.deleted = $%d", len(args)+1)), append(args, false)
}
var environments []*EnvironmentMessage
@@ -309,7 +305,7 @@ func listEnvironmentImplV2(ctx context.Context, tx *Tx, find *FindEnvironmentMes
environment.resource_id,
environment.name,
environment.order,
- environment.row_status,
+ environment.deleted,
policy.payload
FROM environment
LEFT JOIN policy ON environment.resource_id = policy.resource AND policy.resource_type = 'ENVIRONMENT' AND policy.type = 'bb.policy.environment-tier'
@@ -324,18 +320,16 @@ func listEnvironmentImplV2(ctx context.Context, tx *Tx, find *FindEnvironmentMes
for rows.Next() {
var environment EnvironmentMessage
var tierPayload sql.NullString
- var rowStatus string
if err := rows.Scan(
&environment.UID,
&environment.ResourceID,
&environment.Title,
&environment.Order,
- &rowStatus,
+ &environment.Deleted,
&tierPayload,
); err != nil {
return nil, err
}
- environment.Deleted = convertRowStatusToDeleted(rowStatus)
if tierPayload.Valid {
policy := &storepb.EnvironmentTierPolicy{}
if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(tierPayload.String), policy); err != nil {
@@ -353,7 +347,3 @@ func listEnvironmentImplV2(ctx context.Context, tx *Tx, find *FindEnvironmentMes
return environments, nil
}
-
-func convertRowStatusToDeleted(rowStatus string) bool {
- return rowStatus == string(api.Archived)
-}
diff --git a/backend/store/idp.go b/backend/store/idp.go
index 25454b0a76..279117a08d 100644
--- a/backend/store/idp.go
+++ b/backend/store/idp.go
@@ -203,18 +203,14 @@ func (*Store) updateIdentityProviderImpl(ctx context.Context, tx *Tx, patch *Upd
set, args = append(set, fmt.Sprintf("config = $%d", len(args)+1)), append(args, string(configBytes))
}
if v := patch.Delete; v != nil {
- rowStatus := Normal
- if *patch.Delete {
- rowStatus = Archived
- }
- set, args = append(set, fmt.Sprintf("row_status = $%d", len(args)+1)), append(args, rowStatus)
+ set, args = append(set, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, *v)
}
args = append(args, patch.ResourceID)
identityProvider := &IdentityProviderMessage{}
var identityProviderType string
var identityProviderConfig string
- var rowStatus string
+ var deleted bool
if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE idp
SET `+strings.Join(set, ", ")+`
@@ -226,7 +222,7 @@ func (*Store) updateIdentityProviderImpl(ctx context.Context, tx *Tx, patch *Upd
domain,
type,
config,
- row_status
+ deleted
`, len(args)),
args...,
).Scan(
@@ -236,7 +232,7 @@ func (*Store) updateIdentityProviderImpl(ctx context.Context, tx *Tx, patch *Upd
&identityProvider.Domain,
&identityProviderType,
&identityProviderConfig,
- &rowStatus,
+ &deleted,
); err != nil {
if err == sql.ErrNoRows {
return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("identity provider ID not found: %s", patch.ResourceID)}
@@ -246,7 +242,7 @@ func (*Store) updateIdentityProviderImpl(ctx context.Context, tx *Tx, patch *Upd
identityProvider.Type = convertIdentityProviderType(identityProviderType)
identityProvider.Config = convertIdentityProviderConfigString(identityProvider.Type, identityProviderConfig)
- identityProvider.Deleted = convertRowStatusToDeleted(rowStatus)
+ identityProvider.Deleted = deleted
return identityProvider, nil
}
@@ -259,7 +255,7 @@ func (*Store) listIdentityProvidersImpl(ctx context.Context, tx *Tx, find *FindI
where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
}
if !find.ShowDeleted {
- where, args = append(where, fmt.Sprintf("row_status = $%d", len(args)+1)), append(args, Normal)
+ where, args = append(where, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, false)
}
rows, err := tx.QueryContext(ctx, `
@@ -270,7 +266,7 @@ func (*Store) listIdentityProvidersImpl(ctx context.Context, tx *Tx, find *FindI
domain,
type,
config,
- row_status
+ deleted
FROM idp
WHERE `+strings.Join(where, " AND ")+` ORDER BY id ASC`,
args...,
@@ -285,7 +281,6 @@ func (*Store) listIdentityProvidersImpl(ctx context.Context, tx *Tx, find *FindI
var identityProviderMessage IdentityProviderMessage
var identityProviderType string
var identityProviderConfig string
- var rowStatus string
if err := rows.Scan(
&identityProviderMessage.UID,
&identityProviderMessage.ResourceID,
@@ -293,13 +288,12 @@ func (*Store) listIdentityProvidersImpl(ctx context.Context, tx *Tx, find *FindI
&identityProviderMessage.Domain,
&identityProviderType,
&identityProviderConfig,
- &rowStatus,
+ &identityProviderMessage.Deleted,
); err != nil {
return nil, err
}
identityProviderMessage.Type = convertIdentityProviderType(identityProviderType)
identityProviderMessage.Config = convertIdentityProviderConfigString(identityProviderMessage.Type, identityProviderConfig)
- identityProviderMessage.Deleted = convertRowStatusToDeleted(rowStatus)
identityProviderMessages = append(identityProviderMessages, &identityProviderMessage)
}
if err := rows.Err(); err != nil {
diff --git a/backend/store/instance.go b/backend/store/instance.go
index ae55e4b0e0..d39b668fb2 100644
--- a/backend/store/instance.go
+++ b/backend/store/instance.go
@@ -251,14 +251,12 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa
}
if v := patch.Deleted; v != nil {
if patch.Activation != nil {
- return nil, errors.Errorf(`cannot set "activation" and "row_status" at the same time`)
+ return nil, errors.Errorf(`cannot set "activation" and "deleted" at the same time`)
}
- rowStatus := api.Normal
if *patch.Deleted {
- rowStatus = api.Archived
set, args = append(set, fmt.Sprintf("activation = $%d", len(args)+1)), append(args, false)
}
- set, args = append(set, fmt.Sprintf(`"row_status" = $%d`, len(args)+1)), append(args, rowStatus)
+ set, args = append(set, fmt.Sprintf(`deleted = $%d`, len(args)+1)), append(args, *v)
}
if v := patch.OptionsUpsert; v != nil {
options, err := protojson.Marshal(v)
@@ -299,11 +297,10 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa
engine_version,
external_link,
activation,
- row_status,
+ deleted,
options,
metadata
`, strings.Join(where, " AND "))
- var rowStatus string
var environment sql.NullString
var options, metadata []byte
if err := tx.QueryRowContext(ctx, query, args...).Scan(
@@ -315,7 +312,7 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa
&instance.EngineVersion,
&instance.ExternalLink,
&instance.Activation,
- &rowStatus,
+ &instance.Deleted,
&options,
&metadata,
); err != nil {
@@ -329,7 +326,6 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa
return nil, errors.Errorf("invalid engine %s", engine)
}
instance.Engine = storepb.Engine(engineTypeValue)
- instance.Deleted = convertRowStatusToDeleted(rowStatus)
var instanceOptions storepb.InstanceOptions
if err := common.ProtojsonUnmarshaler.Unmarshal(options, &instanceOptions); err != nil {
@@ -393,7 +389,7 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
where, args = append(where, fmt.Sprintf("instance.id = $%d", len(args)+1)), append(args, *v)
}
if !find.ShowDeleted {
- where, args = append(where, fmt.Sprintf("instance.row_status = $%d", len(args)+1)), append(args, api.Normal)
+ where, args = append(where, fmt.Sprintf("instance.deleted = $%d", len(args)+1)), append(args, false)
}
var instanceMessages []*InstanceMessage
@@ -407,7 +403,7 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
engine_version,
external_link,
activation,
- row_status,
+ deleted,
options,
metadata
FROM instance
@@ -422,7 +418,7 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
for rows.Next() {
var instanceMessage InstanceMessage
var environment sql.NullString
- var engine, rowStatus string
+ var engine string
var options, metadata []byte
if err := rows.Scan(
&instanceMessage.UID,
@@ -433,7 +429,7 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
&instanceMessage.EngineVersion,
&instanceMessage.ExternalLink,
&instanceMessage.Activation,
- &rowStatus,
+ &instanceMessage.Deleted,
&options,
&metadata,
); err != nil {
@@ -448,7 +444,6 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
}
instanceMessage.Engine = storepb.Engine(engineTypeValue)
- instanceMessage.Deleted = convertRowStatusToDeleted(rowStatus)
var instanceOptions storepb.InstanceOptions
if err := common.ProtojsonUnmarshaler.Unmarshal(options, &instanceOptions); err != nil {
return nil, err
@@ -481,7 +476,7 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
return instanceMessages, nil
}
-var countActivateInstanceQuery = "SELECT COUNT(*) FROM instance WHERE activation = TRUE AND row_status = 'NORMAL'"
+var countActivateInstanceQuery = "SELECT COUNT(*) FROM instance WHERE activation = TRUE AND deleted = FALSE"
// CheckActivationLimit checks if activation instance count reaches the limit.
func (s *Store) CheckActivationLimit(ctx context.Context, maximumActivation int) error {
diff --git a/backend/store/policy.go b/backend/store/policy.go
index 2da91fcefa..bcd5f2a01e 100644
--- a/backend/store/policy.go
+++ b/backend/store/policy.go
@@ -349,7 +349,8 @@ type FindPolicyMessage struct {
ResourceType *api.PolicyResourceType
Resource *string
Type *api.PolicyType
- ShowDeleted bool
+ // ShowAll will show all policies regardless of the enforce status.
+ ShowAll bool
}
// UpdatePolicyMessage is the message for updating a policy.
@@ -377,7 +378,7 @@ func (s *Store) GetPolicyV2(ctx context.Context, find *FindPolicyMessage) (*Poli
defer tx.Rollback()
// We will always return the resource regardless of its deleted state.
- find.ShowDeleted = true
+ find.ShowAll = true
policies, err := s.listPolicyImplV2(ctx, tx, find)
if err != nil {
return nil, err
@@ -459,11 +460,7 @@ func (s *Store) UpdatePolicyV2(ctx context.Context, patch *UpdatePolicyMessage)
set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, *v)
}
if v := patch.Enforce; v != nil {
- rowStatus := api.Normal
- if !*patch.Enforce {
- rowStatus = api.Archived
- }
- set, args = append(set, fmt.Sprintf(`"row_status" = $%d`, len(args)+1)), append(args, rowStatus)
+ set, args = append(set, fmt.Sprintf(`enforce = $%d`, len(args)+1)), append(args, *v)
}
args = append(args, patch.ResourceType, patch.Resource, patch.Type)
@@ -478,7 +475,6 @@ func (s *Store) UpdatePolicyV2(ctx context.Context, patch *UpdatePolicyMessage)
ResourceType: patch.ResourceType,
Type: patch.Type,
}
- var rowStatus string
if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE policy
@@ -487,14 +483,14 @@ func (s *Store) UpdatePolicyV2(ctx context.Context, patch *UpdatePolicyMessage)
RETURNING
payload,
inherit_from_parent,
- row_status,
+ enforce,
updated_at
`, len(args)-2, len(args)-1, len(args)),
args...,
).Scan(
&policy.Payload,
&policy.InheritFromParent,
- &rowStatus,
+ &policy.Enforce,
&policy.UpdatedAt,
); err != nil {
if err == sql.ErrNoRows {
@@ -502,9 +498,6 @@ func (s *Store) UpdatePolicyV2(ctx context.Context, patch *UpdatePolicyMessage)
}
return nil, err
}
- if rowStatus == string(api.Normal) {
- policy.Enforce = true
- }
if err := tx.Commit(); err != nil {
return nil, err
@@ -543,10 +536,6 @@ func (s *Store) DeletePolicyV2(ctx context.Context, policy *PolicyMessage) error
func upsertPolicyV2Impl(ctx context.Context, tx *Tx, create *PolicyMessage) (*PolicyMessage, error) {
var uid int
- rowStatus := api.Normal
- if !create.Enforce {
- rowStatus = api.Archived
- }
if err := tx.QueryRowContext(ctx, `
INSERT INTO policy (
resource_type,
@@ -554,13 +543,13 @@ func upsertPolicyV2Impl(ctx context.Context, tx *Tx, create *PolicyMessage) (*Po
inherit_from_parent,
type,
payload,
- row_status
+ enforce
)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT(resource_type, resource, type) DO UPDATE SET
inherit_from_parent = EXCLUDED.inherit_from_parent,
payload = EXCLUDED.payload,
- row_status = EXCLUDED.row_status
+ enforce = EXCLUDED.enforce
RETURNING
id,
updated_at
@@ -570,7 +559,7 @@ func upsertPolicyV2Impl(ctx context.Context, tx *Tx, create *PolicyMessage) (*Po
create.InheritFromParent,
create.Type,
create.Payload,
- rowStatus,
+ create.Enforce,
).Scan(
&uid,
&create.UpdatedAt,
@@ -592,8 +581,8 @@ func (*Store) listPolicyImplV2(ctx context.Context, tx *Tx, find *FindPolicyMess
if v := find.Type; v != nil {
where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, *v)
}
- if !find.ShowDeleted {
- where, args = append(where, fmt.Sprintf("row_status = $%d", len(args)+1)), append(args, api.Normal)
+ if !find.ShowAll {
+ where, args = append(where, fmt.Sprintf("enforce = $%d", len(args)+1)), append(args, true)
}
rows, err := tx.QueryContext(ctx, `
@@ -605,7 +594,7 @@ func (*Store) listPolicyImplV2(ctx context.Context, tx *Tx, find *FindPolicyMess
inherit_from_parent,
type,
payload,
- row_status
+ enforce
FROM policy
WHERE `+strings.Join(where, " AND "),
args...,
@@ -618,8 +607,6 @@ func (*Store) listPolicyImplV2(ctx context.Context, tx *Tx, find *FindPolicyMess
var policyList []*PolicyMessage
for rows.Next() {
var policyMessage PolicyMessage
- var rowStatus api.RowStatus
-
if err := rows.Scan(
&policyMessage.UID,
&policyMessage.UpdatedAt,
@@ -628,13 +615,10 @@ func (*Store) listPolicyImplV2(ctx context.Context, tx *Tx, find *FindPolicyMess
&policyMessage.InheritFromParent,
&policyMessage.Type,
&policyMessage.Payload,
- &rowStatus,
+ &policyMessage.Enforce,
); err != nil {
return nil, err
}
- if rowStatus == api.Normal {
- policyMessage.Enforce = true
- }
policyList = append(policyList, &policyMessage)
}
if err := rows.Err(); err != nil {
diff --git a/backend/store/principal.go b/backend/store/principal.go
index 505115cc15..ba50ab2b12 100644
--- a/backend/store/principal.go
+++ b/backend/store/principal.go
@@ -166,13 +166,13 @@ func listUserImpl(ctx context.Context, tx *Tx, find *FindUserMessage) ([]*UserMe
where, args = append(where, fmt.Sprintf("principal.type = $%d", len(args)+1)), append(args, *v)
}
if !find.ShowDeleted {
- where, args = append(where, fmt.Sprintf("principal.row_status = $%d", len(args)+1)), append(args, api.Normal)
+ where, args = append(where, fmt.Sprintf("principal.deleted = $%d", len(args)+1)), append(args, false)
}
query := `
SELECT
principal.id AS user_id,
- principal.row_status AS row_status,
+ principal.deleted,
principal.email,
principal.name,
principal.type,
@@ -196,12 +196,11 @@ func listUserImpl(ctx context.Context, tx *Tx, find *FindUserMessage) ([]*UserMe
defer rows.Close()
for rows.Next() {
var userMessage UserMessage
- var rowStatus string
var mfaConfigBytes []byte
var profileBytes []byte
if err := rows.Scan(
&userMessage.ID,
- &rowStatus,
+ &userMessage.MemberDeleted,
&userMessage.Email,
&userMessage.Name,
&userMessage.Type,
@@ -214,7 +213,6 @@ func listUserImpl(ctx context.Context, tx *Tx, find *FindUserMessage) ([]*UserMe
return nil, err
}
- userMessage.MemberDeleted = convertRowStatusToDeleted(rowStatus)
mfaConfig := storepb.MFAConfig{}
if err := common.ProtojsonUnmarshaler.Unmarshal(mfaConfigBytes, &mfaConfig); err != nil {
return nil, err
@@ -305,11 +303,7 @@ func (s *Store) UpdateUser(ctx context.Context, currentUser *UserMessage, patch
principalSet, principalArgs := []string{}, []any{}
if v := patch.Delete; v != nil {
- rowStatus := api.Normal
- if *patch.Delete {
- rowStatus = api.Archived
- }
- principalSet, principalArgs = append(principalSet, fmt.Sprintf("row_status = $%d", len(principalArgs)+1)), append(principalArgs, rowStatus)
+ principalSet, principalArgs = append(principalSet, fmt.Sprintf("deleted = $%d", len(principalArgs)+1)), append(principalArgs, *v)
}
if v := patch.Email; v != nil {
principalSet, principalArgs = append(principalSet, fmt.Sprintf("email = $%d", len(principalArgs)+1)), append(principalArgs, strings.ToLower(*v))
diff --git a/backend/store/project.go b/backend/store/project.go
index a29c220abb..d10f0f48a0 100644
--- a/backend/store/project.go
+++ b/backend/store/project.go
@@ -227,11 +227,7 @@ func updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdateProjectMessag
set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)
}
if v := patch.Delete; v != nil {
- rowStatus := api.Normal
- if *patch.Delete {
- rowStatus = api.Archived
- }
- set, args = append(set, fmt.Sprintf(`"row_status" = $%d`, len(args)+1)), append(args, rowStatus)
+ set, args = append(set, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, *v)
}
if v := patch.DataClassificationConfigID; v != nil {
set, args = append(set, fmt.Sprintf("data_classification_config_id = $%d", len(args)+1)), append(args, *v)
@@ -265,7 +261,7 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject
where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
}
if !find.ShowDeleted {
- where, args = append(where, fmt.Sprintf("row_status = $%d", len(args)+1)), append(args, api.Normal)
+ where, args = append(where, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, false)
}
var projectMessages []*ProjectMessage
@@ -277,7 +273,7 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject
data_classification_config_id,
(SELECT COUNT(1) FROM vcs_connector WHERE project.resource_id = vcs_connector.project) AS connectors,
setting,
- row_status
+ deleted
FROM project
WHERE %s
ORDER BY project.resource_id`, strings.Join(where, " AND ")),
@@ -291,7 +287,6 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject
for rows.Next() {
var projectMessage ProjectMessage
var payload []byte
- var rowStatus string
if err := rows.Scan(
&projectMessage.UID,
&projectMessage.ResourceID,
@@ -299,7 +294,7 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject
&projectMessage.DataClassificationConfigID,
&projectMessage.VCSConnectorsCount,
&payload,
- &rowStatus,
+ &projectMessage.Deleted,
); err != nil {
return nil, err
}
@@ -308,7 +303,6 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject
return nil, err
}
projectMessage.Setting = setting
- projectMessage.Deleted = convertRowStatusToDeleted(rowStatus)
projectMessages = append(projectMessages, &projectMessage)
}
if err := rows.Err(); err != nil {
diff --git a/backend/store/release.go b/backend/store/release.go
index 7e2963af41..690eda2b78 100644
--- a/backend/store/release.go
+++ b/backend/store/release.go
@@ -11,7 +11,6 @@ import (
"google.golang.org/protobuf/encoding/protojson"
"github.com/bytebase/bytebase/backend/common"
- api "github.com/bytebase/bytebase/backend/legacyapi"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -111,13 +110,13 @@ func (s *Store) ListReleases(ctx context.Context, find *FindReleaseMessage) ([]*
args = append(args, *v)
}
if !find.ShowDeleted {
- where, args = append(where, fmt.Sprintf("row_status = $%d", len(args)+1)), append(args, api.Normal)
+ where, args = append(where, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, false)
}
query := fmt.Sprintf(`
SELECT
id,
- row_status,
+ deleted,
project,
creator_id,
created_at,
@@ -151,12 +150,11 @@ func (s *Store) ListReleases(ctx context.Context, find *FindReleaseMessage) ([]*
r := ReleaseMessage{
Payload: &storepb.ReleasePayload{},
}
- var rowStatus string
var payload []byte
if err := rows.Scan(
&r.UID,
- &rowStatus,
+ &r.Deleted,
&r.ProjectID,
&r.CreatorUID,
&r.At,
@@ -165,7 +163,6 @@ func (s *Store) ListReleases(ctx context.Context, find *FindReleaseMessage) ([]*
return nil, errors.Wrapf(err, "failed to scan rows")
}
- r.Deleted = convertRowStatusToDeleted(rowStatus)
if err := common.ProtojsonUnmarshaler.Unmarshal(payload, r.Payload); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal payload")
}
@@ -188,11 +185,7 @@ func (s *Store) UpdateRelease(ctx context.Context, update *UpdateReleaseMessage)
set, args := []string{}, []any{}
if v := update.Deleted; v != nil {
- rowStatus := api.Normal
- if *v {
- rowStatus = api.Archived
- }
- set, args = append(set, fmt.Sprintf(`row_status = $%d`, len(args)+1)), append(args, rowStatus)
+ set, args = append(set, fmt.Sprintf(`deleted = $%d`, len(args)+1)), append(args, *v)
}
if v := update.Payload; v != nil {
payload, err := protojson.Marshal(update.Payload)
diff --git a/backend/store/review_config.go b/backend/store/review_config.go
index a7cec7bbf9..dee3e5c1a3 100644
--- a/backend/store/review_config.go
+++ b/backend/store/review_config.go
@@ -10,7 +10,6 @@ import (
"google.golang.org/protobuf/encoding/protojson"
"github.com/bytebase/bytebase/backend/common"
- api "github.com/bytebase/bytebase/backend/legacyapi"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -68,7 +67,7 @@ func (s *Store) ListReviewConfigs(ctx context.Context, find *FindReviewConfigMes
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT
id,
- row_status,
+ enabled,
name,
payload
FROM review_config
@@ -84,11 +83,10 @@ func (s *Store) ListReviewConfigs(ctx context.Context, find *FindReviewConfigMes
for rows.Next() {
var sqlReview ReviewConfigMessage
var payload []byte
- var rowStatus string
if err := rows.Scan(
&sqlReview.ID,
- &rowStatus,
+ &sqlReview.Enforce,
&sqlReview.Name,
&payload,
); err != nil {
@@ -100,7 +98,6 @@ func (s *Store) ListReviewConfigs(ctx context.Context, find *FindReviewConfigMes
return nil, err
}
- sqlReview.Enforce = rowStatus == string(api.Normal)
sqlReview.Payload = reviewConfigPyload
sqlReviewList = append(sqlReviewList, &sqlReview)
}
@@ -177,11 +174,7 @@ func (s *Store) DeleteReviewConfig(ctx context.Context, id string) error {
func (s *Store) UpdateReviewConfig(ctx context.Context, patch *PatchReviewConfigMessage) (*ReviewConfigMessage, error) {
set, args := []string{}, []any{}
if v := patch.Enforce; v != nil {
- rowStatus := api.Normal
- if !*patch.Enforce {
- rowStatus = api.Archived
- }
- set, args = append(set, fmt.Sprintf(`"row_status" = $%d`, len(args)+1)), append(args, rowStatus)
+ set, args = append(set, fmt.Sprintf("enabled = $%d", len(args)+1)), append(args, *v)
}
if v := patch.Name; v != nil {
set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)
@@ -207,18 +200,17 @@ func (s *Store) UpdateReviewConfig(ctx context.Context, patch *PatchReviewConfig
WHERE id = $%d
RETURNING
id,
- row_status,
+ enabled,
name,
payload
`, len(args))
var sqlReview ReviewConfigMessage
var payload []byte
- var rowStatus string
if err := tx.QueryRowContext(ctx, query, args...).Scan(
&sqlReview.ID,
- &rowStatus,
+ &sqlReview.Enforce,
&sqlReview.Name,
&payload,
); err != nil {
@@ -233,7 +225,6 @@ func (s *Store) UpdateReviewConfig(ctx context.Context, patch *PatchReviewConfig
return nil, err
}
- sqlReview.Enforce = rowStatus == string(api.Normal)
sqlReview.Payload = reviewConfigPyload
if err := tx.Commit(); err != nil {
diff --git a/backend/store/stats.go b/backend/store/stats.go
index a12d15b047..2d4f66b954 100644
--- a/backend/store/stats.go
+++ b/backend/store/stats.go
@@ -12,7 +12,6 @@ import (
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/metric"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
- v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
// CountInstanceMessage is the message for counting instances.
@@ -47,7 +46,7 @@ func (s *Store) CountUsers(ctx context.Context, userType api.PrincipalType) (int
// CountInstance counts the number of instances.
func (s *Store) CountInstance(ctx context.Context, find *CountInstanceMessage) (int, error) {
- where, args := []string{"instance.row_status = $1"}, []any{api.Normal}
+ where, args := []string{"instance.deleted = $1"}, []any{false}
if v := find.EnvironmentID; v != nil {
where, args = append(where, fmt.Sprintf("instance.environment = $%d", len(args)+1)), append(args, *v)
}
@@ -85,9 +84,9 @@ func (s *Store) CountActiveUsers(ctx context.Context) (int, error) {
SELECT
count(DISTINCT principal.id)
FROM principal
- WHERE principal.row_status = $1 AND (principal.type = $2 OR principal.type = $3)`
+ WHERE principal.deleted = $1 AND (principal.type = $2 OR principal.type = $3)`
var count int
- if err := tx.QueryRowContext(ctx, query, api.Normal, api.EndUser, api.ServiceAccount).Scan(&count); err != nil {
+ if err := tx.QueryRowContext(ctx, query, false, api.EndUser, api.ServiceAccount).Scan(&count); err != nil {
if err == sql.ErrNoRows {
return 0, common.FormatDBErrorEmptyRowWithQuery(query)
}
@@ -99,56 +98,28 @@ func (s *Store) CountActiveUsers(ctx context.Context) (int, error) {
return count, nil
}
-// CountProjectGroupByWorkflow counts the number of projects and group by workflow type.
+// CountProjects counts the number of projects and group by workflow type.
// Used by the metric collector.
-func (s *Store) CountProjectGroupByWorkflow(ctx context.Context) ([]*metric.ProjectCountMetric, error) {
+func (s *Store) CountProjects(ctx context.Context) (int, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
- return nil, err
+ return 0, err
}
defer tx.Rollback()
- rows, err := tx.QueryContext(ctx, `
- WITH project_workflow AS (
- SELECT
- project.row_status as row_status,
- (SELECT COUNT(1) FROM vcs_connector WHERE project.resource_id = vcs_connector.project) > 0 AS has_connector
- FROM project
- WHERE resource_id != 'default'
- )
+ var count int
+ if err := tx.QueryRowContext(ctx, `
SELECT
- has_connector,
- row_status,
- COUNT(*)
- FROM project_workflow
- GROUP BY has_connector, row_status`,
- )
- if err != nil {
- return nil, err
- }
- defer rows.Close()
-
- var res []*metric.ProjectCountMetric
- for rows.Next() {
- var metric metric.ProjectCountMetric
- var hasConnector bool
- if err := rows.Scan(&hasConnector, &metric.RowStatus, &metric.Count); err != nil {
- return nil, err
- }
- workflow := v1pb.Workflow_UI
- if hasConnector {
- workflow = v1pb.Workflow_VCS
- }
- metric.WorkflowType = workflow
- res = append(res, &metric)
- }
- if err := rows.Err(); err != nil {
- return nil, err
+ COUNT(1)
+ FROM project
+ WHERE deleted = FALSE`,
+ ).Scan(&count); err != nil {
+ return 0, err
}
if err := tx.Commit(); err != nil {
- return nil, err
+ return 0, err
}
- return res, nil
+ return count, nil
}
// CountIssueGroupByTypeAndStatus counts the number of issue and group by type and status.
@@ -199,10 +170,10 @@ func (s *Store) CountInstanceGroupByEngineAndEnvironmentID(ctx context.Context)
defer tx.Rollback()
rows, err := tx.QueryContext(ctx, `
- SELECT engine, environment, row_status, COUNT(*)
+ SELECT engine, environment, COUNT(1)
FROM instance
- WHERE id > 101
- GROUP BY engine, environment, row_status`,
+ WHERE id > 101 AND deleted = FALSE
+ GROUP BY engine, environment`,
)
if err != nil {
return nil, err
@@ -213,7 +184,7 @@ func (s *Store) CountInstanceGroupByEngineAndEnvironmentID(ctx context.Context)
for rows.Next() {
var metric metric.InstanceCountMetric
var engine string
- if err := rows.Scan(&engine, &metric.EnvironmentID, &metric.RowStatus, &metric.Count); err != nil {
+ if err := rows.Scan(&engine, &metric.EnvironmentID, &metric.Count); err != nil {
return nil, err
}
engineTypeValue, ok := storepb.Engine_value[engine]
@@ -269,7 +240,7 @@ func (s *Store) CountTaskGroupByTypeAndStatus(ctx context.Context) ([]*metric.Ta
return res, nil
}
-// CountSheetGroupByRowstatusVisibilitySourceAndType counts the number of sheets group by row_status, visibility, source and type.
+// CountSheetGroupByRowstatusVisibilitySourceAndType counts the number of sheets group by visibility, source and type.
// Used by the metric collector.
func (s *Store) CountSheetGroupByRowstatusVisibilitySourceAndType(ctx context.Context) ([]*metric.SheetCountMetric, error) {
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})