|
| 1 | +import { test, expect } from "vitest"; |
| 2 | +import { PostgreSqlContainer } from "@testcontainers/postgresql"; |
| 3 | +import { ConnectionManager } from "./connection-manager.ts"; |
| 4 | +import { Connectable } from "./connectable.ts"; |
| 5 | + |
| 6 | +test("getRecentQueries resolves pg_stat_statements in a non-default schema", async () => { |
| 7 | + const pg = await new PostgreSqlContainer("postgres:17") |
| 8 | + .withCopyContentToContainer([ |
| 9 | + { |
| 10 | + content: ` |
| 11 | + CREATE SCHEMA monitoring; |
| 12 | + CREATE EXTENSION pg_stat_statements SCHEMA monitoring; |
| 13 | +
|
| 14 | + CREATE TABLE users(id int, name text); |
| 15 | + INSERT INTO users (id, name) VALUES (1, 'alice'); |
| 16 | + SELECT * FROM users WHERE id = 1; |
| 17 | + `, |
| 18 | + target: "/docker-entrypoint-initdb.d/init.sql", |
| 19 | + }, |
| 20 | + ]) |
| 21 | + .withCommand([ |
| 22 | + "-c", |
| 23 | + "shared_preload_libraries=pg_stat_statements", |
| 24 | + "-c", |
| 25 | + "autovacuum=off", |
| 26 | + "-c", |
| 27 | + "track_counts=off", |
| 28 | + "-c", |
| 29 | + "track_io_timing=off", |
| 30 | + "-c", |
| 31 | + "track_activities=off", |
| 32 | + ]) |
| 33 | + .start(); |
| 34 | + |
| 35 | + const manager = ConnectionManager.forLocalDatabase(); |
| 36 | + const conn = Connectable.fromString(pg.getConnectionUri()); |
| 37 | + const connector = manager.getConnectorFor(conn); |
| 38 | + |
| 39 | + try { |
| 40 | + const recentQueries = await connector.getRecentQueries(); |
| 41 | + const userQuery = recentQueries.find((q) => |
| 42 | + q.query.includes("users") |
| 43 | + ); |
| 44 | + expect(userQuery, "Expected to find a query involving 'users' table").toBeTruthy(); |
| 45 | + } finally { |
| 46 | + await manager.closeAll(); |
| 47 | + await pg.stop(); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +test("resetPgStatStatements works with a non-default schema", async () => { |
| 52 | + const pg = await new PostgreSqlContainer("postgres:17") |
| 53 | + .withCopyContentToContainer([ |
| 54 | + { |
| 55 | + content: ` |
| 56 | + CREATE SCHEMA monitoring; |
| 57 | + CREATE EXTENSION pg_stat_statements SCHEMA monitoring; |
| 58 | +
|
| 59 | + CREATE TABLE users(id int, name text); |
| 60 | + INSERT INTO users (id, name) VALUES (1, 'alice'); |
| 61 | + SELECT * FROM users WHERE id = 1; |
| 62 | + `, |
| 63 | + target: "/docker-entrypoint-initdb.d/init.sql", |
| 64 | + }, |
| 65 | + ]) |
| 66 | + .withCommand([ |
| 67 | + "-c", |
| 68 | + "shared_preload_libraries=pg_stat_statements", |
| 69 | + "-c", |
| 70 | + "autovacuum=off", |
| 71 | + "-c", |
| 72 | + "track_counts=off", |
| 73 | + "-c", |
| 74 | + "track_io_timing=off", |
| 75 | + "-c", |
| 76 | + "track_activities=off", |
| 77 | + ]) |
| 78 | + .start(); |
| 79 | + |
| 80 | + const manager = ConnectionManager.forLocalDatabase(); |
| 81 | + const conn = Connectable.fromString(pg.getConnectionUri()); |
| 82 | + const connector = manager.getConnectorFor(conn); |
| 83 | + |
| 84 | + try { |
| 85 | + const before = await connector.getRecentQueries(); |
| 86 | + expect(before.length, "Expected queries before reset").toBeGreaterThan(0); |
| 87 | + |
| 88 | + await connector.resetPgStatStatements(); |
| 89 | + |
| 90 | + const after = await connector.getRecentQueries(); |
| 91 | + expect(after.length, "Expected 0 queries after reset").toEqual(0); |
| 92 | + } finally { |
| 93 | + await manager.closeAll(); |
| 94 | + await pg.stop(); |
| 95 | + } |
| 96 | +}); |
0 commit comments