MCPcopy
hub / github.com/drizzle-team/drizzle-orm / introspectPgToFile

Function introspectPgToFile

drizzle-kit/tests/schemaDiffer.ts:2303–2403  ·  view source on GitHub ↗
(
	client: PGlite,
	initSchema: PostgresSchema,
	testName: string,
	schemas: string[] = ['public'],
	entities?: Entities,
	casing?: CasingType | undefined,
)

Source from the content-addressed store, hash-verified

2301// --- Introspect to file helpers ---
2302
2303export const introspectPgToFile = async (
2304 client: PGlite,
2305 initSchema: PostgresSchema,
2306 testName: string,
2307 schemas: string[] = ['public'],
2308 entities?: Entities,
2309 casing?: CasingType | undefined,
2310) => {
2311 // put in db
2312 const { sqlStatements } = await applyPgDiffs(initSchema, casing);
2313 for (const st of sqlStatements) {
2314 await client.query(st);
2315 }
2316
2317 // introspect to schema
2318 const introspectedSchema = await fromDatabase(
2319 {
2320 query: async (query: string, values?: any[] | undefined) => {
2321 const res = await client.query(query, values);
2322 return res.rows as any[];
2323 },
2324 },
2325 undefined,
2326 schemas,
2327 entities,
2328 );
2329
2330 const { version: initV, dialect: initD, ...initRest } = introspectedSchema;
2331
2332 const initSch = {
2333 version: '7',
2334 dialect: 'postgresql',
2335 id: '0',
2336 prevId: '0',
2337 ...initRest,
2338 } as const;
2339
2340 const initSn = squashPgScheme(initSch);
2341 const validatedCur = pgSchema.parse(initSch);
2342
2343 // write to ts file
2344 const file = schemaToTypeScript(introspectedSchema, 'camel');
2345
2346 fs.writeFileSync(`tests/introspect/postgres/${testName}.ts`, file.file);
2347
2348 // generate snapshot from ts file
2349 const response = await prepareFromPgImports([
2350 `tests/introspect/postgres/${testName}.ts`,
2351 ]);
2352
2353 const afterFileImports = generatePgSnapshot(
2354 response.tables,
2355 response.enums,
2356 response.schemas,
2357 response.sequences,
2358 response.roles,
2359 response.policies,
2360 response.views,

Callers 1

pg.test.tsFile · 0.90

Calls 15

fromDatabaseFunction · 0.90
squashPgSchemeFunction · 0.90
schemaToTypeScriptFunction · 0.90
prepareFromPgImportsFunction · 0.90
generatePgSnapshotFunction · 0.90
applyPgSnapshotsDiffFunction · 0.90
applyPgDiffsFunction · 0.85
testSchemasResolverFunction · 0.85
testEnumsResolverFunction · 0.85
testSequencesResolverFunction · 0.85
testPolicyResolverFunction · 0.85
testIndPolicyResolverFunction · 0.85

Tested by

no test coverage detected