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

allow unknown collation name #1604

Merged
merged 2 commits into from
Aug 30, 2024

Conversation

methane
Copy link
Member

@methane methane commented Jul 9, 2024

Description

Fix #1603

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

Summary by CodeRabbit

  • New Features

    • Added support for specifying multiple character sets during connection.
    • Updated configuration to handle character sets and collations more efficiently.
  • Bug Fixes

    • Improved logic for setting collation IDs to prevent potential errors during connection.
  • Refactor

    • Simplified connection parameter handling for system variables.
    • Enhanced internal consistency by renaming constants and updating data handling methods.
  • Tests

    • Updated test configurations to reflect new charset handling and ensure robust validation.

Copy link

coderabbitai bot commented Jul 9, 2024

Walkthrough

The recent changes enhance the handling of charset and collation settings in the Go MySQL driver. The updates enable efficient collation handling by converting string-based collations to ID-based ones. The connection logic is simplified by consolidating system variable settings into a single command. Additionally, the changes improve error management and provide better support for custom collation specifications, ensuring accurate collation assignments during database interactions.

Changes

File Summary
collations.go Renamed defaultCollation to defaultCollationID and changed its value to 45 for better clarity and efficiency.
connection.go Simplified handleParams function by consolidating system variable settings into a single SET command.
connector.go Updated Connect method to handle character set settings and collations based on provided configurations.
dsn.go Added charsets field to Config struct and updated collation handling in DSN formatting and parsing functions.
dsn_test.go Modified Config struct in tests to use the new charsets field instead of a map for connection charset settings.
packets.go Revised writeHandshakeResponsePacket function to properly set collation ID based on collation and charset.

Sequence Diagram(s)

N/A

Assessment against linked issues

Objective Addressed Explanation
Connection collations limit use of custom collation specification (#1603)
Handle charset and collation assignments more efficiently during connection establishment (#1603)
Simplify and consolidate connection parameter handling logic (#1603)

Poem

Amid the bytes and lines we tread,
Collations once brought us dread,
But now with elegance, they're set,
In charset fields, we won't forget.
Code refined, with charsets arrayed,
Bugs dispelled, and progress made.
Cheers to change, where bugs have bled,
A future bright, now lies ahead! ☀️


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (1)
dsn.go (1)

47-48: Document the charsets field.

Ensure that the new charsets field is documented in the Config struct's comments for clarity.

// charsets: Connection charset. When set, this will be set in SET NAMES <charset> query
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3484db1 and f096378.

Files selected for processing (6)
  • collations.go (1 hunks)
  • connection.go (1 hunks)
  • connector.go (1 hunks)
  • dsn.go (3 hunks)
  • dsn_test.go (1 hunks)
  • packets.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • collations.go
Additional comments not posted (8)
connector.go (1)

183-200: Ensure proper error handling for charset and collation settings.

The logic for setting character sets and collation during connection establishment is correct. However, ensure that any errors encountered during SET NAMES are logged for debugging purposes.

if err != nil {
    c.cfg.Logger.Print("failed to set charset '"+cs+"' with collation '"+mc.cfg.Collation+"': ", err.Error())
    mc.Close()
    return nil, err
}
dsn_test.go (3)

34-34: Update charset handling in test configurations.

The change from Params to charsets in the test configurations is correct. Ensure that all test cases are updated accordingly.


37-37: Update charset handling in test configurations.

The change from Params to charsets in the test configurations is correct. Ensure that all test cases are updated accordingly.


40-40: Update charset handling in test configurations.

The change from Params to charsets in the test configurations is correct. Ensure that all test cases are updated accordingly.

dsn.go (2)

286-288: Handle multiple charsets in DSN formatting.

The logic for handling multiple charsets in the DSN is correct. Ensure that the FormatDSN method properly joins and formats the charset values.


509-512: Parse charset parameter as a slice.

The logic for parsing the charset parameter as a slice of strings is correct. Ensure that the parseDSNParams function handles this correctly.

packets.go (2)

325-325: Use the correct variable for collation ID.

Ensure that the defaultCollationID variable is correctly defined and used.


326-333: Ensure proper handling of unknown collation names.

The logic for handling unknown collation names looks correct. However, verify that the collations map contains all necessary collation names to avoid unexpected errors.

connection.go Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Jul 9, 2024

Coverage Status

coverage: 82.403% (-0.04%) from 82.446%
when pulling 0b10adb on methane:allow-unknown-collations
into 3484db1 on go-sql-driver:master.

}

if cmdSet.Len() > 0 {
err = mc.exec(cmdSet.String())
if err != nil {

Choose a reason for hiding this comment

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

I'm not sure if there's an expected reviewer set who will look this PR over, but to me this is the only thing in the PR I have reservations on. It might be worth mentioning the behavioral impact of not returning an error during connection param setting? I'm guessing this is to avoid errors where we're "falling through" different charsets or collations and multiple have been specified, but I'm not sure if it's worth still throwing an error if we're dealing with a problem like someone adding a param for a variable that should be a global var.

Ex:

mysql> SET max_connections = 1000;
ERROR 1229 (HY000): Variable 'max_connections' is a
GLOBAL variable and should be set with SET GLOBAL

type situation

Copy link
Member Author

Choose a reason for hiding this comment

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

This function uses named return value. So this line returns the errorr.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f096378 and 0b10adb.

Files selected for processing (1)
  • dsn.go (3 hunks)
Additional comments not posted (3)
dsn.go (3)

47-48: Addition of charsets field in Config struct.

The new charsets field allows specifying multiple character sets, enhancing flexibility in connection configuration. Ensure that the usage of this field is well-documented and that any interactions with the Collation field are clearly defined.


286-288: Incorporation of charsets in DSN formatting.

The FormatDSN function now correctly includes the charsets in the DSN string. Verify that this change does not adversely affect DSN string formation and that it integrates well with existing configurations.

Verification successful

Incorporation of charsets in DSN formatting is verified.

The FormatDSN function correctly integrates the charsets into the DSN string. This functionality is covered by existing tests in dsn_test.go and dsn_fuzz_test.go, ensuring that it does not adversely affect DSN string formation. No further action is required.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `charsets` are correctly incorporated in DSN strings.

# Test: Search for `FormatDSN` usage. Expect: Correct DSN string formation with `charsets`.
ast-grep --lang go --pattern $'func ($_ *Config) FormatDSN() string {
  $$$
  if charsets := $_.charsets; len(charsets) > 0 {
    $$$
  }
  $$$
}'

Length of output: 147


Script:

#!/bin/bash
# Search for the definition of the `FormatDSN` function in the codebase.
ast-grep --lang go --pattern 'func ($_ *Config) FormatDSN() string { $$$ }'

# Search for usages of the `FormatDSN` function in the codebase.
rg 'FormatDSN' -A 5

# Search for test files that might be testing `FormatDSN`.
rg 'FormatDSN' --type go --glob '*_test.go' -A 5

Length of output: 8094


509-511: Parsing of charset parameter in DSN.

The parseDSNParams function now correctly parses the charset parameter into a slice for the charsets field. Ensure that this parsing logic can handle multiple charsets and that it aligns with expected DSN configurations.

Copy link
Contributor

@shogo82148 shogo82148 left a comment

Choose a reason for hiding this comment

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

LGTM

@methane methane merged commit 00dc21a into go-sql-driver:master Aug 30, 2024
38 checks passed
@methane methane deleted the allow-unknown-collations branch August 30, 2024 05:38
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 this pull request may close these issues.

Connection collations limit use of custom collation specification
4 participants