|
| 1 | +-- migrate:up |
| 2 | + |
| 3 | +-- Create or normalize query role (nologin, used for grouping read permissions) |
| 4 | +DO $do$ BEGIN IF EXISTS ( |
| 5 | + SELECT |
| 6 | + FROM pg_catalog.pg_roles |
| 7 | + WHERE rolname = 'query' |
| 8 | +) THEN |
| 9 | + ALTER ROLE query WITH NOLOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS; |
| 10 | +ELSE |
| 11 | + create role query nologin nosuperuser nocreatedb nocreaterole noreplication nobypassrls; |
| 12 | +END IF; |
| 13 | +END $do$; |
| 14 | + |
| 15 | +-- Create or normalize reader role (login, inherits query permissions) |
| 16 | +DO $do$ BEGIN IF EXISTS ( |
| 17 | + SELECT |
| 18 | + FROM pg_catalog.pg_roles |
| 19 | + WHERE rolname = 'reader' |
| 20 | +) THEN |
| 21 | + ALTER ROLE reader WITH INHERIT LOGIN PASSWORD '3eadooor' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS; |
| 22 | +ELSE |
| 23 | + create role reader inherit login password '3eadooor' nosuperuser nocreatedb nocreaterole noreplication nobypassrls; |
| 24 | +END IF; |
| 25 | +END $do$; |
| 26 | + |
| 27 | +-- Strip any broader privileges from previous manual grants before granting read-only access. |
| 28 | +REVOKE ALL PRIVILEGES ON DATABASE markdb FROM query; |
| 29 | +REVOKE ALL PRIVILEGES ON DATABASE markdb FROM reader; |
| 30 | +REVOKE ALL PRIVILEGES ON SCHEMA public FROM query; |
| 31 | +REVOKE ALL PRIVILEGES ON SCHEMA public FROM reader; |
| 32 | +REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM query; |
| 33 | +REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM reader; |
| 34 | + |
| 35 | +GRANT CONNECT ON DATABASE markdb TO query; |
| 36 | +grant usage on schema public to query; |
| 37 | +grant select on public.admin_actions to query; |
| 38 | +grant select on public.cex_withdrawals to query; |
| 39 | +grant select on public.earmarks to query; |
| 40 | +grant select on public.rebalance_operations to query; |
| 41 | +grant select on public.schema_migrations to query; |
| 42 | +grant select on public.transactions to query; |
| 43 | + |
| 44 | +grant query to reader; |
| 45 | + |
| 46 | +-- migrate:down |
| 47 | + |
| 48 | +-- Revoke from reader |
| 49 | +REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM reader; |
| 50 | +REVOKE ALL PRIVILEGES ON SCHEMA public FROM reader; |
| 51 | +REVOKE ALL PRIVILEGES ON DATABASE markdb FROM reader; |
| 52 | +REVOKE query FROM reader; |
| 53 | + |
| 54 | +-- Revoke from query |
| 55 | +REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM query; |
| 56 | +REVOKE ALL PRIVILEGES ON SCHEMA public FROM query; |
| 57 | +REVOKE ALL PRIVILEGES ON DATABASE markdb FROM query; |
| 58 | + |
| 59 | +DROP ROLE IF EXISTS reader; |
| 60 | +DROP ROLE IF EXISTS query; |
| 61 | + |
0 commit comments