feat: schema-check (#25904)

This commit is contained in:
Jason Rasmussen
2026-02-12 17:59:00 -05:00
committed by GitHub
parent 7413356a2f
commit 8ef4e4d452
37 changed files with 449 additions and 213 deletions

View File

@@ -5,14 +5,20 @@ import { ReaderContext } from 'src/sql-tools/contexts/reader-context';
import { readers } from 'src/sql-tools/readers';
import { DatabaseSchema, PostgresDB, SchemaFromDatabaseOptions } from 'src/sql-tools/types';
export type DatabaseLike = Sql | Kysely<any>;
const isKysely = (db: DatabaseLike): db is Kysely<any> => db instanceof Kysely;
/**
* Load schema from a database url
*/
export const schemaFromDatabase = async (
postgres: Sql,
database: DatabaseLike,
options: SchemaFromDatabaseOptions = {},
): Promise<DatabaseSchema> => {
const db = new Kysely<PostgresDB>({ dialect: new PostgresJSDialect({ postgres }) });
const db = isKysely(database)
? (database as Kysely<PostgresDB>)
: new Kysely<PostgresDB>({ dialect: new PostgresJSDialect({ postgres: database }) });
const ctx = new ReaderContext(options);
try {
@@ -22,6 +28,9 @@ export const schemaFromDatabase = async (
return ctx.build();
} finally {
await db.destroy();
// only close the connection it we created it
if (!isKysely(database)) {
await db.destroy();
}
}
};