go-sql-driver / mysql

Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
https://pkg.go.dev/github.com/go-sql-driver/mysql
Mozilla Public License 2.0
14.52k stars 2.32k forks source link

Introduce `timeTruncate` parameter for `time.Time` arguments #1541

Closed PauliusLozys closed 9 months ago

PauliusLozys commented 9 months ago

Description

This introduces timeTruncate parameter that will truncate time.Time values in query arguments.

After https://github.com/go-sql-driver/mysql/issues/1121, Timestamp and Datetime indexes in MariaDB broke due to nanoseconds now being present in time.Time https://github.com/go-sql-driver/mysql/issues/1121#issuecomment-852920526.

Query without nanoseconds part: On MariaDB v11.2.2 image

Same query on MySQL v8.3.0 image

Query with nanosecond part: On MariaDB v11.2.2 image

On MySQL v8.3.0 image

Currently, to have correct timestamp/datetime index usage is to either use time.Truncate(time.Microsecond) or time.Round(time.Microsecond). This can be very repetitive and is very likely to be forgotten when writing the query. Having a parameter that would truncate incoming time.Time values in the driver would prove useful and would remove the need to write time.Truncate methods every time a time value needs to be bound to an argument for MariaDB databases.

Checklist

Summary by CodeRabbit

coderabbitai[bot] commented 9 months ago

Walkthrough

The overall change introduces a new feature that allows time values to be truncated to a specified duration across various parts of the system. This is achieved by adding a TimeTruncate configuration option and updating the relevant functions to accept this new parameter. The feature impacts how date-time values are processed and formatted, affecting database connection configurations, parameter interpolation, and utility functions. It extends the system's flexibility in handling time values according to user-specified precision.

Changes

Files Change Summary
AUTHORS, README.md Added a new author and introduced the timeTruncate configuration option in the documentation.
connection.go, packets.go, utils.go Modified functions to include the TimeTruncate parameter, affecting date-time value processing.
dsn.go, dsn_test.go Updated the Config struct to include TimeTruncate, enhanced DSN parsing and formatting to support this new field, and added relevant tests.
utils_test.go Modified tests to incorporate time truncation testing, reflecting the new functionality.

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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

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 .` - `Generate unit-tests for this file.` - 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 tests 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 from git and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit tests.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` 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 a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - The JSON schema for the configuration file is available [here](https://coderabbit.ai/integrations/coderabbit-overrides.v2.json). - 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/coderabbit-overrides.v2.json` ### CodeRabbit Discord Community Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback.
methane commented 9 months ago

https://github.com/go-sql-driver/mysql/blob/c48c0e7da17e8fc06133e431ce7c10e7a3e94f06/connection.go#L249-L259

methane commented 9 months ago

~I think t.Truncate() should be called in caller of appendDateTime.~

Sorry. Adding truncate option to appendDateTime makes it easy to find a bug like missing truncate. When caller don't need truncate, caller can pass 0.

PauliusLozys commented 9 months ago

~I think t.Truncate() should be called in caller of appendDateTime.~

Sorry. Adding truncate option to appendDateTime makes it easy to find a bug like missing truncate. When caller don't need truncate, caller can pass 0.

Thanks for the review! I'm a little confused about the comment. Do you mean to keep the function as it is, or move the truncation logic outside appendDateTime? I think having truncation inside appendDateTime would seem more appropriate as this function's purpose is to format time string. Having the ability to pass additional formatting parameters like truncation seems appropriate and would show that such option is available. But if you think it's better to have it outside, I'll change it.

methane commented 9 months ago

Thanks for the review! I'm a little confused about the comment. Do you mean to keep the function as it is, or move the truncation logic outside appendDateTime?

Keep as it is.

coveralls commented 9 months ago

Coverage Status

coverage: 82.17% (-0.001%) from 82.171% when pulling e6f93d357045577d8453fc8f4868cebe18286158 on PauliusLozys:master into c48c0e7da17e8fc06133e431ce7c10e7a3e94f06 on go-sql-driver:master.

methane commented 9 months ago

We may revert adding TimeTruncate to Config obj, although keeping this feature.

See #1548 for detail.