(tableNames []string)
| 1842 | } |
| 1843 | |
| 1844 | func (d *PostgresDatabase) getPrivilegeDefsForTables(tableNames []string) (map[string][]string, error) { |
| 1845 | // If no roles are specified to include, don't query privileges at all |
| 1846 | if len(d.generatorConfig.ManagedRoles) == 0 { |
| 1847 | return map[string][]string{}, nil |
| 1848 | } |
| 1849 | |
| 1850 | const query = ` |
| 1851 | SELECT |
| 1852 | table_schema || '.' || table_name AS qualified_table_name, |
| 1853 | grantee, |
| 1854 | string_agg(privilege_type, ', ' ORDER BY privilege_type) as privileges |
| 1855 | FROM information_schema.table_privileges |
| 1856 | WHERE table_schema || '.' || table_name = ANY($1::text[]) |
| 1857 | AND grantee = ANY($2::text[]) |
| 1858 | AND grantee != ( |
| 1859 | SELECT tableowner FROM pg_tables |
| 1860 | WHERE schemaname = table_schema AND tablename = table_name |
| 1861 | ) |
| 1862 | GROUP BY table_schema, table_name, grantee |
| 1863 | ORDER BY table_schema, table_name, grantee |
| 1864 | ` |
| 1865 | |
| 1866 | rows, err := d.db.Query(query, pq.Array(tableNames), pq.Array(d.generatorConfig.ManagedRoles)) |
| 1867 | if err != nil { |
| 1868 | return nil, fmt.Errorf("failed to query privileges: %w", err) |
| 1869 | } |
| 1870 | defer rows.Close() |
| 1871 | |
| 1872 | result := make(map[string][]string, len(tableNames)) |
| 1873 | for rows.Next() { |
| 1874 | var tableName, grantee, privileges string |
| 1875 | if err := rows.Scan(&tableName, &grantee, &privileges); err != nil { |
| 1876 | return nil, fmt.Errorf("failed to scan privilege row: %w", err) |
| 1877 | } |
| 1878 | |
| 1879 | escapedGrantee := grantee |
| 1880 | if grantee != "PUBLIC" { |
| 1881 | // PUBLIC is a special keyword and should not be quoted |
| 1882 | escapedGrantee = d.quoteIdentifierIfNeeded(grantee) |
| 1883 | } |
| 1884 | |
| 1885 | grant := fmt.Sprintf("GRANT %s ON TABLE %s TO %s", privileges, tableName, escapedGrantee) |
| 1886 | result[tableName] = append(result[tableName], grant) |
| 1887 | } |
| 1888 | |
| 1889 | return result, nil |
| 1890 | } |
no test coverage detected