postgresai 0.12.0-beta.6 → 0.14.0-dev.7
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 +91 -2
- package/bin/postgres-ai.ts +552 -37
- package/dist/bin/postgres-ai.js +503 -26
- package/dist/bin/postgres-ai.js.map +1 -1
- package/dist/lib/init.d.ts +61 -0
- package/dist/lib/init.d.ts.map +1 -0
- package/dist/lib/init.js +359 -0
- package/dist/lib/init.js.map +1 -0
- package/dist/lib/issues.d.ts +69 -1
- package/dist/lib/issues.d.ts.map +1 -1
- package/dist/lib/issues.js +232 -1
- package/dist/lib/issues.js.map +1 -1
- package/dist/lib/mcp-server.d.ts.map +1 -1
- package/dist/lib/mcp-server.js +69 -15
- package/dist/lib/mcp-server.js.map +1 -1
- package/dist/package.json +3 -2
- package/lib/init.ts +404 -0
- package/lib/issues.ts +325 -3
- package/lib/mcp-server.ts +75 -17
- package/package.json +3 -2
- package/test/init.integration.test.cjs +269 -0
- package/test/init.test.cjs +69 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
const test = require("node:test");
|
|
2
|
+
const assert = require("node:assert/strict");
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const os = require("node:os");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const net = require("node:net");
|
|
7
|
+
const { spawn, spawnSync } = require("node:child_process");
|
|
8
|
+
|
|
9
|
+
function findOnPath(cmd) {
|
|
10
|
+
const which = spawnSync("sh", ["-lc", `command -v ${cmd}`], { encoding: "utf8" });
|
|
11
|
+
if (which.status === 0) return String(which.stdout || "").trim();
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function findPgBin(cmd) {
|
|
16
|
+
const p = findOnPath(cmd);
|
|
17
|
+
if (p) return p;
|
|
18
|
+
|
|
19
|
+
// Debian/Ubuntu (GitLab CI node:*-bullseye images): binaries usually live here.
|
|
20
|
+
// We avoid filesystem globbing in JS and just ask the shell.
|
|
21
|
+
const probe = spawnSync(
|
|
22
|
+
"sh",
|
|
23
|
+
[
|
|
24
|
+
"-lc",
|
|
25
|
+
`ls -1 /usr/lib/postgresql/*/bin/${cmd} 2>/dev/null | head -n 1 || true`,
|
|
26
|
+
],
|
|
27
|
+
{ encoding: "utf8" }
|
|
28
|
+
);
|
|
29
|
+
const out = String(probe.stdout || "").trim();
|
|
30
|
+
if (out) return out;
|
|
31
|
+
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function havePostgresBinaries() {
|
|
36
|
+
return !!(findPgBin("initdb") && findPgBin("postgres"));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function getFreePort() {
|
|
40
|
+
return await new Promise((resolve, reject) => {
|
|
41
|
+
const srv = net.createServer();
|
|
42
|
+
srv.listen(0, "127.0.0.1", () => {
|
|
43
|
+
const addr = srv.address();
|
|
44
|
+
srv.close((err) => {
|
|
45
|
+
if (err) return reject(err);
|
|
46
|
+
resolve(addr.port);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
srv.on("error", reject);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function waitFor(fn, { timeoutMs = 10000, intervalMs = 100 } = {}) {
|
|
54
|
+
const start = Date.now();
|
|
55
|
+
// eslint-disable-next-line no-constant-condition
|
|
56
|
+
while (true) {
|
|
57
|
+
try {
|
|
58
|
+
return await fn();
|
|
59
|
+
} catch (e) {
|
|
60
|
+
if (Date.now() - start > timeoutMs) throw e;
|
|
61
|
+
await new Promise((r) => setTimeout(r, intervalMs));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function withTempPostgres(t) {
|
|
67
|
+
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "postgresai-init-"));
|
|
68
|
+
const dataDir = path.join(tmpRoot, "data");
|
|
69
|
+
const socketDir = path.join(tmpRoot, "sock");
|
|
70
|
+
fs.mkdirSync(socketDir, { recursive: true });
|
|
71
|
+
|
|
72
|
+
const initdb = findPgBin("initdb");
|
|
73
|
+
const postgresBin = findPgBin("postgres");
|
|
74
|
+
assert.ok(initdb && postgresBin, "PostgreSQL binaries not found (need initdb and postgres)");
|
|
75
|
+
|
|
76
|
+
const init = spawnSync(initdb, ["-D", dataDir, "-U", "postgres", "-A", "trust"], {
|
|
77
|
+
encoding: "utf8",
|
|
78
|
+
});
|
|
79
|
+
assert.equal(init.status, 0, init.stderr || init.stdout);
|
|
80
|
+
|
|
81
|
+
// Configure: local socket trust, TCP scram.
|
|
82
|
+
const hbaPath = path.join(dataDir, "pg_hba.conf");
|
|
83
|
+
fs.appendFileSync(
|
|
84
|
+
hbaPath,
|
|
85
|
+
"\n# Added by postgresai init integration tests\nlocal all all trust\nhost all all 127.0.0.1/32 scram-sha-256\nhost all all ::1/128 scram-sha-256\n",
|
|
86
|
+
"utf8"
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const port = await getFreePort();
|
|
90
|
+
|
|
91
|
+
const postgresProc = spawn(postgresBin, ["-D", dataDir, "-k", socketDir, "-h", "127.0.0.1", "-p", String(port)], {
|
|
92
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const { Client } = require("pg");
|
|
96
|
+
|
|
97
|
+
const connectLocal = async (database = "postgres") => {
|
|
98
|
+
// IMPORTANT: must match the port Postgres is started with; otherwise pg defaults to 5432 and the socket path won't exist.
|
|
99
|
+
const c = new Client({ host: socketDir, port, user: "postgres", database });
|
|
100
|
+
await c.connect();
|
|
101
|
+
return c;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
await waitFor(async () => {
|
|
105
|
+
const c = await connectLocal();
|
|
106
|
+
await c.end();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const postgresPassword = "postgrespw";
|
|
110
|
+
{
|
|
111
|
+
const c = await connectLocal();
|
|
112
|
+
await c.query("alter user postgres password $1", [postgresPassword]);
|
|
113
|
+
await c.query("create database testdb");
|
|
114
|
+
await c.end();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
t.after(async () => {
|
|
118
|
+
postgresProc.kill("SIGTERM");
|
|
119
|
+
try {
|
|
120
|
+
await waitFor(
|
|
121
|
+
async () => {
|
|
122
|
+
if (postgresProc.exitCode === null) throw new Error("still running");
|
|
123
|
+
},
|
|
124
|
+
{ timeoutMs: 5000, intervalMs: 100 }
|
|
125
|
+
);
|
|
126
|
+
} catch {
|
|
127
|
+
postgresProc.kill("SIGKILL");
|
|
128
|
+
}
|
|
129
|
+
fs.rmSync(tmpRoot, { recursive: true, force: true });
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const adminUri = `postgresql://postgres:${postgresPassword}@127.0.0.1:${port}/testdb`;
|
|
133
|
+
return { port, socketDir, adminUri, postgresPassword };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function runCliInit(args, env = {}) {
|
|
137
|
+
const node = process.execPath;
|
|
138
|
+
const cliPath = path.resolve(__dirname, "..", "dist", "bin", "postgres-ai.js");
|
|
139
|
+
const res = spawnSync(node, [cliPath, "init", ...args], {
|
|
140
|
+
encoding: "utf8",
|
|
141
|
+
env: { ...process.env, ...env },
|
|
142
|
+
});
|
|
143
|
+
return res;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
test(
|
|
147
|
+
"integration: init supports URI / conninfo / psql-like connection styles",
|
|
148
|
+
{ skip: !havePostgresBinaries() },
|
|
149
|
+
async (t) => {
|
|
150
|
+
const pg = await withTempPostgres(t);
|
|
151
|
+
|
|
152
|
+
// 1) positional URI
|
|
153
|
+
{
|
|
154
|
+
const r = await runCliInit([pg.adminUri, "--password", "monpw", "--skip-optional-permissions"]);
|
|
155
|
+
assert.equal(r.status, 0, r.stderr || r.stdout);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 2) conninfo
|
|
159
|
+
{
|
|
160
|
+
const conninfo = `dbname=testdb host=127.0.0.1 port=${pg.port} user=postgres password=${pg.postgresPassword}`;
|
|
161
|
+
const r = await runCliInit([conninfo, "--password", "monpw2", "--skip-optional-permissions"]);
|
|
162
|
+
assert.equal(r.status, 0, r.stderr || r.stdout);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 3) psql-like options (+ PGPASSWORD)
|
|
166
|
+
{
|
|
167
|
+
const r = await runCliInit(
|
|
168
|
+
[
|
|
169
|
+
"-h",
|
|
170
|
+
"127.0.0.1",
|
|
171
|
+
"-p",
|
|
172
|
+
String(pg.port),
|
|
173
|
+
"-U",
|
|
174
|
+
"postgres",
|
|
175
|
+
"-d",
|
|
176
|
+
"testdb",
|
|
177
|
+
"--password",
|
|
178
|
+
"monpw3",
|
|
179
|
+
"--skip-optional-permissions",
|
|
180
|
+
],
|
|
181
|
+
{ PGPASSWORD: pg.postgresPassword }
|
|
182
|
+
);
|
|
183
|
+
assert.equal(r.status, 0, r.stderr || r.stdout);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
test(
|
|
189
|
+
"integration: init fixes slightly-off permissions idempotently",
|
|
190
|
+
{ skip: !havePostgresBinaries() },
|
|
191
|
+
async (t) => {
|
|
192
|
+
const pg = await withTempPostgres(t);
|
|
193
|
+
const { Client } = require("pg");
|
|
194
|
+
|
|
195
|
+
// Create monitoring role with wrong password, no grants.
|
|
196
|
+
{
|
|
197
|
+
const c = new Client({ connectionString: pg.adminUri });
|
|
198
|
+
await c.connect();
|
|
199
|
+
await c.query(
|
|
200
|
+
"do $$ begin if not exists (select 1 from pg_roles where rolname='postgres_ai_mon') then create role postgres_ai_mon login password 'wrong'; end if; end $$;"
|
|
201
|
+
);
|
|
202
|
+
await c.end();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Run init (should grant everything).
|
|
206
|
+
{
|
|
207
|
+
const r = await runCliInit([pg.adminUri, "--password", "correctpw", "--skip-optional-permissions"]);
|
|
208
|
+
assert.equal(r.status, 0, r.stderr || r.stdout);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Verify privileges.
|
|
212
|
+
{
|
|
213
|
+
const c = new Client({ connectionString: pg.adminUri });
|
|
214
|
+
await c.connect();
|
|
215
|
+
const dbOk = await c.query(
|
|
216
|
+
"select has_database_privilege('postgres_ai_mon', current_database(), 'CONNECT') as ok"
|
|
217
|
+
);
|
|
218
|
+
assert.equal(dbOk.rows[0].ok, true);
|
|
219
|
+
const roleOk = await c.query("select pg_has_role('postgres_ai_mon', 'pg_monitor', 'member') as ok");
|
|
220
|
+
assert.equal(roleOk.rows[0].ok, true);
|
|
221
|
+
const idxOk = await c.query(
|
|
222
|
+
"select has_table_privilege('postgres_ai_mon', 'pg_catalog.pg_index', 'SELECT') as ok"
|
|
223
|
+
);
|
|
224
|
+
assert.equal(idxOk.rows[0].ok, true);
|
|
225
|
+
const viewOk = await c.query(
|
|
226
|
+
"select has_table_privilege('postgres_ai_mon', 'public.pg_statistic', 'SELECT') as ok"
|
|
227
|
+
);
|
|
228
|
+
assert.equal(viewOk.rows[0].ok, true);
|
|
229
|
+
const sp = await c.query("select rolconfig from pg_roles where rolname='postgres_ai_mon'");
|
|
230
|
+
assert.ok(Array.isArray(sp.rows[0].rolconfig));
|
|
231
|
+
assert.ok(sp.rows[0].rolconfig.some((v) => String(v).includes("search_path=")));
|
|
232
|
+
await c.end();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Run init again (idempotent).
|
|
236
|
+
{
|
|
237
|
+
const r = await runCliInit([pg.adminUri, "--password", "correctpw", "--skip-optional-permissions"]);
|
|
238
|
+
assert.equal(r.status, 0, r.stderr || r.stdout);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
test("integration: init reports nicely when lacking permissions", { skip: !havePostgresBinaries() }, async (t) => {
|
|
244
|
+
const pg = await withTempPostgres(t);
|
|
245
|
+
const { Client } = require("pg");
|
|
246
|
+
|
|
247
|
+
// Create limited user that can connect but cannot create roles / grant.
|
|
248
|
+
const limitedPw = "limitedpw";
|
|
249
|
+
{
|
|
250
|
+
const c = new Client({ connectionString: pg.adminUri });
|
|
251
|
+
await c.connect();
|
|
252
|
+
await c.query(
|
|
253
|
+
"do $$ begin if not exists (select 1 from pg_roles where rolname='limited') then create role limited login password $1; end if; end $$;",
|
|
254
|
+
[limitedPw]
|
|
255
|
+
);
|
|
256
|
+
await c.query("grant connect on database testdb to limited");
|
|
257
|
+
await c.end();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const limitedUri = `postgresql://limited:${limitedPw}@127.0.0.1:${pg.port}/testdb`;
|
|
261
|
+
const r = await runCliInit([limitedUri, "--password", "monpw", "--skip-optional-permissions"]);
|
|
262
|
+
assert.notEqual(r.status, 0);
|
|
263
|
+
assert.match(r.stderr, /init failed:/);
|
|
264
|
+
// Should include step context and hint.
|
|
265
|
+
assert.match(r.stderr, /Failed at step "/);
|
|
266
|
+
assert.match(r.stderr, /Hint: connect as a superuser/i);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
|
|
8
|
+
test("maskConnectionString hides password when present", () => {
|
|
9
|
+
const masked = init.maskConnectionString("postgresql://user:secret@localhost:5432/mydb");
|
|
10
|
+
assert.match(masked, /postgresql:\/\/user:\*{5}@localhost:5432\/mydb/);
|
|
11
|
+
assert.doesNotMatch(masked, /secret/);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("parseLibpqConninfo parses basic host/dbname/user/port/password", () => {
|
|
15
|
+
const cfg = init.parseLibpqConninfo("dbname=mydb host=localhost user=alice port=5432 password=secret");
|
|
16
|
+
assert.equal(cfg.database, "mydb");
|
|
17
|
+
assert.equal(cfg.host, "localhost");
|
|
18
|
+
assert.equal(cfg.user, "alice");
|
|
19
|
+
assert.equal(cfg.port, 5432);
|
|
20
|
+
assert.equal(cfg.password, "secret");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("parseLibpqConninfo supports quoted values", () => {
|
|
24
|
+
const cfg = init.parseLibpqConninfo("dbname='my db' host='local host'");
|
|
25
|
+
assert.equal(cfg.database, "my db");
|
|
26
|
+
assert.equal(cfg.host, "local host");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("buildInitPlan includes create user when role does not exist", async () => {
|
|
30
|
+
const plan = await init.buildInitPlan({
|
|
31
|
+
database: "mydb",
|
|
32
|
+
monitoringUser: "postgres_ai_mon",
|
|
33
|
+
monitoringPassword: "pw",
|
|
34
|
+
includeOptionalPermissions: false,
|
|
35
|
+
roleExists: false,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
assert.equal(plan.database, "mydb");
|
|
39
|
+
assert.ok(plan.steps.some((s) => s.name === "create monitoring user"));
|
|
40
|
+
assert.ok(!plan.steps.some((s) => s.optional));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("buildInitPlan includes alter user when role exists", async () => {
|
|
44
|
+
const plan = await init.buildInitPlan({
|
|
45
|
+
database: "mydb",
|
|
46
|
+
monitoringUser: "postgres_ai_mon",
|
|
47
|
+
monitoringPassword: "pw",
|
|
48
|
+
includeOptionalPermissions: true,
|
|
49
|
+
roleExists: true,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
assert.ok(plan.steps.some((s) => s.name === "update monitoring user password"));
|
|
53
|
+
assert.ok(plan.steps.some((s) => s.optional));
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("resolveAdminConnection accepts positional URI", () => {
|
|
57
|
+
const r = init.resolveAdminConnection({ conn: "postgresql://u:p@h:5432/d" });
|
|
58
|
+
assert.ok(r.clientConfig.connectionString);
|
|
59
|
+
assert.doesNotMatch(r.display, /:p@/);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("resolveAdminConnection accepts positional conninfo", () => {
|
|
63
|
+
const r = init.resolveAdminConnection({ conn: "dbname=mydb host=localhost user=alice" });
|
|
64
|
+
assert.equal(r.clientConfig.database, "mydb");
|
|
65
|
+
assert.equal(r.clientConfig.host, "localhost");
|
|
66
|
+
assert.equal(r.clientConfig.user, "alice");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
|