Skip to content

Commit

Permalink
fix: change postgres driver version checking query (#9319)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
CaptainCrucial committed Aug 25, 2022
1 parent 3671887 commit c4f4650
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

/**
Expand Down
9 changes: 5 additions & 4 deletions test/functional/cube/postgres/cube-postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
47 changes: 47 additions & 0 deletions test/github-issues/9318/issue-9318.ts
Original file line number Diff line number Diff line change
@@ -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,
)
}),
))
})

0 comments on commit c4f4650

Please sign in to comment.