Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres version command incompatible with Redshift #9318

Closed
CaptainCrucial opened this issue Aug 24, 2022 · 0 comments · Fixed by #9319
Closed

Postgres version command incompatible with Redshift #9318

CaptainCrucial opened this issue Aug 24, 2022 · 0 comments · Fixed by #9319

Comments

@CaptainCrucial
Copy link
Contributor

When connecting using the Postgres driver, the afterConnect method is running the SQLSHOW server_version; as part of feature support detection.

This command works on postgres and cockroachdb, but is incompatible with the redshift database.

This command could be adjusted to use the SELECT version(); command which is supported across all of these database types.

I've patched this command locally and successfully connected and read data using typeorm:

diff --git a/src/driver/postgres/PostgresDriver.ts b/src/driver/postgres/PostgresDriver.ts
index 92142ba53..0dfad6d7a 100644
--- a/src/driver/postgres/PostgresDriver.ts
+++ b/src/driver/postgres/PostgresDriver.ts
@@ -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",
diff --git a/src/driver/postgres/PostgresQueryRunner.ts b/src/driver/postgres/PostgresQueryRunner.ts
index e8aaa3eaa..4a5c84655 100644
--- a/src/driver/postgres/PostgresQueryRunner.ts
+++ b/src/driver/postgres/PostgresQueryRunner.ts
@@ -3989,8 +3989,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")
     }
 
     /**
diff --git a/test/functional/cube/postgres/cube-postgres.ts b/test/functional/cube/postgres/cube-postgres.ts
index f70baf10b..d5e81a68c 100644
--- a/test/functional/cube/postgres/cube-postgres.ts
+++ b/test/functional/cube/postgres/cube-postgres.ts
@@ -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
                 }

Good to get your thoughts

pleerock pushed a commit that referenced this issue Aug 25, 2022
* 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
nordinh pushed a commit to nordinh/typeorm that referenced this issue Aug 29, 2022
* fix typeorm#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: typeorm#9318

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

* fix-9318: prettier format test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant