-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalog.ts
More file actions
193 lines (178 loc) · 5.89 KB
/
Copy pathcatalog.ts
File metadata and controls
193 lines (178 loc) · 5.89 KB
1
2
3
4
5
6
7
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { type PgdevConfig } from "../config.ts";
import { runPsqlQuery } from "../commands/exec.ts";
export interface CatalogRoutine {
schema: string;
name: string;
type: "function" | "procedure";
parameters: { name: string | null; type: string }[];
returns: { setof: boolean; type: string } | null;
body: string | null;
language: string;
volatility: "immutable" | "stable" | "volatile";
strict: boolean;
securityDefiner: boolean;
parallel: "safe" | "restricted" | "unsafe";
leakproof: boolean;
cost: number;
rows: number;
config: string[];
comment: string | null;
}
/** Raw row shape returned by catalogMetadataQuery(). */
export interface CatalogRow {
schema: string;
name: string;
type: string;
routine_oid: number;
param_ord: number | null;
param_name: string | null;
param_type: string | null;
return_type: string | null;
return_setof: boolean;
body: string | null;
language: string;
volatility: string;
strict: boolean;
security_definer: boolean;
parallel: string;
leakproof: boolean;
cost: number;
rows: number;
config: string[] | null;
comment: string | null;
}
const VOLATILITY_MAP: Record<string, "immutable" | "stable" | "volatile"> = {
i: "immutable",
s: "stable",
v: "volatile",
};
const PARALLEL_MAP: Record<string, "safe" | "restricted" | "unsafe"> = {
s: "safe",
r: "restricted",
u: "unsafe",
};
/** Build SQL that returns routine metadata from pg_catalog, filtered by schemas. */
export function catalogMetadataQuery(schemas: string[]): string {
const list = schemas.map((s) => `'${s}'`).join(", ");
return [
"SELECT n.nspname AS schema, p.proname AS name,",
" CASE p.prokind WHEN 'f' THEN 'function' WHEN 'p' THEN 'procedure' END AS type,",
" p.oid::int AS routine_oid,",
" a.ord::int AS param_ord,",
" CASE WHEN p.proargnames IS NOT NULL THEN nullif(p.proargnames[a.ord::int], '') ELSE NULL END AS param_name,",
" format_type(a.type_oid, NULL) AS param_type,",
" format_type(p.prorettype, NULL) AS return_type,",
" p.proretset AS return_setof,",
" p.prosrc AS body,",
" l.lanname AS language,",
" p.provolatile AS volatility,",
" p.proisstrict AS strict,",
" p.prosecdef AS security_definer,",
" p.proparallel AS parallel,",
" p.proleakproof AS leakproof,",
" p.procost::real AS cost,",
" p.prorows::real AS rows,",
" p.proconfig AS config,",
" obj_description(p.oid, 'pg_proc') AS comment",
"FROM pg_proc p",
"JOIN pg_namespace n ON n.oid = p.pronamespace",
"JOIN pg_language l ON l.oid = p.prolang",
"LEFT JOIN LATERAL unnest(p.proargtypes) WITH ORDINALITY AS a(type_oid, ord) ON true",
`WHERE n.nspname IN (${list})`,
"ORDER BY n.nspname, p.proname, p.oid, a.ord",
].join("\n");
}
/** Group flat rows into CatalogRoutine[]. Works with both Bun SQL and parsed psql output. */
export function parseCatalogRows(rows: CatalogRow[]): CatalogRoutine[] {
const map = new Map<number, CatalogRoutine>();
for (const row of rows) {
let routine = map.get(row.routine_oid);
if (!routine) {
routine = {
schema: row.schema,
name: row.name,
type: row.type as "function" | "procedure",
parameters: [],
returns: row.type === "procedure" ? null : { setof: row.return_setof, type: row.return_type! },
body: row.body,
language: row.language,
volatility: VOLATILITY_MAP[row.volatility] ?? "volatile",
strict: row.strict,
securityDefiner: row.security_definer,
parallel: PARALLEL_MAP[row.parallel] ?? "unsafe",
leakproof: row.leakproof,
cost: row.cost,
rows: row.rows,
config: row.config ?? [],
comment: row.comment ?? null,
};
map.set(row.routine_oid, routine);
}
if (row.param_ord != null) {
routine.parameters.push({
name: row.param_name,
type: row.param_type!,
});
}
}
return [...map.values()];
}
/** Parse psql {key=val,...} array format to string[]. */
function parsePgArray(raw: string): string[] {
if (!raw || raw === "{}") return [];
return raw.replace(/^\{/, "").replace(/\}$/, "").split(",");
}
/** Normalize body for comparison: lowercase, strip non-printable, collapse whitespace. */
export function normalizeBody(body: string): string {
return body
.toLowerCase()
.replace(/[^\x20-\x7e]/g, " ")
.replace(/\s+/g, " ")
.trim();
}
/** Hash a normalized body string (SHA-256, hex). */
export function bodyHash(body: string): string {
const hasher = new Bun.CryptoHasher("sha256");
hasher.update(normalizeBody(body));
return hasher.digest("hex");
}
/** Production execution via psql. */
export async function fetchCatalogMetadata(config: PgdevConfig): Promise<CatalogRoutine[]> {
const schemas = config.project.schemas;
if (schemas.length === 0) return [];
const sql = catalogMetadataQuery(schemas);
const result = await runPsqlQuery(config, sql);
if (!result.ok) {
throw new Error(`Catalog query failed: ${result.error}`);
}
const rows: CatalogRow[] = result.rows.map((line) => {
const [
schema, name, type, oid, ord, paramName, paramType,
returnType, returnSetof, body, language, volatility,
strict, securityDefiner, parallel, leakproof, cost, rows, config, comment,
] = line.split("|");
return {
schema,
name,
type,
routine_oid: Number(oid),
param_ord: ord ? Number(ord) : null,
param_name: paramName || null,
param_type: paramType || null,
return_type: returnType || null,
return_setof: returnSetof === "t",
body: body || null,
language,
volatility,
strict: strict === "t",
security_definer: securityDefiner === "t",
parallel,
leakproof: leakproof === "t",
cost: Number(cost),
rows: Number(rows),
config: parsePgArray(config),
comment: comment || null,
};
});
return parseCatalogRows(rows);
}