Skip to content

Commit f3ae3a4

Browse files
author
George Hansper
committed
postgresql::server::grant use SELECT 1 WHERE NOT EXISTS in preference to HAVING count(*)=0
1 parent 89ad955 commit f3ae3a4

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

manifests/server/grant.pp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
# the role does not have the specified privilege, making it necessary to
140140
# execute the GRANT statement.
141141
if $ensure == 'present' {
142-
$custom_unless = "SELECT 1 FROM (
142+
$custom_unless = "SELECT 1 WHERE NOT EXISTS (
143143
SELECT sequence_name
144144
FROM information_schema.sequences
145145
WHERE sequence_schema='${schema}'
@@ -174,11 +174,10 @@
174174
WHERE grantee='${role}'
175175
AND object_schema='${schema}'
176176
AND privilege_type='${custom_privilege}'
177-
) P
178-
HAVING count(P.sequence_name) = 0"
177+
)"
179178
} else {
180179
# ensure == absent
181-
$custom_unless = "SELECT 1 FROM (
180+
$custom_unless = "SELECT 1 WHERE NOT EXISTS (
182181
SELECT object_name as sequence_name
183182
FROM (
184183
SELECT object_schema,
@@ -209,8 +208,7 @@
209208
WHERE grantee='${role}'
210209
AND object_schema='${schema}'
211210
AND privilege_type='${custom_privilege}'
212-
) P
213-
HAVING count(P.sequence_name) = 0"
211+
)"
214212
}
215213
}
216214
'TABLE': {
@@ -251,7 +249,7 @@
251249
if $ensure == 'present' {
252250
if $_privilege == 'ALL' or $_privilege == 'ALL PRIVILEGES' {
253251
# GRANT ALL
254-
$custom_unless = "SELECT 1 FROM
252+
$custom_unless = "SELECT 1 WHERE NOT EXISTS
255253
( SELECT 1 FROM pg_catalog.pg_tables AS t,
256254
(VALUES ('SELECT'), ('UPDATE'), ('INSERT'), ('DELETE'), ('TRIGGER'), ('REFERENCES'), ('TRUNCATE')) AS p(privilege_type)
257255
WHERE t.schemaname = '${schema}'
@@ -261,11 +259,11 @@
261259
AND g.table_schema = '${schema}'
262260
AND g.privilege_type = p.privilege_type
263261
)
264-
) AS privs_missing HAVING privs_missing.count=0"
262+
)"
265263

266264
} else {
267265
# GRANT $_privilege
268-
$custom_unless = "SELECT 1 FROM
266+
$custom_unless = "SELECT 1 WHERE NOT EXISTS
269267
( SELECT 1 FROM pg_catalog.pg_tables AS t
270268
WHERE t.schemaname = '${schema}'
271269
AND NOT EXISTS (
@@ -274,22 +272,22 @@
274272
AND g.table_schema = '${schema}'
275273
AND g.privilege_type = '${_privilege}'
276274
)
277-
) AS tbls HAVING tbls.count=0"
275+
)"
278276
}
279277
} else {
280278
if $_privilege == 'ALL' or $_privilege == 'ALL PRIVILEGES' {
281279
# REVOKE ALL
282-
$custom_unless = "SELECT 1 FROM
280+
$custom_unless = "SELECT 1 WHERE NOT EXISTS
283281
( SELECT table_name FROM information_schema.role_table_grants
284282
WHERE grantee = '${role}' AND table_schema ='${schema}'
285-
) AS tbls HAVING tbls.count=0"
283+
)"
286284
} else {
287285
# REVOKE $_privilege
288-
$custom_unless = "SELECT 1 FROM
286+
$custom_unless = "SELECT 1 WHERE NOT EXISTS
289287
( SELECT table_name FROM information_schema.role_table_grants
290288
WHERE grantee = '${role}' AND table_schema ='${schema}'
291289
AND privilege_type = '${_privilege}'
292-
) AS tbls HAVING tbls.count=0"
290+
)"
293291
}
294292
}
295293

0 commit comments

Comments
 (0)