postgresai 0.12.0-beta.7 → 0.14.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -15
- package/bin/postgres-ai.ts +333 -0
- package/dist/bin/postgres-ai.js +307 -0
- package/dist/bin/postgres-ai.js.map +1 -1
- package/dist/lib/init.d.ts +77 -0
- package/dist/lib/init.d.ts.map +1 -0
- package/dist/lib/init.js +550 -0
- package/dist/lib/init.js.map +1 -0
- package/dist/package.json +3 -2
- package/lib/init.ts +629 -0
- package/package.json +3 -2
- package/sql/01.role.sql +16 -0
- package/sql/02.permissions.sql +33 -0
- package/sql/03.optional_rds.sql +6 -0
- package/sql/04.optional_self_managed.sql +8 -0
- package/test/init.integration.test.cjs +382 -0
- package/test/init.test.cjs +323 -0
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
const test = require("node:test");
|
|
2
|
+
const assert = require("node:assert/strict");
|
|
3
|
+
|
|
4
|
+
// These tests intentionally import the compiled JS output.
|
|
5
|
+
// Run via: npm --prefix cli test
|
|
6
|
+
const init = require("../dist/lib/init.js");
|
|
7
|
+
const DEFAULT_MONITORING_USER = init.DEFAULT_MONITORING_USER;
|
|
8
|
+
|
|
9
|
+
function runCli(args, env = {}) {
|
|
10
|
+
const { spawnSync } = require("node:child_process");
|
|
11
|
+
const path = require("node:path");
|
|
12
|
+
const node = process.execPath;
|
|
13
|
+
const cliPath = path.resolve(__dirname, "..", "dist", "bin", "postgres-ai.js");
|
|
14
|
+
return spawnSync(node, [cliPath, ...args], {
|
|
15
|
+
encoding: "utf8",
|
|
16
|
+
env: { ...process.env, ...env },
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function runPgai(args, env = {}) {
|
|
21
|
+
const { spawnSync } = require("node:child_process");
|
|
22
|
+
const path = require("node:path");
|
|
23
|
+
const node = process.execPath;
|
|
24
|
+
const pgaiPath = path.resolve(__dirname, "..", "..", "pgai", "bin", "pgai.js");
|
|
25
|
+
return spawnSync(node, [pgaiPath, ...args], {
|
|
26
|
+
encoding: "utf8",
|
|
27
|
+
env: { ...process.env, ...env },
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test("maskConnectionString hides password when present", () => {
|
|
32
|
+
const masked = init.maskConnectionString("postgresql://user:secret@localhost:5432/mydb");
|
|
33
|
+
assert.match(masked, /postgresql:\/\/user:\*{5}@localhost:5432\/mydb/);
|
|
34
|
+
assert.doesNotMatch(masked, /secret/);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("parseLibpqConninfo parses basic host/dbname/user/port/password", () => {
|
|
38
|
+
const cfg = init.parseLibpqConninfo("dbname=mydb host=localhost user=alice port=5432 password=secret");
|
|
39
|
+
assert.equal(cfg.database, "mydb");
|
|
40
|
+
assert.equal(cfg.host, "localhost");
|
|
41
|
+
assert.equal(cfg.user, "alice");
|
|
42
|
+
assert.equal(cfg.port, 5432);
|
|
43
|
+
assert.equal(cfg.password, "secret");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("parseLibpqConninfo supports quoted values", () => {
|
|
47
|
+
const cfg = init.parseLibpqConninfo("dbname='my db' host='local host'");
|
|
48
|
+
assert.equal(cfg.database, "my db");
|
|
49
|
+
assert.equal(cfg.host, "local host");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("buildInitPlan includes a race-safe role DO block", async () => {
|
|
53
|
+
const plan = await init.buildInitPlan({
|
|
54
|
+
database: "mydb",
|
|
55
|
+
monitoringUser: DEFAULT_MONITORING_USER,
|
|
56
|
+
monitoringPassword: "pw",
|
|
57
|
+
includeOptionalPermissions: false,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
assert.equal(plan.database, "mydb");
|
|
61
|
+
const roleStep = plan.steps.find((s) => s.name === "01.role");
|
|
62
|
+
assert.ok(roleStep);
|
|
63
|
+
assert.match(roleStep.sql, /do\s+\$\$/i);
|
|
64
|
+
assert.match(roleStep.sql, /create\s+user/i);
|
|
65
|
+
assert.match(roleStep.sql, /alter\s+user/i);
|
|
66
|
+
assert.ok(!plan.steps.some((s) => s.optional));
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("buildInitPlan handles special characters in monitoring user and database identifiers", async () => {
|
|
70
|
+
const monitoringUser = 'user "with" quotes ✓';
|
|
71
|
+
const database = 'db name "with" quotes ✓';
|
|
72
|
+
const plan = await init.buildInitPlan({
|
|
73
|
+
database,
|
|
74
|
+
monitoringUser,
|
|
75
|
+
monitoringPassword: "pw",
|
|
76
|
+
includeOptionalPermissions: false,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const roleStep = plan.steps.find((s) => s.name === "01.role");
|
|
80
|
+
assert.ok(roleStep);
|
|
81
|
+
// Double quotes inside identifiers must be doubled.
|
|
82
|
+
assert.match(roleStep.sql, /create\s+user\s+"user ""with"" quotes ✓"/i);
|
|
83
|
+
assert.match(roleStep.sql, /alter\s+user\s+"user ""with"" quotes ✓"/i);
|
|
84
|
+
|
|
85
|
+
const permStep = plan.steps.find((s) => s.name === "02.permissions");
|
|
86
|
+
assert.ok(permStep);
|
|
87
|
+
assert.match(permStep.sql, /grant connect on database "db name ""with"" quotes ✓" to "user ""with"" quotes ✓"/i);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("buildInitPlan keeps backslashes in passwords (no unintended escaping)", async () => {
|
|
91
|
+
const pw = String.raw`pw\with\backslash`;
|
|
92
|
+
const plan = await init.buildInitPlan({
|
|
93
|
+
database: "mydb",
|
|
94
|
+
monitoringUser: DEFAULT_MONITORING_USER,
|
|
95
|
+
monitoringPassword: pw,
|
|
96
|
+
includeOptionalPermissions: false,
|
|
97
|
+
});
|
|
98
|
+
const roleStep = plan.steps.find((s) => s.name === "01.role");
|
|
99
|
+
assert.ok(roleStep);
|
|
100
|
+
assert.ok(roleStep.sql.includes(`password '${pw}'`));
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("buildInitPlan rejects identifiers with null bytes", async () => {
|
|
104
|
+
await assert.rejects(
|
|
105
|
+
() =>
|
|
106
|
+
init.buildInitPlan({
|
|
107
|
+
database: "mydb",
|
|
108
|
+
monitoringUser: "bad\0user",
|
|
109
|
+
monitoringPassword: "pw",
|
|
110
|
+
includeOptionalPermissions: false,
|
|
111
|
+
}),
|
|
112
|
+
/Identifier cannot contain null bytes/
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("buildInitPlan rejects literals with null bytes", async () => {
|
|
117
|
+
await assert.rejects(
|
|
118
|
+
() =>
|
|
119
|
+
init.buildInitPlan({
|
|
120
|
+
database: "mydb",
|
|
121
|
+
monitoringUser: DEFAULT_MONITORING_USER,
|
|
122
|
+
monitoringPassword: "pw\0bad",
|
|
123
|
+
includeOptionalPermissions: false,
|
|
124
|
+
}),
|
|
125
|
+
/Literal cannot contain null bytes/
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("buildInitPlan inlines password safely for CREATE/ALTER ROLE grammar", async () => {
|
|
130
|
+
const plan = await init.buildInitPlan({
|
|
131
|
+
database: "mydb",
|
|
132
|
+
monitoringUser: DEFAULT_MONITORING_USER,
|
|
133
|
+
monitoringPassword: "pa'ss",
|
|
134
|
+
includeOptionalPermissions: false,
|
|
135
|
+
});
|
|
136
|
+
const step = plan.steps.find((s) => s.name === "01.role");
|
|
137
|
+
assert.ok(step);
|
|
138
|
+
assert.match(step.sql, /password 'pa''ss'/);
|
|
139
|
+
assert.equal(step.params, undefined);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("buildInitPlan includes optional steps when enabled", async () => {
|
|
143
|
+
const plan = await init.buildInitPlan({
|
|
144
|
+
database: "mydb",
|
|
145
|
+
monitoringUser: DEFAULT_MONITORING_USER,
|
|
146
|
+
monitoringPassword: "pw",
|
|
147
|
+
includeOptionalPermissions: true,
|
|
148
|
+
});
|
|
149
|
+
assert.ok(plan.steps.some((s) => s.optional));
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("resolveAdminConnection accepts positional URI", () => {
|
|
153
|
+
const r = init.resolveAdminConnection({ conn: "postgresql://u:p@h:5432/d" });
|
|
154
|
+
assert.ok(r.clientConfig.connectionString);
|
|
155
|
+
assert.doesNotMatch(r.display, /:p@/);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("resolveAdminConnection accepts positional conninfo", () => {
|
|
159
|
+
const r = init.resolveAdminConnection({ conn: "dbname=mydb host=localhost user=alice" });
|
|
160
|
+
assert.equal(r.clientConfig.database, "mydb");
|
|
161
|
+
assert.equal(r.clientConfig.host, "localhost");
|
|
162
|
+
assert.equal(r.clientConfig.user, "alice");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("resolveAdminConnection rejects invalid psql-like port", () => {
|
|
166
|
+
assert.throws(
|
|
167
|
+
() => init.resolveAdminConnection({ host: "localhost", port: "abc", username: "u", dbname: "d" }),
|
|
168
|
+
/Invalid port value/
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("resolveAdminConnection rejects when only PGPASSWORD is provided (no connection details)", () => {
|
|
173
|
+
assert.throws(() => init.resolveAdminConnection({ envPassword: "pw" }), /Connection is required/);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("resolveAdminConnection error message includes examples", () => {
|
|
177
|
+
assert.throws(() => init.resolveAdminConnection({}), /Examples:/);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("cli: init with missing connection prints init help/options", () => {
|
|
181
|
+
const r = runCli(["init"]);
|
|
182
|
+
assert.notEqual(r.status, 0);
|
|
183
|
+
// We should show options, not just the error message.
|
|
184
|
+
assert.match(r.stderr, /--print-sql/);
|
|
185
|
+
assert.match(r.stderr, /--monitoring-user/);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("resolveMonitoringPassword auto-generates a strong, URL-safe password by default", async () => {
|
|
189
|
+
const r = await init.resolveMonitoringPassword({ monitoringUser: DEFAULT_MONITORING_USER });
|
|
190
|
+
assert.equal(r.generated, true);
|
|
191
|
+
assert.ok(typeof r.password === "string" && r.password.length >= 30);
|
|
192
|
+
assert.match(r.password, /^[A-Za-z0-9_-]+$/);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("applyInitPlan preserves Postgres error fields on step failures", async () => {
|
|
196
|
+
const plan = {
|
|
197
|
+
monitoringUser: DEFAULT_MONITORING_USER,
|
|
198
|
+
database: "mydb",
|
|
199
|
+
steps: [{ name: "01.role", sql: "select 1" }],
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const pgErr = Object.assign(new Error("permission denied to create role"), {
|
|
203
|
+
code: "42501",
|
|
204
|
+
detail: "some detail",
|
|
205
|
+
hint: "some hint",
|
|
206
|
+
schema: "pg_catalog",
|
|
207
|
+
table: "pg_roles",
|
|
208
|
+
constraint: "some_constraint",
|
|
209
|
+
routine: "aclcheck_error",
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const calls = [];
|
|
213
|
+
const client = {
|
|
214
|
+
query: async (sql) => {
|
|
215
|
+
calls.push(sql);
|
|
216
|
+
if (sql === "begin;") return { rowCount: 1 };
|
|
217
|
+
if (sql === "rollback;") return { rowCount: 1 };
|
|
218
|
+
if (sql === "select 1") throw pgErr;
|
|
219
|
+
throw new Error(`unexpected sql: ${sql}`);
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
await assert.rejects(
|
|
224
|
+
() => init.applyInitPlan({ client, plan }),
|
|
225
|
+
(e) => {
|
|
226
|
+
assert.ok(e instanceof Error);
|
|
227
|
+
assert.match(e.message, /Failed at step "01\.role":/);
|
|
228
|
+
assert.equal(e.code, "42501");
|
|
229
|
+
assert.equal(e.detail, "some detail");
|
|
230
|
+
assert.equal(e.hint, "some hint");
|
|
231
|
+
assert.equal(e.schema, "pg_catalog");
|
|
232
|
+
assert.equal(e.table, "pg_roles");
|
|
233
|
+
assert.equal(e.constraint, "some_constraint");
|
|
234
|
+
assert.equal(e.routine, "aclcheck_error");
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
assert.deepEqual(calls, ["begin;", "select 1", "rollback;"]);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("verifyInitSetup runs inside a repeatable read snapshot and rolls back", async () => {
|
|
243
|
+
const calls = [];
|
|
244
|
+
const client = {
|
|
245
|
+
query: async (sql, params) => {
|
|
246
|
+
calls.push(String(sql));
|
|
247
|
+
|
|
248
|
+
if (String(sql).toLowerCase().startsWith("begin isolation level repeatable read")) {
|
|
249
|
+
return { rowCount: 1, rows: [] };
|
|
250
|
+
}
|
|
251
|
+
if (String(sql).toLowerCase() === "rollback;") {
|
|
252
|
+
return { rowCount: 1, rows: [] };
|
|
253
|
+
}
|
|
254
|
+
if (String(sql).includes("select rolconfig")) {
|
|
255
|
+
return { rowCount: 1, rows: [{ rolconfig: ['search_path="$user", public, pg_catalog'] }] };
|
|
256
|
+
}
|
|
257
|
+
if (String(sql).includes("from pg_catalog.pg_roles")) {
|
|
258
|
+
return { rowCount: 1, rows: [] };
|
|
259
|
+
}
|
|
260
|
+
if (String(sql).includes("has_database_privilege")) {
|
|
261
|
+
return { rowCount: 1, rows: [{ ok: true }] };
|
|
262
|
+
}
|
|
263
|
+
if (String(sql).includes("pg_has_role")) {
|
|
264
|
+
return { rowCount: 1, rows: [{ ok: true }] };
|
|
265
|
+
}
|
|
266
|
+
if (String(sql).includes("has_table_privilege") && String(sql).includes("pg_catalog.pg_index")) {
|
|
267
|
+
return { rowCount: 1, rows: [{ ok: true }] };
|
|
268
|
+
}
|
|
269
|
+
if (String(sql).includes("to_regclass('public.pg_statistic')")) {
|
|
270
|
+
return { rowCount: 1, rows: [{ ok: true }] };
|
|
271
|
+
}
|
|
272
|
+
if (String(sql).includes("has_table_privilege") && String(sql).includes("public.pg_statistic")) {
|
|
273
|
+
return { rowCount: 1, rows: [{ ok: true }] };
|
|
274
|
+
}
|
|
275
|
+
if (String(sql).includes("has_schema_privilege")) {
|
|
276
|
+
return { rowCount: 1, rows: [{ ok: true }] };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
throw new Error(`unexpected sql: ${sql} params=${JSON.stringify(params)}`);
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const r = await init.verifyInitSetup({
|
|
284
|
+
client,
|
|
285
|
+
database: "mydb",
|
|
286
|
+
monitoringUser: DEFAULT_MONITORING_USER,
|
|
287
|
+
includeOptionalPermissions: false,
|
|
288
|
+
});
|
|
289
|
+
assert.equal(r.ok, true);
|
|
290
|
+
assert.equal(r.missingRequired.length, 0);
|
|
291
|
+
|
|
292
|
+
assert.ok(calls.length > 2);
|
|
293
|
+
assert.match(calls[0].toLowerCase(), /^begin isolation level repeatable read/);
|
|
294
|
+
assert.equal(calls[calls.length - 1].toLowerCase(), "rollback;");
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test("redactPasswordsInSql redacts password literals with embedded quotes", async () => {
|
|
298
|
+
const plan = await init.buildInitPlan({
|
|
299
|
+
database: "mydb",
|
|
300
|
+
monitoringUser: DEFAULT_MONITORING_USER,
|
|
301
|
+
monitoringPassword: "pa'ss",
|
|
302
|
+
includeOptionalPermissions: false,
|
|
303
|
+
});
|
|
304
|
+
const step = plan.steps.find((s) => s.name === "01.role");
|
|
305
|
+
assert.ok(step);
|
|
306
|
+
const redacted = init.redactPasswordsInSql(step.sql);
|
|
307
|
+
assert.match(redacted, /password '<redacted>'/i);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test("cli: init --print-sql works without connection (offline mode)", () => {
|
|
311
|
+
const r = runCli(["init", "--print-sql", "-d", "mydb", "--password", "monpw"]);
|
|
312
|
+
assert.equal(r.status, 0, r.stderr || r.stdout);
|
|
313
|
+
assert.match(r.stdout, /SQL plan \(offline; not connected\)/);
|
|
314
|
+
assert.match(r.stdout, new RegExp(`grant connect on database "mydb" to "${DEFAULT_MONITORING_USER}"`, "i"));
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("pgai wrapper forwards to postgresai CLI", () => {
|
|
318
|
+
const r = runPgai(["--help"]);
|
|
319
|
+
assert.equal(r.status, 0, r.stderr || r.stdout);
|
|
320
|
+
assert.match(r.stdout, /postgresai|PostgresAI/i);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
|