# LogicTest: local local-opt fakedist fakedist-opt fakedist-metadata

# Test default database-level permissions.
# Default user is root.
statement ok
CREATE DATABASE a

statement ok
DROP DATABASE a

statement ok
CREATE DATABASE a

statement ok
SHOW DATABASES

statement ok
SET DATABASE = a

statement ok
CREATE TABLE t (id INT PRIMARY KEY)

statement ok
SHOW TABLES

statement ok
SHOW GRANTS ON DATABASE a

statement ok
CREATE USER bar

statement ok
GRANT ALL ON DATABASE a TO bar

statement ok
REVOKE ALL ON DATABASE a FROM bar

# Switch to a user without any privileges.
user testuser

statement error only superusers are allowed to CREATE DATABASE
CREATE DATABASE b

statement error user testuser does not have DROP privilege on database a
DROP DATABASE a CASCADE

statement ok
SHOW DATABASES

statement ok
SET DATABASE = a

statement error user testuser does not have CREATE privilege on database a
CREATE TABLE t2 (id INT PRIMARY KEY)

statement ok
SHOW TABLES

statement ok
SHOW GRANTS ON DATABASE a

statement error user testuser does not have GRANT privilege on database a
GRANT ALL ON DATABASE a TO bar

statement error user testuser does not have GRANT privilege on database a
REVOKE ALL ON DATABASE a FROM bar

# Grant read-only privileges.
user root

statement ok
GRANT SELECT ON DATABASE a TO testuser

user testuser

statement error only superusers are allowed to CREATE DATABASE
CREATE DATABASE b

statement error user testuser does not have DROP privilege on database a
DROP DATABASE a CASCADE

statement ok
SHOW DATABASES

statement ok
SET DATABASE = a

statement error user testuser does not have CREATE privilege on database a
CREATE TABLE t2 (id INT PRIMARY KEY)

statement ok
SHOW TABLES

statement ok
SHOW GRANTS ON DATABASE a

statement error user testuser does not have GRANT privilege on database a
GRANT ALL ON DATABASE a TO bar

statement error user testuser does not have GRANT privilege on database a
REVOKE ALL ON DATABASE a FROM bar

# Grant all privileges.
user root

statement ok
GRANT ALL ON DATABASE a TO testuser

user testuser

statement error only superusers are allowed to CREATE DATABASE
CREATE DATABASE b

statement ok
SHOW DATABASES

statement ok
SET DATABASE = a

statement ok
CREATE TABLE t2 (id INT PRIMARY KEY)

statement ok
SHOW TABLES

statement ok
SHOW GRANTS ON DATABASE a

statement ok
GRANT ALL ON DATABASE a TO bar

statement ok
REVOKE ALL ON DATABASE a FROM bar

statement error user testuser does not have DROP privilege on relation t
DROP DATABASE a CASCADE

user root

statement ok
GRANT DROP ON TABLE a.t TO testuser

user testuser

statement ok
DROP DATABASE a CASCADE
