8000 fix: Carbon was being altered by google/cloud-spanner by taka-oyama · Pull Request #264 · colopl/laravel-spanner · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: Carbon was being altered by google/cloud-spanner #264

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

Merged
merged 1 commit into from
Feb 19, 2025

Conversation

taka-oyama
Copy link
Collaborator
@taka-oyama taka-oyama commented Feb 18, 2025

Caused by this.

Summary by CodeRabbit

  • Bug Fixes

    • Improved date-time handling to ensure that original time values remain unaltered, preserving accurate timestamps and time zones during operations.
  • Tests

    • Introduced validation to confirm that time zone settings are maintained when processing date-time values in requests.

@taka-oyama taka-oyama added bug Something isn't working waiting for review labels Feb 18, 2025
@taka-oyama taka-oyama requested review from zeriyoshi, t-matsuno-777 and a team February 18, 2025 11:58
@taka-oyama taka-oyama self-assigned this Feb 18, 2025
Copy link
coderabbitai bot commented Feb 18, 2025

Walkthrough

This pull request modifies the date-time binding behavior in the connection handling. It updates the logic in the prepareBinding method to ensure that instances of DateTimeInterface are always converted to DateTimeImmutable before being used as a Timestamp, thereby preserving the original instance. Additionally, a new test case has been introduced to verify that binding a mutable Carbon instance does not alter its timezone.

Changes

File Change Summary
src/Connection.php Adds an import for DateTimeImmutable and updates the prepareBinding method to check for DateTimeImmutable. If the value is not a DateTimeImmutable, it is converted to one before creating a Timestamp.
tests/ConnectionTest.php Adds the test_binding_mutable_carbon_doesnt_change_timezone test method to ensure that a mutable Carbon instance retains its timezone after being bound to a query.

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 PHPStan (2.0.3)

/bin/bash: line 1: /vendor/bin/phpstan: No such file or directory


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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: 0

🧹 Nitpick comments (1)
tests/ConnectionTest.php (1)

635-641: LGTM! Well-designed test case.

The test effectively verifies that the timezone of a mutable Carbon instance is preserved during SQL binding. This directly validates the fix implemented in Connection.php.

Consider adding these additional test cases to strengthen the coverage:

 public function test_binding_mutable_carbon_doesnt_change_timezone(): void
 {
     $conn = $this->getDefaultConnection();
     $now = now()->setTimezone('Asia/Tokyo');
     $conn->select('SELECT ?', [$now]);
     $this->assertSame('Asia/Tokyo', $now->getTimezone()->getName());
+
+    // Test with DateTimeImmutable
+    $immutable = DateTimeImmutable::createFromMutable($now);
+    $conn->select('SELECT ?', [$immutable]);
+    $this->assertSame('Asia/Tokyo', $immutable->getTimezone()->getName());
+
+    // Test with array of date-time objects
+    $dates = [$now, $immutable];
+    $conn->select('SELECT ?', [$dates]);
+    $this->assertSame('Asia/Tokyo', $dates[0]->getTimezone()->getName());
+    $this->assertSame('Asia/Tokyo', $dates[1]->getTimezone()->getName());
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5442599 and 3cb33a2.

📒 Files selected for processing (2)
  • src/Connection.php (2 hunks)
  • tests/ConnectionTest.php (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Run-PHPUnit
🔇 Additional comments (2)
src/Connection.php (2)

28-28: LGTM!

Added import for DateTimeImmutable which is used in the updated prepareBinding method.


465-469:

✅ Verification successful

Excellent fix for preserving the original DateTime object!

The changes effectively prevent the modification of the original DateTime object by:

  1. Using the original DateTimeImmutable instance directly
  2. Creating a new DateTimeImmutable for mutable instances

This fixes the issue where Carbon's timezone was being altered by the Timestamp constructor.

Let's verify that this change doesn't introduce any regressions in date-time handling:


🏁 Script executed:

#!/bin/bash
# Description: Search for other date-time related code that might be affected by this change

# Test: Search for Timestamp usage
rg -A 5 'new Timestamp'

# Test: Search for Carbon usage
rg -A 5 'Carbon::'

Length of output: 5890


Date-Time Handling Update Verified & Approved

Our investigation confirms that the new implementation in src/Connection.php correctly preserves the original DateTime object by:

  • Cloning mutable DateTime instances via creating a new DateTimeImmutable.
  • Maintaining consistent usage of new Timestamp($value) across the codebase (see tests in tests/Query/DatetimeTest.php and related classes).
  • Preserving expected behavior in related Carbon invocations without introducing any regressions.

Great work on this fix!

@taka-oyama taka-oyama merged commit 2eb0288 into master Feb 19, 2025
1 check passed
@taka-oyama taka-oyama deleted the fix/dont-alter-carbon branch February 19, 2025 01:58
Sign up for free to join t 7794 his conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0