Skip to content

Commit 9988389

Browse files
authored
Merge pull request #26 from supabase/refactor/sql-cleanup
refactor: SQL cleanup
2 parents b7ca2a8 + 007becd commit 9988389

File tree

8 files changed

+52
-14
lines changed

8 files changed

+52
-14
lines changed

src/api/roles.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import sql = require('../lib/sql')
44
const { grants, roles } = sql
55
import { coalesceRowsToArray } from '../lib/helpers'
66
import { RunQuery } from '../lib/connectionPool'
7-
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
7+
import { DEFAULT_ROLES, DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
88
import { Roles } from '../lib/interfaces'
99

1010
/**
1111
* @param {boolean} [includeSystemSchemas=false] - Return system schemas as well as user schemas
1212
*/
1313
interface GetRolesQueryParams {
14+
includeDefaultRoles?: boolean
1415
includeSystemSchemas?: boolean
1516
}
1617

@@ -29,6 +30,7 @@ FROM
2930
const query: GetRolesQueryParams = req.query
3031
let payload: Roles.Role[] = data
3132
if (!query?.includeSystemSchemas) payload = removeSystemSchemas(data)
33+
if (!query?.includeDefaultRoles) payload = removeDefaultRoles(payload)
3234

3335
return res.status(200).json(payload)
3436
} catch (error) {
@@ -104,4 +106,8 @@ const removeSystemSchemas = (data: Roles.Role[]) => {
104106
})
105107
}
106108

109+
const removeDefaultRoles = (data: Roles.Role[]) => {
110+
return data.filter((role) => !DEFAULT_ROLES.includes(role.name))
111+
}
112+
107113
export = router

src/api/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Router } from 'express'
33
import sql = require('../lib/sql')
44
const { schemas } = sql
55
import { RunQuery } from '../lib/connectionPool'
6-
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
6+
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
77
import { Schemas } from '../lib/interfaces'
88

99
/**

src/api/tables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sql = require('../lib/sql')
44
const { columns, grants, primary_keys, relationships, tables } = sql
55
import { coalesceRowsToArray, formatColumns } from '../lib/helpers'
66
import { RunQuery } from '../lib/connectionPool'
7-
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
7+
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
88
import { Tables } from '../lib/interfaces'
99

1010
const router = Router()

src/api/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Router } from 'express'
33
import sql = require('../lib/sql')
44
const { types } = sql
55
import { RunQuery } from '../lib/connectionPool'
6-
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
6+
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
77
import { Types } from '../lib/interfaces'
88

99
const router = Router()

src/lib/constants.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,22 @@ export const PG_API_URL = process.env.PG_API_URL || 'http://localhost'
22
export const PG_API_PORT = Number(process.env.PG_API_PORT || 1337)
33
export const CRYPTO_KEY = process.env.CRYPTO_KEY || 'SAMPLE_KEY'
44
export const PG_CONNECTION = 'postgres://postgres:postgres@localhost:5432/postgres'
5+
6+
export const DEFAULT_ROLES: string[] = [
7+
'pg_read_all_settings',
8+
'pg_read_all_stats',
9+
'pg_stat_scan_tables',
10+
'pg_monitor',
11+
'pg_signal_backend',
12+
'pg_read_server_files',
13+
'pg_write_server_files',
14+
'pg_execute_server_program',
15+
]
16+
17+
export const DEFAULT_SYSTEM_SCHEMAS: string[] = [
18+
'information_schema',
19+
'pg_catalog',
20+
'pg_toast_temp_1',
21+
'pg_temp_1',
22+
'pg_toast',
23+
]

src/lib/constants/schemas.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/lib/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export namespace Roles {
1212
password: string
1313
valid_until: string
1414
config: string
15-
oid: number
15+
id: number
1616

1717
grants: Grant[]
1818
}

src/lib/sql/roles.sql

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,30 @@ SELECT
77
rolcanlogin AS can_login,
88
rolreplication AS is_replication_role,
99
rolbypassrls AS can_bypass_rls,
10-
rolconnlimit AS connection_limit,
10+
active_connections,
11+
CASE WHEN rolconnlimit = -1 THEN max_db_connections :: int4
12+
ELSE rolconnlimit
13+
END AS connection_limit,
1114
rolpassword AS password,
1215
rolvaliduntil AS valid_until,
1316
rolconfig AS config,
14-
oid
17+
oid AS id
1518
FROM
1619
pg_catalog.pg_roles
20+
INNER JOIN LATERAL (
21+
SELECT
22+
count(*) AS active_connections
23+
FROM
24+
pg_stat_activity
25+
WHERE
26+
state = 'active'
27+
AND pg_roles.rolname = pg_stat_activity.usename
28+
) AS active_connections ON 1 = 1
29+
INNER JOIN LATERAL (
30+
SELECT
31+
setting AS max_db_connections
32+
FROM
33+
pg_settings
34+
WHERE
35+
name = 'max_connections'
36+
) AS max_db_connections ON 1 = 1

0 commit comments

Comments
 (0)