PostgreSQL/Diff/Policy.pm

filter: Statementstmt 73.4% 36 / 49 Branchbran 42.8% 12 / 28 Conditioncond 38.4% 5 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 83.3% 5 / 6 Podpod 100% 3 / 3 totaltotal 57.9% 62 / 107 CC 25 SCAR 30.2
CC
25
Cov
59%
diff
29.9
as_sql
13.9
summary
12.0
CRAP 20.4
« PostgreSQL/Introspect/Types.pm ↑ Summary PostgreSQL/Introspect/Columns.pm »
1 package DBIO::PostgreSQL::Diff::Policy;
2 # ABSTRACT: Diff operations for PostgreSQL Row Level Security policies
3  
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 __PACKAGE__->mk_diff_accessors(qw(table_key policy_name policy_info));
10  
11  
12  
13  
14  
15  
16 2 ▶sub diff {
Subroutine diff: called CC=15 SCAR=34.0
Pod documented
17 2 my ($class, $source_pol, $target_pol, $source_tables, $target_tables) = @_;
18 2 my @ops;
19  
20 # RLS enable/disable changes on tables
21 2 for my $key (sort keys %$target_tables) {
22 2 ▶ next unless exists $source_tables->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_tables->{$key} 0 2 2
23 2 my $src = $source_tables->{$key};
24 2 my $tgt = $target_tables->{$key};
25  
26 2 ▶ if (!$src->{rls_enabled} && $tgt->{rls_enabled}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $src->{'rls_enabled'} and $tgt->{'rls_enabled'}) { } 1 1 2
elsif ($src->{'rls_enabled'} and not $tgt->{'rls_enabled'}) { } 0 1 1
Condition66%2/3
not $src->{"rls_enabled"} and $tgt->{"rls_enabled"}
!ll&&!rl&&r
101
MC/DC50%1/2
not $src->{"rls_enabled"} and $tgt->{"rls_enabled"}
not $src->{"rls_enabled"} $tgt->{"rls_enabled"}
Truth table66%2/3
not(A) and B
A= not $src->{"rls_enabled"}B= $tgt->{"rls_enabled"}
ABresult
0X0
100
111
27 1 push @ops, $class->new(
28 action => 'enable_rls',
29 table_key => $key,
30 );
31 }
32 ▶ elsif ($src->{rls_enabled} && !$tgt->{rls_enabled}) {
Condition33%1/3
$src->{"rls_enabled"} and not $tgt->{"rls_enabled"}
!ll&&!rl&&r
010
MC/DC0%0/2
$src->{"rls_enabled"} and not $tgt->{"rls_enabled"}
$src->{"rls_enabled"} not $tgt->{"rls_enabled"}
Truth table33%1/3
A and not(B)
A= $src->{"rls_enabled"}B= not $tgt->{"rls_enabled"}
ABresult
0X0
100
111
33 0 push @ops, $class->new(
34 action => 'disable_rls',
35 table_key => $key,
36 );
37 }
38 }
39  
40 # Policy diffs
41 2 for my $table_key (sort keys %$target_pol) {
42 1 ▶ my $src_pols = $source_pol->{$table_key} // {};
Condition50%1/2
$source_pol->{$table_key} // {}
l!l
01
MC/DC0%0/1
$source_pol->{$table_key} // {}
$source_pol->{$table_key}
Truth table50%1/2
A
A= $source_pol->{$table_key}
Aresult
11
00
43 1 my $tgt_pols = $target_pol->{$table_key};
44  
45 1 for my $name (sort keys %$tgt_pols) {
46 1 ▶ if (!exists $src_pols->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $src_pols->{$name}) 1 0 1
47 push @ops, $class->new(
48 action => 'create',
49 table_key => $table_key,
50 policy_name => $name,
51 1 policy_info => $tgt_pols->{$name},
52 );
53 }
54 }
55 }
56  
57 2 for my $table_key (sort keys %$source_pol) {
58 0 my $src_pols = $source_pol->{$table_key};
59 0 ▶ my $tgt_pols = $target_pol->{$table_key} // {};
Condition0%0/2
$target_pol->{$table_key} // {}
l!l
00
MC/DC0%0/1
$target_pol->{$table_key} // {}
$target_pol->{$table_key}
Truth table0%0/2
A
A= $target_pol->{$table_key}
Aresult
11
00
60  
61 0 for my $name (sort keys %$src_pols) {
62 0 ▶ next if exists $tgt_pols->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt_pols->{$name} 0 0 0
63 push @ops, $class->new(
64 action => 'drop',
65 table_key => $table_key,
66 policy_name => $name,
67 0 policy_info => $src_pols->{$name},
68 );
69 }
70 }
71  
72 2 return @ops;
73 }
74  
75  
76 2 ▶sub as_sql {
Subroutine as_sql: called CC=9 SCAR=26.3
Pod documented
77 2 my ($self) = @_;
78  
79 2 ▶ if ($self->action eq 'enable_rls') {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->action eq 'enable_rls') { } 1 1 2
elsif ($self->action eq 'disable_rls') { } 0 1 1
elsif ($self->action eq 'create') { } 1 0 1
elsif ($self->action eq 'drop') { } 0 0 0
80 1 return sprintf 'ALTER TABLE %s ENABLE ROW LEVEL SECURITY;', $self->table_key;
81 }
82 elsif ($self->action eq 'disable_rls') {
83 0 return sprintf 'ALTER TABLE %s DISABLE ROW LEVEL SECURITY;', $self->table_key;
84 }
85 elsif ($self->action eq 'create') {
86 1 my $info = $self->policy_info;
87 1 my $sql = sprintf 'CREATE POLICY %s ON %s', $self->policy_name, $self->table_key;
88 1 ▶ $sql .= sprintf ' FOR %s', $info->{command} if $info->{command} && $info->{command} ne 'ALL';
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"command"} and $info->{"command"} ne "ALL" 0 1 1
Condition33%1/3
$info->{"command"} and $info->{"command"} ne "ALL"
!ll&&!rl&&r
010
MC/DC0%0/2
$info->{"command"} and $info->{"command"} ne "ALL"
$info->{"command"} $info->{"command"} ne "ALL"
Truth table33%1/3
A and B
A= $info->{"command"}B= $info->{"command"} ne "ALL"
ABresult
0X0
100
111
89 1 ▶ $sql .= sprintf ' USING (%s)', $info->{using_expr} if $info->{using_expr};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"using_expr"} 1 0 1
90 1 ▶ $sql .= sprintf ' WITH CHECK (%s)', $info->{check_expr} if $info->{check_expr};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"check_expr"} 0 1 1
91 1 return "$sql;";
92 }
93 elsif ($self->action eq 'drop') {
94 0 return sprintf 'DROP POLICY %s ON %s;', $self->policy_name, $self->table_key;
95 }
96 }
97  
98  
99 0 ▶sub summary {
Subroutine summary: not called CC=3 SCAR=24.8
Pod documented
100 0 my ($self) = @_;
101 0 ▶ if ($self->action =~ /rls/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action =~ /rls/) 0 0 0
102 0 return sprintf ' %s: RLS on %s', $self->action, $self->table_key;
103 }
104 0 ▶ my $prefix = $self->action eq 'create' ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 0 0 0
105 0 return sprintf ' %spolicy: %s on %s', $prefix, $self->policy_name, $self->table_key;
106 }
107  
108 9 1;
109  
110 __END__
111  
112 =pod
113  
114 =encoding UTF-8
115  
116 =head1 NAME
117  
118 DBIO::PostgreSQL::Diff::Policy - Diff operations for PostgreSQL Row Level Security policies
119  
120 =head1 VERSION
121  
122 version 0.900001
123  
124 =head1 DESCRIPTION
125  
126 Represents a Row Level Security diff operation: C<CREATE POLICY>, C<DROP
127 POLICY>, C<ENABLE ROW LEVEL SECURITY>, or C<DISABLE ROW LEVEL SECURITY>. RLS
128 enable/disable changes on tables are detected by comparing the C<rls_enabled>
129 flag from table introspection.
130  
131 =head1 ATTRIBUTES
132  
133 =head2 table_key
134  
135 The C<schema.table> key identifying the table.
136  
137 =head2 policy_name
138  
139 The policy name (not set for C<enable_rls> / C<disable_rls> operations).
140  
141 =head2 policy_info
142  
143 Policy metadata hashref (C<command>, C<permissive>, C<using_expr>,
144 C<check_expr>, C<roles>).
145  
146 =head1 METHODS
147  
148 =head2 diff
149  
150 my @ops = DBIO::PostgreSQL::Diff::Policy->diff(
151 $source_pol, $target_pol, $source_tables, $target_tables,
152 );
153  
154 Compares RLS state and policy sets. Detects RLS enable/disable changes on
155 existing tables, new policies, and dropped policies.
156  
157 =head2 as_sql
158  
159 Returns the SQL for this operation: C<ALTER TABLE ... ENABLE/DISABLE ROW LEVEL
160 SECURITY>, C<CREATE POLICY ...>, or C<DROP POLICY ... ON ...>.
161  
162 =head2 summary
163  
164 Returns a one-line description such as C<+policy: users_own_data on auth.users>
165 or C<enable_rls on auth.users>.
166  
167 =head1 AUTHOR
168  
169 DBIO & DBIx::Class Authors
170  
171 =head1 COPYRIGHT AND LICENSE
172  
173 Copyright (C) 2026 DBIO Authors
174 Portions Copyright (C) 2005-2025 DBIx::Class Authors
175 Based on DBIx::Class, heavily modified.
176  
177 This is free software; you can redistribute it and/or modify it under
178 the same terms as the Perl 5 programming language system itself.
179  
180 =cut
« PostgreSQL/Introspect/Types.pm ↑ Summary PostgreSQL/Introspect/Columns.pm »