From e4d27b9a60de57ff9e7d9a43d0af984572ee41b7 Mon Sep 17 00:00:00 2001 From: Yehor Kulbachka <38127655+EgorKulbachka@users.noreply.github.com> Date: Fri, 12 Jan 2024 19:46:44 +0100 Subject: [PATCH] Use server_version setting to resolve current PG version. (#279) --- .../pg-migrations/src/PostgresDatabaseEngine.ts | 13 ++++++------- .../src/__tests__/getTypes.test.pg.ts | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/pg-migrations/src/PostgresDatabaseEngine.ts b/packages/pg-migrations/src/PostgresDatabaseEngine.ts index 57ff3785..fdb3caea 100644 --- a/packages/pg-migrations/src/PostgresDatabaseEngine.ts +++ b/packages/pg-migrations/src/PostgresDatabaseEngine.ts @@ -248,14 +248,13 @@ function getExport(mod: any, filename: string) { } async function getPgVersion(connection: Queryable): Promise<[number, number]> { - // e.g. PostgreSQL 10.1 on x86_64-apple-darwin16.7.0, compiled by Apple LLVM version 9.0.0 (clang-900.0.38), 64-bit - const [{version: sqlVersionString}] = await connection.query( - connection.sql`SELECT version();`, + // server_version -> 10.6.0 + const [{server_version: sqlVersionString}] = await connection.query( + connection.sql`SHOW server_version;`, ); - const match = /PostgreSQL (\d+).(\d+)/.exec(sqlVersionString); - if (match) { - const [, major, minor] = match; - return [parseInt(major, 10), parseInt(minor, 10)]; + const versions = sqlVersionString.split('.'); + if (versions.length > 1) { + return [parseInt(versions[0], 10), parseInt(versions[1], 10)]; } return [0, 0]; } diff --git a/packages/pg-schema-introspect/src/__tests__/getTypes.test.pg.ts b/packages/pg-schema-introspect/src/__tests__/getTypes.test.pg.ts index 99b4ce77..90033644 100644 --- a/packages/pg-schema-introspect/src/__tests__/getTypes.test.pg.ts +++ b/packages/pg-schema-introspect/src/__tests__/getTypes.test.pg.ts @@ -519,14 +519,13 @@ test('get custom types', async () => { }); async function getPgVersion(): Promise<[number, number]> { - // e.g. PostgreSQL 10.1 on x86_64-apple-darwin16.7.0, compiled by Apple LLVM version 9.0.0 (clang-900.0.38), 64-bit - const [{version: sqlVersionString}] = await db.query( - db.sql`SELECT version();`, + // server_version -> 10.6.0 + const [{server_version: sqlVersionString}] = await db.query( + db.sql`SHOW server_version;`, ); - const match = /PostgreSQL (\d+).(\d+)/.exec(sqlVersionString); - if (match) { - const [, major, minor] = match; - return [parseInt(major, 10), parseInt(minor, 10)]; + const versions = sqlVersionString.split('.'); + if (versions.length > 1) { + return [parseInt(versions[0], 10), parseInt(versions[1], 10)]; } return [0, 0]; }