Skip to content

Commit 7fdce33

Browse files
committed
Update test to use pgsql-test
1 parent 681d3b9 commit 7fdce33

7 files changed

Lines changed: 412 additions & 490 deletions

File tree

pgpm/cli/__tests__/export-parity.test.ts

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,16 @@ jest.doMock(_exportGraphQLMetaPath, () => ({
5858
exportGraphQLMeta: jest.fn()
5959
}));
6060

61-
// NOW load everything — pg-cache, core exports, etc.
61+
// NOW load everything — core exports, etc.
6262
const {
6363
PgpmPackage,
6464
exportGraphQL,
6565
exportMigrations,
6666
exportMeta
6767
} = require('@pgpmjs/core');
68-
const { getPgPool, teardownPgPools } = require('pg-cache');
69-
const { getPgEnvOptions } = require('pg-env');
7068
const { PgpmMigrate } = require('@pgpmjs/core');
7169
const { camelize } = require('inflekt');
70+
const { getConnections, seed } = require('pgsql-test');
7271

7372
// Mocked modules we need to configure per-test
7473
const { GraphQLClient } = require(_graphqlClientPath);
@@ -246,10 +245,10 @@ const SEED_SQL = `
246245

247246
describe('export parity — SQL vs GraphQL (integration)', () => {
248247
let tempDir: string;
249-
let dbName: string;
250248
let dbConfig: any;
251249
let sqlWorkspaceDir: string;
252250
let graphqlWorkspaceDir: string;
251+
let teardown: () => Promise<void>;
253252

254253
// Pre-fetched from the real DB in beforeAll (before exportMigrations ends the pool)
255254
let schemaNames: string[];
@@ -270,24 +269,37 @@ describe('export parity — SQL vs GraphQL (integration)', () => {
270269
beforeAll(async () => {
271270
tempDir = fs.mkdtempSync(path.join(require('os').tmpdir(), 'pgpm-parity-int-'));
272271

273-
// Create temp database
274-
dbName = `test_parity_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
275-
const baseConfig = getPgEnvOptions({ database: 'postgres' });
276-
const adminPool = getPgPool(baseConfig);
277-
await adminPool.query(`CREATE DATABASE "${dbName}"`);
278-
279-
const pgConfig = getPgEnvOptions({ database: dbName });
280-
dbConfig = {
281-
host: pgConfig.host,
282-
port: pgConfig.port,
283-
user: pgConfig.user,
284-
password: pgConfig.password,
285-
database: pgConfig.database
286-
};
272+
({ teardown } = await getConnections({}, [
273+
seed.fn(async ({ pg: pgClient, config }: any) => {
274+
dbConfig = config;
275+
276+
// Initialize pgpm_migrate schema
277+
const migrate = new PgpmMigrate(config);
278+
await migrate.initialize();
279+
280+
// Create shim schemas + tables
281+
await pgClient.query(SCHEMA_SHIMS_SQL);
282+
283+
// Seed data
284+
await pgClient.query(SEED_SQL);
285+
286+
const schemaResult = await pgClient.query(
287+
'SELECT * FROM metaschema_public.schema WHERE database_id = $1',
288+
[DATABASE_ID]
289+
);
290+
schemas = schemaResult.rows;
291+
schemaNames = schemas.map((r: any) => r.schema_name);
292+
293+
const sqlActionsResult = await pgClient.query(
294+
'SELECT * FROM db_migrate.sql_actions WHERE database_id = $1 ORDER BY id',
295+
[DATABASE_ID]
296+
);
297+
camelActions = sqlActionsResult.rows.map(pgRowToCamel);
287298

288-
// Initialize pgpm_migrate schema
289-
const migrate = new PgpmMigrate(dbConfig);
290-
await migrate.initialize();
299+
// Run the real SQL-based exportMeta to get the meta result that both flows will use
300+
metaResult = await exportMeta({ opts: { pg: config }, dbname: config.database, database_id: DATABASE_ID });
301+
})
302+
]));
291303

292304
// ---- Prepare two isolated workspaces for the exports ----
293305
for (const label of ['sql-flow', 'graphql-flow']) {
@@ -310,41 +322,11 @@ describe('export parity — SQL vs GraphQL (integration)', () => {
310322

311323
sqlWorkspaceDir = path.join(tempDir, 'sql-flow');
312324
graphqlWorkspaceDir = path.join(tempDir, 'graphql-flow');
313-
314-
// Pre-fetch everything we need from the DB BEFORE any export runs
315-
// (exportMigrations calls pgPool.end() internally, killing the pool)
316-
const dbPool = getPgPool(pgConfig);
317-
318-
// Create shim schemas + tables
319-
await dbPool.query(SCHEMA_SHIMS_SQL);
320-
321-
// Seed data
322-
await dbPool.query(SEED_SQL);
323-
324-
const schemaResult = await dbPool.query(
325-
'SELECT * FROM metaschema_public.schema WHERE database_id = $1',
326-
[DATABASE_ID]
327-
);
328-
schemas = schemaResult.rows;
329-
schemaNames = schemas.map((r: any) => r.schema_name);
330-
331-
const sqlActionsResult = await dbPool.query(
332-
'SELECT * FROM db_migrate.sql_actions WHERE database_id = $1 ORDER BY id',
333-
[DATABASE_ID]
334-
);
335-
camelActions = sqlActionsResult.rows.map(pgRowToCamel);
336-
337-
// Run the real SQL-based exportMeta to get the meta result that both flows will use
338-
metaResult = await exportMeta({ opts: { pg: dbConfig }, dbname: dbName, database_id: DATABASE_ID });
339325
});
340326

341327
afterAll(async () => {
342-
try { await teardownPgPools(); } catch (_) { /* */ }
343-
try {
344-
const adminPool = getPgPool(getPgEnvOptions({ database: 'postgres' }));
345-
await adminPool.query(`DROP DATABASE IF EXISTS "${dbName}"`);
346-
} catch (_) { /* */ }
347-
try { fs.rmSync(tempDir, { recursive: true, force: true }); } catch (_) { /* */ }
328+
await teardown();
329+
fs.rmSync(tempDir, { recursive: true, force: true });
348330
});
349331

350332
// =========================================================================
@@ -358,7 +340,7 @@ describe('export parity — SQL vs GraphQL (integration)', () => {
358340
project: sqlProject,
359341
options: { pg: dbConfig },
360342
dbInfo: {
361-
dbname: dbName,
343+
dbname: dbConfig.database,
362344
databaseName: DATABASE_NAME,
363345
database_ids: [DATABASE_ID]
364346
},

pgpm/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"glob": "^13.0.6",
4343
"makage": "^0.1.12",
4444
"pg": "^8.20.0",
45+
"pgsql-test": "workspace:^",
4546
"ts-node": "^10.9.2"
4647
},
4748
"dependencies": {

0 commit comments

Comments
 (0)