Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: handle legacy data in specific workspace (#11170)
ecmadao81be69a8e927
diff --git a/backend/common/db_schema.go b/backend/common/db_schema.goindex 6b1b6fd612..1c19caf1c3 100644--- a/backend/common/db_schema.go+++ b/backend/common/db_schema.go@@ -2,6 +2,7 @@ package commonimport ("fmt"+ "os""regexp""strings")@@ -9,8 +10,9 @@ import (// classificationIDPattern is the pattern for classification, it should joined id (numbers) by "-", like 1-2, 1-2-3.const classificationIDPattern = "^[0-9]+(-([0-9])+){0,}"-// GetClassificationAndUserComment parses classification and user comment from the given comment.-func GetClassificationAndUserComment(comment string) (string, string) {+var numberReg = regexp.MustCompile("^[0-9]+$")++func getClassificationAndUserCommentFromLegacy(comment string) (string, string) {classificationIDReg := regexp.MustCompile(classificationIDPattern)classification := classificationIDReg.FindString(comment)if classification == comment {@@ -28,6 +30,28 @@ func GetClassificationAndUserComment(comment string) (string, string) {return classification, userComment}+// GetClassificationAndUserComment parses classification and user comment from the given comment.+func GetClassificationAndUserComment(comment string) (string, string) {+ workspaceID := os.Getenv("BYTEBASE_WORKSPACE_ID")+ if workspaceID == "a4fea42c-c097-47c3-b661-4a6fcea1cf6d" {+ // handle legacy data in specific workspace+ return getClassificationAndUserCommentFromLegacy(comment)+ }++ sections := strings.Split(comment, "-")+ classification := []string{}+ userComment := ""+ for i, section := range sections {+ if numberReg.MatchString(section) {+ classification = append(classification, section)+ } else {+ userComment = strings.Join(sections[i:], "-")+ break+ }+ }+ return strings.Join(classification, "-"), userComment+}+// GetCommentFromClassificationAndUserComment returns the comment from the given classification and user comment.func GetCommentFromClassificationAndUserComment(classification, userComment string) string {if classification == "" {diff --git a/backend/common/db_schema_test.go b/backend/common/db_schema_test.goindex 01ddda9216..7a07e49675 100644--- a/backend/common/db_schema_test.go+++ b/backend/common/db_schema_test.go@@ -2,6 +2,7 @@ package commonimport ("fmt"+ "os""testing""github.com/stretchr/testify/assert"@@ -13,6 +14,7 @@ func TestClassification(t *testing.T) {classification stringuserComment stringrebuildComment string+ workspaceID string}{{rawComment: "1abc",@@ -49,12 +51,14 @@ func TestClassification(t *testing.T) {classification: "1-2",userComment: "abc",rebuildComment: "1-2-abc",+ workspaceID: "a4fea42c-c097-47c3-b661-4a6fcea1cf6d",},{rawComment: "1 abc",classification: "1",userComment: "abc",rebuildComment: "1-abc",+ workspaceID: "a4fea42c-c097-47c3-b661-4a6fcea1cf6d",},{rawComment: "1",@@ -73,6 +77,7 @@ func TestClassification(t *testing.T) {classification: "1",userComment: "2",rebuildComment: "1-2",+ workspaceID: "a4fea42c-c097-47c3-b661-4a6fcea1cf6d",},{rawComment: "1-2",@@ -96,6 +101,8 @@ func TestClassification(t *testing.T) {for _, test := range tests {t.Run(fmt.Sprintf("test classification for comment: %v", test.rawComment), func(t *testing.T) {+ err := os.Setenv("BYTEBASE_WORKSPACE_ID", test.workspaceID)+ assert.NoError(t, err)classification, userComment := GetClassificationAndUserComment(test.rawComment)assert.Equal(t, test.classification, classification)assert.Equal(t, test.userComment, userComment)diff --git a/backend/server/init.go b/backend/server/init.goindex 3e49aae777..7882cc3351 100644--- a/backend/server/init.go+++ b/backend/server/init.go@@ -2,6 +2,7 @@ package serverimport ("context"+ "os""time""github.com/google/uuid"@@ -48,11 +49,15 @@ func (s *Server) getInitSetting(ctx context.Context, datastore *store.Store) (stsecret = authSetting.Value// initial workspace- if _, _, err := datastore.CreateSettingIfNotExistV2(ctx, &store.SettingMessage{+ workspaceIDSetting, _, err := datastore.CreateSettingIfNotExistV2(ctx, &store.SettingMessage{Name: api.SettingWorkspaceID,Value: uuid.New().String(),Description: "The workspace identifier",- }, api.SystemBotID); err != nil {+ }, api.SystemBotID)+ if err != nil {+ return "", 0, err+ }+ if err := os.Setenv("BYTEBASE_WORKSPACE_ID", workspaceIDSetting.Value); err != nil {return "", 0, err}