fix: change postgres driver version checking query (#9319)

* fix #9318

fix: change postgres driver version checking query

Change the postgres `SHOW server_version` query to use `SELECT version()` which adds compatibility with AWS Redshift database

Closes: #9318

* git-issue 9318: remove describe from only in test

* fix-9318: prettier format test
This commit is contained in:
John 2022-08-25 13:40:12 +01:00 committed by GitHub
parent 36718876f9
commit c4f46506d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 9 deletions

View File

@ -375,13 +375,16 @@ export class PostgresDriver implements Driver {
const results = (await this.executeQuery(
connection,
"SHOW server_version;",
"SELECT version();",
)) as {
rows: {
server_version: string
version: string
}[]
}
const versionString = results.rows[0].server_version
const versionString = results.rows[0].version.replace(
/^PostgreSQL ([\d\.]+) .*$/,
"$1",
)
this.isGeneratedColumnsSupported = VersionUtils.isGreaterOrEqual(
versionString,
"12.0",

View File

@ -3991,8 +3991,8 @@ export class PostgresQueryRunner
* Loads Postgres version.
*/
protected async getVersion(): Promise<string> {
const result = await this.query(`SHOW SERVER_VERSION`)
return result[0]["server_version"]
const result = await this.query(`SELECT version()`)
return result[0]["version"].replace(/^PostgreSQL ([\d\.]+) .*$/, "$1")
}
/**

View File

@ -109,10 +109,11 @@ describe("cube-postgres", () => {
// Get Postgres version because zero-length cubes are not legal
// on all Postgres versions. Zero-length cubes are only tested
// to be working on Postgres version >=10.6.
const [{ server_version }] = await connection.query(
"SHOW server_version",
)
const semverArray = server_version.split(".").map(Number)
const [{ version }] = await connection.query("SELECT version()")
const semverArray = version
.replace(/^PostgreSQL ([\d\.]+) .*$/, "$1")
.split(".")
.map(Number)
if (!(semverArray[0] >= 10 && semverArray[1] >= 6)) {
return
}

View File

@ -0,0 +1,47 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src"
import { expect } from "chai"
import { PostgresDriver } from "../../../src/driver/postgres/PostgresDriver"
import { VersionUtils } from "../../../src/util/VersionUtils"
describe("github issues > #9318 Change version query from SHOW server_version to SELECT version", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [],
schemaCreate: false,
dropSchema: true,
enabledDrivers: ["postgres"],
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))
it("should have proper isGeneratedColumnsSupported value for postgres version", () =>
Promise.all(
connections.map(async (connection) => {
const { isGeneratedColumnsSupported } =
connection.driver as PostgresDriver
const result = await connection.query("SELECT VERSION()")
const dbVersion = result[0]["version"].replace(
/^PostgreSQL ([\d\.]+) .*$/,
"$1",
)
const versionGreaterOfEqualTo12 = VersionUtils.isGreaterOrEqual(
dbVersion,
"12.0",
)
expect(isGeneratedColumnsSupported).to.eq(
versionGreaterOfEqualTo12,
)
}),
))
})