Skip to content

Commit

Permalink
chore: deprecate member table (bytebase#13826)
Browse files Browse the repository at this point in the history
* chore: deprecate member table

* chore: update
  • Loading branch information
ecmadao authored Sep 20, 2024
1 parent 4fedeef commit 95af4b6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 68 deletions.
16 changes: 9 additions & 7 deletions backend/metric/collector/member_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ func NewMemberCountCollector(store *store.Store) metric.Collector {
func (c *memberCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {
var res []*metric.Metric

memberCountMetricList, err := c.store.CountMemberGroupByRoleAndStatus(ctx)
workspaceIAMPolicy, err := c.store.GetWorkspaceIamPolicy(ctx)
if err != nil {
return nil, err
}

for _, memberCountMetric := range memberCountMetricList {
roleMap := map[string]int{}
for _, binding := range workspaceIAMPolicy.Policy.Bindings {
roleMap[binding.Role] += len(binding.Members)
}

for role, count := range roleMap {
res = append(res, &metric.Metric{
Name: metricapi.MemberCountMetricName,
Value: memberCountMetric.Count,
Value: count,
Labels: map[string]any{
"role": string(memberCountMetric.Role),
"status": string(memberCountMetric.Status),
"row_status": string(memberCountMetric.RowStatus),
"type": string(memberCountMetric.Type),
"role": role,
},
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS member;
17 changes: 0 additions & 17 deletions backend/migrator/migration/prod/LATEST.sql
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,6 @@ CREATE UNIQUE INDEX idx_role_unique_resource_id on role (resource_id);

ALTER SEQUENCE role_id_seq RESTART WITH 101;

-- Member
-- We separate the concept from Principal because if we support multiple workspace in the future, each workspace can have different member for the same principal
CREATE TABLE member (
id SERIAL PRIMARY KEY,
row_status row_status NOT NULL DEFAULT 'NORMAL',
creator_id INTEGER NOT NULL REFERENCES principal (id),
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()),
role TEXT NOT NULL,
principal_id INTEGER NOT NULL REFERENCES principal (id)
);

CREATE INDEX idx_member_principal_id ON member (principal_id);

ALTER SEQUENCE member_id_seq RESTART WITH 101;

-- Environment
CREATE TABLE environment (
id SERIAL PRIMARY KEY,
Expand Down
2 changes: 1 addition & 1 deletion backend/migrator/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,5 @@ func TestMigrationCompatibility(t *testing.T) {
func TestGetCutoffVersion(t *testing.T) {
releaseVersion, err := getProdCutoffVersion()
require.NoError(t, err)
require.Equal(t, semver.MustParse("2.23.0"), releaseVersion)
require.Equal(t, semver.MustParse("2.23.1"), releaseVersion)
}
43 changes: 0 additions & 43 deletions backend/store/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,49 +100,6 @@ func (s *Store) CountActiveUsers(ctx context.Context) (int, error) {
return count, nil
}

// CountMemberGroupByRoleAndStatus counts the number of member and group by role and status.
// Used by the metric collector.
func (s *Store) CountMemberGroupByRoleAndStatus(ctx context.Context) ([]*metric.MemberCountMetric, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()

rows, err := tx.QueryContext(ctx, `
SELECT role, 'ACTIVE' AS status, principal.row_status AS row_status, principal.type, COUNT(*)
FROM member
LEFT JOIN principal ON principal.id = member.principal_id
GROUP BY role, status, principal.row_status, principal.type`,
)
if err != nil {
return nil, err
}
defer rows.Close()

var res []*metric.MemberCountMetric
for rows.Next() {
var metric metric.MemberCountMetric
if err := rows.Scan(
&metric.Role,
&metric.Status,
&metric.RowStatus,
&metric.Type,
&metric.Count,
); err != nil {
return nil, err
}
res = append(res, &metric)
}
if err := rows.Err(); err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
return res, nil
}

// CountProjectGroupByWorkflow 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) {
Expand Down

0 comments on commit 95af4b6

Please sign in to comment.