Skip to content

fix: use timezone-aware datetimes (fixes #34)#37

Merged
doganarif merged 1 commit into
mainfrom
fix/timezone-aware-datetimes
Nov 11, 2025
Merged

fix: use timezone-aware datetimes (fixes #34)#37
doganarif merged 1 commit into
mainfrom
fix/timezone-aware-datetimes

Conversation

@doganarif
Copy link
Copy Markdown
Owner

@doganarif doganarif commented Nov 11, 2025

Summary

Fixes #34 - All DateTime columns now use timezone-aware datetimes to ensure correct timestamp handling across different timezones.

Changes

  • Updated all SQLAlchemy DateTime columns to use DateTime(timezone=True)
  • This ensures timestamps are stored with timezone information in the database
  • All datetime values are already using datetime.now(timezone.utc) for UTC timestamps

Fix Details

The issue was that while Python code was creating timezone-aware datetime objects using datetime.now(timezone.utc), SQLAlchemy's DateTime columns were not configured to preserve timezone information in the database. This caused timestamps to be stored as naive datetimes, leading to incorrect time display.

By adding timezone=True to all DateTime columns:

  • PostgreSQL will use TIMESTAMPTZ (timestamp with timezone)
  • SQLite will store ISO format strings with timezone info
  • MySQL will handle timezone conversion appropriately

Test plan

  • All existing tests pass
  • Type checking (mypy) passes
  • Linting (flake8) passes
  • Built and published to PyPI (v0.3.3)

Breaking Changes

None - this is backward compatible as the change affects how data is stored, not the API.

Summary by Sourcery

Use timezone-aware datetimes in all SQLAlchemy models to fix issues with naive timestamps

Bug Fixes:

  • Configure all DateTime columns with timezone=True to preserve timezone information in the database

Build:

  • Bump package version to 0.3.3

Summary by cubic

Make all database timestamps timezone-aware to fix incorrect time display across timezones (fixes #34). Set SQLAlchemy DateTime columns to DateTime(timezone=True) across models, keep UTC via datetime.now(timezone.utc), and bump the package version to 0.3.3.

Written for commit 1a6ba7a. Summary will update automatically on new commits.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Nov 11, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR configures all SQLAlchemy DateTime columns to be timezone-aware by specifying timezone=True, ensuring consistent UTC timestamp handling across supported databases, and updates the package version to 0.3.3.

Entity Relationship diagram for updated DateTime columns with timezone awareness

erDiagram
  CapturedRequest {
    Float duration_ms
    String client_ip
    DateTime created_at_timezone_aware
  }
  CapturedQuery {
    Integer rows_affected
    String connection_name
    DateTime created_at_timezone_aware
  }
  CapturedException {
    Text exception_value
    Text traceback
    DateTime created_at_timezone_aware
  }
  Trace {
    String service_name
    String operation_name
    DateTime start_time_timezone_aware
    DateTime end_time_timezone_aware
    Float duration_ms
    Integer span_count
    String status
    JSON tags
    DateTime created_at_timezone_aware
  }
  Span {
    String operation_name
    String service_name
    String span_kind
    DateTime start_time_timezone_aware
    DateTime end_time_timezone_aware
    Float duration_ms
    String status
    JSON tags
    JSON logs
    DateTime created_at_timezone_aware
  }
  SpanRelation {
    String parent_span_id
    String child_span_id
    Integer depth
    DateTime created_at_timezone_aware
  }
  BackgroundTask {
    String status
    DateTime start_time_timezone_aware
    DateTime end_time_timezone_aware
    Float duration_ms
    Text error
    DateTime created_at_timezone_aware
  }
Loading

Class diagram for updated models with timezone-aware DateTime columns

classDiagram
  class CapturedRequest {
    +duration_ms: float
    +client_ip: str
    +created_at: DateTime(timezone=True)
  }
  class CapturedQuery {
    +rows_affected: int
    +connection_name: str
    +created_at: DateTime(timezone=True)
  }
  class CapturedException {
    +exception_value: str
    +traceback: str
    +created_at: DateTime(timezone=True)
  }
  class Trace {
    +service_name: str
    +operation_name: str
    +start_time: DateTime(timezone=True)
    +end_time: DateTime(timezone=True)
    +duration_ms: float
    +span_count: int
    +status: str
    +tags: dict
    +created_at: DateTime(timezone=True)
  }
  class Span {
    +operation_name: str
    +service_name: str
    +span_kind: str
    +start_time: DateTime(timezone=True)
    +end_time: DateTime(timezone=True)
    +duration_ms: float
    +status: str
    +tags: dict
    +logs: dict
    +created_at: DateTime(timezone=True)
  }
  class SpanRelation {
    +parent_span_id: str
    +child_span_id: str
    +depth: int
    +created_at: DateTime(timezone=True)
  }
  class BackgroundTask {
    +status: str
    +start_time: DateTime(timezone=True)
    +end_time: DateTime(timezone=True)
    +duration_ms: float
    +error: str
    +created_at: DateTime(timezone=True)
  }
Loading

File-Level Changes

Change Details Files
Enable timezone-aware datetimes in SQLAlchemy models
  • Add timezone=True to created_at columns
  • Add timezone=True to start_time and end_time columns
  • Retain default datetime.now(timezone.utc) for UTC timestamps
fastapi_radar/models.py
Bump package version to 0.3.3
  • Update version in init.py
  • Update version in pyproject.toml
  • Update version in setup.py
fastapi_radar/__init__.py
pyproject.toml
setup.py

Assessment against linked issues

Issue Objective Addressed Explanation
#34 Ensure all datetime values stored in the database are timezone-aware to prevent incorrect time display.
#34 Update SQLAlchemy DateTime columns to use timezone information (i.e., DateTime(timezone=True)).

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@doganarif doganarif merged commit 0655c53 into main Nov 11, 2025
5 checks passed
@doganarif doganarif deleted the fix/timezone-aware-datetimes branch November 11, 2025 01:45
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 5 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="fastapi_radar/models.py">

<violation number="1" location="fastapi_radar/models.py:44">
The change to `DateTime(timezone=True)` does not work for the MySQL backend, contrary to the PR description. The SQLAlchemy documentation confirms the `timezone` flag is ignored by the MySQL dialect, meaning the original bug of storing naive datetimes will persist on MySQL deployments.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

Comment thread fastapi_radar/models.py
client_ip = Column(String(50))
created_at = Column(
DateTime, default=lambda: datetime.now(timezone.utc), index=True
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), index=True
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot Nov 11, 2025

Choose a reason for hiding this comment

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

The change to DateTime(timezone=True) does not work for the MySQL backend, contrary to the PR description. The SQLAlchemy documentation confirms the timezone flag is ignored by the MySQL dialect, meaning the original bug of storing naive datetimes will persist on MySQL deployments.

Prompt for AI agents
Address the following comment on fastapi_radar/models.py at line 44:

<comment>The change to `DateTime(timezone=True)` does not work for the MySQL backend, contrary to the PR description. The SQLAlchemy documentation confirms the `timezone` flag is ignored by the MySQL dialect, meaning the original bug of storing naive datetimes will persist on MySQL deployments.</comment>

<file context>
@@ -41,7 +41,7 @@ class CapturedRequest(Base):
     client_ip = Column(String(50))
     created_at = Column(
-        DateTime, default=lambda: datetime.now(timezone.utc), index=True
+        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), index=True
     )
 
</file context>
Fix with Cubic

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.

Missing Timezone in datetimes

1 participant