Module: PgRls::Database::Prepared

Defined in:
lib/pg_rls/database/prepared.rb

Overview

Prepare database for test unit

Class Method Summary collapse

Class Method Details

.grant_user_credentials(name: PgRls.username, schema: 'public') ⇒ Object



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
# File 'lib/pg_rls/database/prepared.rb', line 8

def grant_user_credentials(name: PgRls.username, schema: 'public')
  PgRls.admin_execute <<-SQL.squish
    DO
    $do$
    BEGIN
      IF NOT EXISTS (
        SELECT table_catalog, table_schema, table_name, privilege_type
          FROM   information_schema.table_privileges
          WHERE  grantee = '#{name}'
      ) THEN
          GRANT ALL PRIVILEGES ON TABLE schema_migrations TO #{name};
          GRANT USAGE ON SCHEMA #{schema} TO #{name};
          ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema}
            GRANT USAGE, SELECT
            ON SEQUENCES TO #{name};
          ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema}
            GRANT SELECT, INSERT, UPDATE, DELETE
            ON TABLES TO #{name};
          GRANT SELECT, INSERT, UPDATE, DELETE
            ON ALL TABLES IN SCHEMA #{schema}
            TO #{name};
          GRANT USAGE, SELECT
            ON ALL SEQUENCES IN SCHEMA #{schema}
            TO #{name};
        END IF;
    END;
    $do$;
  SQL
end