Skip to content

Handle PG::ConnectionBad from PG#server_version similarly to version 0#54731

Merged
byroot merged 1 commit into
rails:mainfrom
joshuay03:handle-pg-check-version-error
Mar 10, 2025
Merged

Handle PG::ConnectionBad from PG#server_version similarly to version 0#54731
byroot merged 1 commit into
rails:mainfrom
joshuay03:handle-pg-check-version-error

Conversation

@joshuay03

Copy link
Copy Markdown
Contributor

Motivation / Background

Follow-up to #54713 and ged/ruby-pg#632.

Detail

Now that in future versions pg will raise PG::ConnectionBad when attempting to get the database version with a bad connection, we should handle it the same as when a version of 0 returned.

I opted to not preserve the error message from pg cause it's a bit too specific to the native implementation. Getting the message across to the user that the root of the failure is a bad connection should suffice imo.

cc @byroot

Additional information

N/A

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@joshuay03 joshuay03 moved this to In Progress / Pending Review in Open Source Mar 10, 2025
@byroot

byroot commented Mar 10, 2025

Copy link
Copy Markdown
Member

Isn't it already handled by with_raw_connection?

@joshuay03

Copy link
Copy Markdown
Contributor Author

Isn't it already handled by with_raw_connection?

Ah yes, however that raises ActiveRecord::ConnectionNotEstablished and will preserve the original message, what do you think about that?

If that's fine, should we update the explicit version 0 raise to match?

@byroot

byroot commented Mar 10, 2025

Copy link
Copy Markdown
Member

If the error message isn't good, we can handle that centrally in translate_exception_class

@joshuay03 joshuay03 force-pushed the handle-pg-check-version-error branch 5 times, most recently from aba78e4 to 65d7ba8 Compare March 10, 2025 09:48
@joshuay03

joshuay03 commented Mar 10, 2025

Copy link
Copy Markdown
Contributor Author

If the error message isn't good, we can handle that centrally in translate_exception_class

I've actioned this and made the version 0 logic raise a PG::ConnectionBad with a message partially matching the underlying one, which I think is fine for this special case? Mainly because the intention behind #translate_exception_class seems to be native exception translation only.

Alternatively we just keep the duplication.

ConnectionFailed.new(exception, connection_pool: @pool)
else
ConnectionNotEstablished.new(exception, connection_pool: @pool)
message = if exception.message.match?(/can't get server version/i)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I really don't think we should be rewriting error messages here, the original message is fine

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay I've updated, all this does now is ensure the error classes are consistent.

@joshuay03 joshuay03 force-pushed the handle-pg-check-version-error branch from 65d7ba8 to 407f3dc Compare March 10, 2025 10:43
@joshuay03 joshuay03 force-pushed the handle-pg-check-version-error branch from 407f3dc to 2aba1f5 Compare March 10, 2025 10:46
@joshuay03 joshuay03 changed the title Handle PG::ConnectionBad from PG#server_version similar to version 0 Handle PG::ConnectionBad from PG#server_version similarly to version 0 Mar 10, 2025
@byroot byroot merged commit bcab3ae into rails:main Mar 10, 2025
@joshuay03 joshuay03 deleted the handle-pg-check-version-error branch March 10, 2025 22:32
@joshuay03 joshuay03 moved this from In Progress / Pending Review to Done in Open Source Mar 10, 2025
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.

3 participants