Skip to content

Commit 3536930

Browse files
authored
Merge pull request #153808 from spilchen/backport25.2-153787
release-25.2: sql/schemachanger: fix lost dependencies in ALTER POLICY expressions
2 parents 694c9e8 + 8c8d0eb commit 3536930

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

pkg/sql/logictest/testdata/logic_test/row_level_security

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4818,3 +4818,63 @@ statement ok
48184818
DROP ROLE parent_role
48194819

48204820
subtest end
4821+
4822+
# Test that dependencies are preserved when altering only the USING expression
4823+
# of a policy that has both USING and WITH CHECK expressions. This is a repro
4824+
# for GH issue 153191.
4825+
subtest bug_fix_lost_dependencies_alter_policy_#153191
4826+
4827+
statement ok
4828+
CREATE TABLE dep_test (id INT PRIMARY KEY, value INT);
4829+
4830+
statement ok
4831+
CREATE SEQUENCE seq_with_check;
4832+
4833+
statement ok
4834+
CREATE FUNCTION func_using_old(n INT) RETURNS BOOL AS $$ SELECT n > 0; $$ LANGUAGE SQL;
4835+
4836+
statement ok
4837+
CREATE FUNCTION func_using_new(n INT) RETURNS BOOL AS $$ SELECT n >= 0; $$ LANGUAGE SQL;
4838+
4839+
# Create a olicy that have different dependencies for USING and WITH CHECK
4840+
# expressions.
4841+
statement ok
4842+
CREATE POLICY test_policy ON dep_test
4843+
FOR ALL
4844+
USING (func_using_old(value))
4845+
WITH CHECK (nextval('seq_with_check') < 1000);
4846+
4847+
# Verify initial dependencies are tracked.
4848+
statement error pq: cannot drop function "func_using_old" because other objects
4849+
DROP FUNCTION func_using_old;
4850+
4851+
statement error pq: cannot drop sequence seq_with_check because other objects depend on it
4852+
DROP SEQUENCE seq_with_check;
4853+
4854+
# Alter only the USING expression, which should preserve the WITH CHECK
4855+
# dependency.
4856+
statement ok
4857+
ALTER POLICY test_policy ON dep_test USING (func_using_new(value));
4858+
4859+
# Ensure dependency on WITH CHECK still exists.
4860+
statement error pq: cannot drop sequence seq_with_check because other objects depend on it
4861+
DROP SEQUENCE seq_with_check;
4862+
4863+
# Ensure old dependency on USING doesn't exist anymore.
4864+
statement ok
4865+
DROP FUNCTION func_using_old;
4866+
4867+
# And the new USING function should be protected.
4868+
statement error pq: cannot drop function "func_using_new" because other objects
4869+
DROP FUNCTION func_using_new;
4870+
4871+
statement ok
4872+
DROP TABLE dep_test CASCADE;
4873+
4874+
statement ok
4875+
DROP SEQUENCE seq_with_check;
4876+
4877+
statement ok
4878+
DROP FUNCTION func_using_new;
4879+
4880+
subtest end

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/create_policy.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ func upsertPolicyExpressions(
204204
} else {
205205
// If we aren't dropping the old expression, we need to keep track of
206206
// the dependencies in case we replace PolicyDeps.
207-
usesTypeIDs = catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...)
208-
usesRelationIDs = catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...)
209-
usesFunctionIDs = catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...)
207+
usesTypeIDs = usesTypeIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...))
208+
usesRelationIDs = usesRelationIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...))
209+
usesFunctionIDs = usesFunctionIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...))
210210
}
211211
}
212212
})
@@ -217,9 +217,9 @@ func upsertPolicyExpressions(
217217
PolicyID: policyID,
218218
Expression: *expr,
219219
})
220-
usesTypeIDs = catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...)
221-
usesRelationIDs = catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...)
222-
usesFunctionIDs = catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...)
220+
usesTypeIDs = usesTypeIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...))
221+
usesRelationIDs = usesRelationIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...))
222+
usesFunctionIDs = usesFunctionIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...))
223223
}
224224

225225
policyElems.ForEach(func(current scpb.Status, target scpb.TargetStatus, e scpb.Element) {
@@ -230,9 +230,9 @@ func upsertPolicyExpressions(
230230
} else {
231231
// If we aren't dropping the old expression, we need to keep track of
232232
// the dependencies in case we replace PolicyDeps.
233-
usesTypeIDs = catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...)
234-
usesRelationIDs = catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...)
235-
usesFunctionIDs = catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...)
233+
usesTypeIDs = usesTypeIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesTypeIDs...))
234+
usesRelationIDs = usesRelationIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesSequenceIDs...))
235+
usesFunctionIDs = usesFunctionIDs.Union(catalog.MakeDescriptorIDSet(expr.UsesFunctionIDs...))
236236
}
237237
}
238238
})

0 commit comments

Comments
 (0)