-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathdrop_owned_by
318 lines (228 loc) · 5.95 KB
/
drop_owned_by
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# LogicTest: !local-legacy-schema-changer
# Test dropping nothing.
statement ok
DROP OWNED BY testuser
statement ok
CREATE USER testuser2
# DROP-OBJECTS: Test that DROP OWNED BY drops objects owned by the specified
# roles.
#
# In this test, testuser creates multiple objects and drops all of them in one
# go. Additionally, testuser2 owns a table that shouldn't be dropped by
# testuser's DROP OWNED BY.
subtest drop-objects
user testuser2
statement ok
CREATE TABLE u()
user root
statement ok
GRANT CREATE ON DATABASE test TO testuser WITH GRANT OPTION
user testuser
statement ok
CREATE TABLE t(a INT)
statement ok
CREATE VIEW v AS SELECT 1
statement ok
CREATE SEQUENCE seq
statement ok
CREATE TYPE enum AS ENUM('a', 'b')
query TTTTIT
SHOW TABLES FROM public
----
public seq sequence testuser 0 NULL
public t table testuser 0 NULL
public u table testuser2 0 NULL
public v view testuser 0 NULL
query TTTT
SHOW ENUMS
----
public enum {a,b} testuser
statement ok
DROP OWNED BY testuser
query TTTTIT
SHOW TABLES FROM public
----
public u table testuser2 0 NULL
query error pgcode 42P01 relation "t" does not exist
SELECT * FROM t
query error pgcode 42P01 relation "v" does not exist
SELECT * FROM v
query TTTT
SHOW ENUMS
----
user testuser2
statement ok
DROP OWNED BY testuser2
query TTTTIT
SHOW TABLES FROM public
----
# DROP-BEHAVIOR: Test RESTRICT/CASCADE.
#
# In this test, testuser2 creates a view dependent on a table owned by
# testuser. Under RESTRICT, testuser cannot DROP OWNED BY due to this
# dependency. Under CASCADE, testuser can DROP OWNED BY, which drops both
# testuser's table and testuser2's view.
subtest drop-behavior
user testuser
statement ok
CREATE TABLE t(a INT)
statement ok
GRANT SELECT ON t TO testuser2 WITH GRANT OPTION
user testuser2
statement ok
CREATE VIEW v AS SELECT a FROM t
user testuser
statement error pq: cannot drop desired object\(s\) because other objects depend on them
DROP OWNED BY testuser
statement error pq: cannot drop desired object\(s\) because other objects depend on them
DROP OWNED BY testuser RESTRICT
query TTTTIT
SHOW TABLES FROM public
----
public t table testuser 0 NULL
public v view testuser2 0 NULL
user testuser2
# TODO(jasonmchan): Replace the two following DROP OWNED BY statements with a
# single DROP OWNED BY testuser2 CASCADE statement.
statement ok
DROP OWNED BY testuser2
user testuser
statement ok
DROP OWNED BY testuser
query TTTTIT
SHOW TABLES FROM public
----
# DROP-SCHEMA: Test that schemas and the objects that they contain can all be
# dropped together by a single DROP OWNED BY (when they are all owned by the
# specified roles).
subtest drop-schema
user root
statement ok
GRANT ALL ON DATABASE test TO testuser WITH GRANT OPTION
user testuser
statement ok
CREATE SCHEMA s
statement ok
CREATE TABLE s.t1()
statement ok
CREATE TABLE s.t2()
statement ok
DROP OWNED BY testuser
statement error pq: target database or schema does not exist
SHOW TABLES FROM s
user root
# REVOKE-PRIVILEGES-DB: Test that DROP OWNED BY revokes privileges on the
# current database.
#
# The DROP OWNED BY from the previous subtest did not revoke testuser's
# privileges for the DATABASE. This is because a user should not revoke its own
# database privileges. However, the root user should be able to drop testuser's
# database privileges via DROP OWNED BY.
subtest revoke-privileges-db
query TTTB
SHOW GRANTS ON DATABASE test
----
test admin ALL true
test public CONNECT false
test root ALL true
test testuser ALL true
user root
statement ok
DROP OWNED BY testuser
query TTTB
SHOW GRANTS ON DATABASE test
----
test admin ALL true
test public CONNECT false
test root ALL true
# REVOKE-PRIVILEGES-SCHEMA: Test that DROP OWNED BY revokes privileges on
# schemas in the current database.
#
# In this test, root creates a schema and grants privileges for the schema to
# testuser. When testuser issues a DROP OWNED BY, those privileges should be
# revoked.
subtest revoke-privileges-schema
user root
statement ok
CREATE SCHEMA s
statement ok
GRANT CREATE ON SCHEMA s TO testuser WITH GRANT OPTION
user testuser
statement ok
CREATE TABLE s.t()
statement ok
DROP OWNED BY testuser
query TTTTB
SHOW GRANTS ON SCHEMA s
----
test s admin ALL true
test s root ALL true
query TTTTIT
SHOW TABLES FROM s
----
user root
statement ok
DROP SCHEMA s
# REVOKE-PRIVILEGES-TABLE: Test that DROP OWNED BY revokes privileges on
# objects in the database.
subtest revoke-privileges-table
user root
statement ok
CREATE TABLE t()
statement ok
GRANT ALL ON t TO testuser WITH GRANT OPTION
user testuser
query TTTTTB
SHOW GRANTS ON t
----
test public t admin ALL true
test public t root ALL true
test public t testuser ALL true
statement ok
DROP OWNED BY testuser
query TTTTTB
SHOW GRANTS ON t
----
test public t admin ALL true
test public t root ALL true
user root
statement ok
DROP TABLE t
# MUTIROLE: Test DROP OWNED BY with multiple roles.
subtest multirole
statement ok
CREATE ROLE r1
statement ok
CREATE ROLE r2
statement ok
SET ROLE r1
statement ok
CREATE TABLE t1()
statement ok
SET ROLE r2
statement ok
CREATE TABLE t2()
statement ok
SET ROLE root
query TTTTIT
SHOW TABLES FROM public
----
public t1 table r1 0 NULL
public t2 table r2 0 NULL
statement ok
DROP OWNED BY r1, r2
query TTTTIT
SHOW TABLES FROM public
----
# ROLES: Test that the current user is a member of all the specified roles. The
# admin role and the root user are off-limits.
subtest roles
user testuser
statement error pq: permission denied to drop objects
DROP OWNED BY testuser2
statement error pq: permission denied to drop objects
DROP OWNED BY testuser, testuser2
statement error pq: cannot drop objects owned by role "root" because they are required by the database system
DROP OWNED BY root
statement error pq: cannot drop objects owned by role "admin" because they are required by the database system
DROP OWNED BY admin