Skip to content

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

fix: update tag policy for reivew config (#17287)

ecmadao827c5eb1f493
diff --git a/backend/api/v1/org_policy_service.go b/backend/api/v1/org_policy_service.go
index a259a71d5a..f94c7c1952 100644
--- a/backend/api/v1/org_policy_service.go
+++ b/backend/api/v1/org_policy_service.go
@@ -546,7 +546,7 @@ func (s *OrgPolicyService) convertToPolicy(ctx context.Context, policyMessage *s
case storepb.Policy_TAG:
p := &v1pb.TagPolicy{}
if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(policyMessage.Payload), p); err != nil {
- return nil, errors.Wrapf(err, "failed to unmarshal rollout policy payload")
+ return nil, errors.Wrapf(err, "failed to unmarshal tag policy payload")
}
policy.Policy = &v1pb.Policy_TagPolicy{
TagPolicy: p,
diff --git a/backend/api/v1/review_config_service.go b/backend/api/v1/review_config_service.go
index 521b0c1b1b..fb52b97a9b 100644
--- a/backend/api/v1/review_config_service.go
+++ b/backend/api/v1/review_config_service.go
@@ -5,6 +5,7 @@ import (
"connectrpc.com/connect"
"github.com/pkg/errors"
+ "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/bytebase/bytebase/backend/common"
@@ -156,6 +157,41 @@ func (s *ReviewConfigService) DeleteReviewConfig(ctx context.Context, req *conne
return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to delete review config"))
}
+ policyType := storepb.Policy_TAG
+ policies, err := s.store.ListPoliciesV2(ctx, &store.FindPolicyMessage{
+ Type: &policyType,
+ })
+ if err != nil {
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to list tag policy"))
+ }
+ for _, policy := range policies {
+ payload := &storepb.TagPolicy{}
+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(policy.Payload), payload); err != nil {
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to unmarshal rollout policy payload"))
+ }
+ reviewConfigName, ok := payload.Tags[common.ReservedTagReviewConfig]
+ if !ok {
+ continue
+ }
+ if reviewConfigName != req.Msg.Name {
+ continue
+ }
+ delete(payload.Tags, common.ReservedTagReviewConfig)
+
+ payloadBytes, err := protojson.Marshal(payload)
+ if err != nil {
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to marshal tag policy"))
+ }
+ patch := string(payloadBytes)
+ if _, err := s.store.UpdatePolicyV2(ctx, &store.UpdatePolicyMessage{
+ ResourceType: policy.ResourceType,
+ Resource: policy.Resource,
+ Payload: &patch,
+ }); err != nil {
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to update tag policy"))
+ }
+ }
+
return connect.NewResponse(&emptypb.Empty{}), nil
}
@@ -206,7 +242,7 @@ func (s *ReviewConfigService) convertToV1ReviewConfig(ctx context.Context, revie
if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(policy.Payload), p); err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to unmarshal tag policy"))
}
- if p.Tags[string(common.ReservedTagReviewConfig)] != config.Name {
+ if p.Tags[common.ReservedTagReviewConfig] != config.Name {
continue
}
diff --git a/backend/store/policy.go b/backend/store/policy.go
index ba3ab1598b..b8db81a6cb 100644
--- a/backend/store/policy.go
+++ b/backend/store/policy.go
@@ -260,7 +260,7 @@ func (s *Store) getReviewConfigByResource(ctx context.Context, resourceType stor
return nil, errors.Wrapf(err, "failed to unmarshal tag policy payload")
}
- reviewConfigName, ok := payload.Tags[string(common.ReservedTagReviewConfig)]
+ reviewConfigName, ok := payload.Tags[common.ReservedTagReviewConfig]
if !ok {
return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("review config tag for resource %v/%s not found", resourceType, resource)}
}
diff --git a/backend/tests/sql_review_test.go b/backend/tests/sql_review_test.go
index 7638d3b853..37e606477d 100644
--- a/backend/tests/sql_review_test.go
+++ b/backend/tests/sql_review_test.go
@@ -117,7 +117,7 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {
Policy: &v1pb.Policy_TagPolicy{
TagPolicy: &v1pb.TagPolicy{
Tags: map[string]string{
- string(common.ReservedTagReviewConfig): createdConfig.Msg.Name,
+ common.ReservedTagReviewConfig: createdConfig.Msg.Name,
},
},
},
@@ -286,7 +286,7 @@ func TestSQLReviewForMySQL(t *testing.T) {
Policy: &v1pb.Policy_TagPolicy{
TagPolicy: &v1pb.TagPolicy{
Tags: map[string]string{
- string(common.ReservedTagReviewConfig): createdConfig.Msg.Name,
+ common.ReservedTagReviewConfig: createdConfig.Msg.Name,
},
},
},
diff --git a/frontend/src/components/SQLReview/SQLReviewCreation.vue b/frontend/src/components/SQLReview/SQLReviewCreation.vue
index 1cce32438b..8581f8443e 100644
--- a/frontend/src/components/SQLReview/SQLReviewCreation.vue
+++ b/frontend/src/components/SQLReview/SQLReviewCreation.vue
@@ -42,6 +42,7 @@
</template>
<script lang="ts" setup>
+import { isEqual } from "lodash-es";
import { useDialog } from "naive-ui";
import { reactive, computed } from "vue";
import { useI18n } from "vue-i18n";
@@ -227,7 +228,9 @@ const tryFinishSetup = async () => {
const policy = await store.upsertReviewPolicy({
title: state.name,
ruleList: convertRuleMapToPolicyRuleList(state.selectedRuleMapByEngine),
- resources: state.attachedResources,
+ resources: isEqual(props.selectedResources, state.attachedResources)
+ ? undefined
+ : state.attachedResources,
id: `${reviewConfigNamePrefix}${state.resourceId}`,
enforce: isUpdate.value ? undefined : true,
});
diff --git a/frontend/src/store/modules/sqlReview.ts b/frontend/src/store/modules/sqlReview.ts
index 197b9dd116..d643fc8756 100644
--- a/frontend/src/store/modules/sqlReview.ts
+++ b/frontend/src/store/modules/sqlReview.ts
@@ -94,8 +94,8 @@ interface SQLReviewState {
reviewPolicyList: SQLReviewPolicy[];
}
-const getTagPolicyName = (environmentPath: string): string => {
- return `${environmentPath}/${policyNamePrefix}tag`;
+const getTagPolicyName = (resourcePath: string): string => {
+ return `${resourcePath}/${policyNamePrefix}tag`;
};
export const useSQLReviewStore = defineStore("sqlReview", {
@@ -162,8 +162,6 @@ export const useSQLReviewStore = defineStore("sqlReview", {
});
await reviewConfigServiceClientConnect.deleteReviewConfig(request);
- await removeReviewConfigTag(targetPolicy.resources);
-
pullAt(this.reviewPolicyList, index);
},
async upsertReviewPolicy({