Skip to content

Conversation

@Missna
Copy link

@Missna Missna commented Dec 9, 2025

Description of change

  • fix get version return a undefined value when use PolarDB-X 2.0
  • When using the distributed database PolarDB-X 2.0 compatible with MYSQL 8.0, executing the SELECT version () statement returns a column name of uppercase VERSION(), resulting in const version String=result [0] ["version ()"] returning version String as undefined and reporting an error

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links relevant issues as #11838
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md)

@qodo-free-for-open-source-projects

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Case Sensitivity

The fix assumes the column name will always be lowercase 'version', but different database systems or configurations might return different casing. Consider using a case-insensitive approach or checking for both 'version' and 'VERSION()' to ensure compatibility across all MySQL-compatible databases.

const result: [{ version: string }] = await this.query(
    "SELECT version() as version",
)

// MariaDB: https://mariadb.com/kb/en/version/
// - "10.2.27-MariaDB-10.2.27+maria~jessie-log"

// MySQL: https://dev.mysql.com/doc/refman/8.4/en/information-functions.html#function_version
// - "8.4.3"
// - "8.4.4-standard"

const versionString = result[0]["version"]

@qodo-free-for-open-source-projects

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Add check for empty query result

Add a check to ensure the database version query returns a result before
accessing it, preventing a potential runtime error if the result is unexpectedly
empty.

src/driver/mysql/MysqlQueryRunner.ts [3385-3387]

-const versionString = result[0]["version"]
+if (!result.length) {
+    throw new TypeORMError(`Could not get version from the database.`)
+}
+
+const versionString = result[0].version
 
 return versionString.replace(/^([\d.]+).*$/, "$1")
  • Apply / Chat
Suggestion importance[1-10]: 4

__

Why: The suggestion correctly identifies a potential TypeError if the query returns an empty result, but the SELECT version() query is highly unlikely to do so, making this a low-impact robustness improvement.

Low
  • More

@Missna
Copy link
Author

Missna commented Dec 9, 2025

When using the distributed database PolarDB-X 2.0 compatible with MYSQL 8.0,start a nestjs server will show a error:

[Nest] 8943 - 2025/12/09 10:42:40 ERROR [TypeOrmModule] Unable to connect to the database (ops-db-connection). Retrying (1)...
TypeError: Cannot read properties of undefined (reading 'replace')
at MysqlQueryRunner.getVersion (/Users/admin/workspace/projects/bff-nestjs/node_modules/.pnpm/[email protected][email protected][email protected][email protected]_ts-node@10.9.2_@types[email protected][email protected]_/node_modules/typeorm/driver/src/driver/mysql/MysqlQueryRunner.ts:3385:30)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async MysqlDriver.connect (/Users/admin/workspace/projects/bff-nestjs/node_modules/.pnpm/[email protected][email protected][email protected][email protected]_ts-node@10.9.2_@types[email protected][email protected]_/node_modules/typeorm/driver/src/driver/mysql/MysqlDriver.ts:398:24)
at async DataSource.initialize (/Users/admin/workspace/projects/bff-nestjs/node_modules/.pnpm/[email protected][email protected][email protected][email protected]_ts-node@10.9.2_@types[email protected][email protected]_/node_modules/typeorm/data-source/src/data-source/DataSource.ts:254:9)

Copy link
Collaborator

@gioboa gioboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your help @Missna
Can you add a test to validate this change?

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 9, 2025

commit: ce144b8

@coveralls
Copy link

Coverage Status

coverage: 80.79% (+0.03%) from 80.765%
when pulling ce144b8 on Missna:fix/getVersion
into 514e3d8 on typeorm:master.

@OSA413
Copy link
Collaborator

OSA413 commented Dec 9, 2025

Can you add a test to validate this change?

A test for PolarDB or for MySQL?

Copy link
Collaborator

@gioboa gioboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a test that shows that this fix is solving the issue.
if this was an issue with MySql, let's use it.


UPDATE

I saw your message, it's a PolarDB issue. Anyway, Let's add a test for mysql to validate the correct beaviour.

@pkuczynski pkuczynski changed the title fix: fix get version return undefined fix: get version returning undefined when using PolarDB-X 2.0 Dec 9, 2025
@pkuczynski pkuczynski changed the title fix: get version returning undefined when using PolarDB-X 2.0 fix(mysql): getVersion returning undefined for PolarDB-X 2.0 Dec 9, 2025
Copy link
Collaborator

@pkuczynski pkuczynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, simple test would be useful...

@OSA413
Copy link
Collaborator

OSA413 commented Dec 10, 2025

I'm afraid that fixing the version return is not enough to make PolarDBX to work properly with TypeORM, after getting the version it fails at every test saying No database selected.

You can play around with their docker image and see yourself.

(Here was a big message with logs from their container to demonstrate what I'm talking about but docker ate up all the RAM I had before I could send the comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants