Skip to content

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

fix: calculate matched/unmatched databases (#17684)

ecmadao8056a53e84a3
diff --git a/backend/migrator/migration/3.11/0004##migrate_cel_attribute_names.sql b/backend/migrator/migration/3.11/0004##migrate_cel_attribute_names.sql
index dd72d03b48..67387b7fca 100644
--- a/backend/migrator/migration/3.11/0004##migrate_cel_attribute_names.sql
+++ b/backend/migrator/migration/3.11/0004##migrate_cel_attribute_names.sql
@@ -13,7 +13,7 @@
-- 7. table_name -> resource.table_name
--
-- Attribute name changes (renaming for consistency):
--- 8. resource.environment_name -> resource.environment_id (in IAM policies)
+-- 8. resource.environment_name -> resource.environment_id (in IAM policies and database groups)
-- 9. resource.schema -> resource.schema_name (in IAM policies)
-- 10. resource.table -> resource.table_name (in IAM policies)
-- 11. resource.labels -> resource.database_labels (in database groups)
@@ -119,6 +119,28 @@ WHERE expression->>'expression' ~ '\m(environment_id|project_id|instance_id|db_e
-- Update database group expressions
-- The db_group.expression column stores CEL expressions in jsonb format with an "expression" field
+-- Change resource.environment_name -> resource.environment_id and remove "environments/" prefix
+UPDATE db_group
+SET expression = jsonb_set(
+ expression,
+ '{expression}',
+ to_jsonb(
+ regexp_replace(
+ regexp_replace(
+ expression->>'expression',
+ 'resource\.environment_name',
+ 'resource.environment_id',
+ 'g'
+ ),
+ '"environments/',
+ '"',
+ 'g'
+ )
+ )
+)
+WHERE expression->>'expression' ~ 'resource\.environment_name';
+
+-- Update database group expressions - replace resource.labels with resource.database_labels
UPDATE db_group
SET expression = jsonb_set(
expression,
diff --git a/backend/tests/grouping_change_test.go b/backend/tests/grouping_change_test.go
index 15fff05667..1304bca9b0 100644
--- a/backend/tests/grouping_change_test.go
+++ b/backend/tests/grouping_change_test.go
@@ -147,7 +147,7 @@ func TestCreateDatabaseGroup(t *testing.T) {
DatabaseGroup: &v1pb.DatabaseGroup{
Title: tc.databaseGroupPlaceholder,
DatabaseExpr: &expr.Expr{
- Expression: fmt.Sprintf(`(resource.environment_id == "environments/prod" && (%s))`, tc.databaseGroupExpr),
+ Expression: fmt.Sprintf(`(resource.environment_id == "prod" && (%s))`, tc.databaseGroupExpr),
},
},
}))
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index 9a5029b7b1..bee199b4e7 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -193,7 +193,7 @@ func CheckDatabaseGroupMatch(ctx context.Context, expression string, database *s
}
res, _, err := prog.ContextEval(ctx, map[string]any{
common.CELAttributeResourceDatabaseName: database.DatabaseName,
- common.CELAttributeResourceEnvironmentID: common.FormatEnvironment(effectiveEnvironmentID),
+ common.CELAttributeResourceEnvironmentID: effectiveEnvironmentID,
common.CELAttributeResourceInstanceID: database.InstanceID,
common.CELAttributeResourceDatabaseLabels: database.Metadata.Labels,
})
diff --git a/frontend/src/components/DatabaseGroup/MatchedDatabaseView.vue b/frontend/src/components/DatabaseGroup/MatchedDatabaseView.vue
index fc37c3a634..f97c73724c 100644
--- a/frontend/src/components/DatabaseGroup/MatchedDatabaseView.vue
+++ b/frontend/src/components/DatabaseGroup/MatchedDatabaseView.vue
@@ -219,13 +219,13 @@ const updateDatabaseMatchingState = useDebounceFn(async () => {
});
state.matchingError = undefined;
+ state.databaseMatchLists = getInitialState();
matchedDatabaseNameList.value = matchedDatabaseList;
await Promise.all(
state.databaseMatchLists.map((item) => loadMoreDatabase(item))
);
} catch (error) {
state.matchingError = (error as ConnectError).message;
- state.databaseMatchLists = getInitialState();
} finally {
state.loading = false;
}