mirror of
https://git.vectorsigma.ru/public/immich.git
synced 2026-08-07 12:28:20 +00:00
feat: schema-check (#25904)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user