-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: Strip timezone for PostgreSQL timestamps in DatabaseSessionService #4365
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
Closed
filipecaixeta
wants to merge
3
commits into
google:main
from
filipecaixeta:fix-postgresql-timestamp-timezone
+42
−1
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,47 @@ def fake_create_async_engine(_db_url: str, **kwargs): | |
| assert captured_kwargs.get('pool_pre_ping') is True | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('dialect_name', ['sqlite', 'postgresql']) | ||
| def test_database_session_service_strips_timezone_for_dialect(dialect_name): | ||
| """Verifies that timezone-aware datetimes are converted to naive datetimes | ||
| for SQLite and PostgreSQL to avoid 'can't subtract offset-naive and | ||
| offset-aware datetimes' errors. | ||
|
|
||
| PostgreSQL's default TIMESTAMP type is WITHOUT TIME ZONE, which cannot | ||
| accept timezone-aware datetime objects when using asyncpg. SQLite also | ||
| requires naive datetimes. | ||
| """ | ||
| # Simulate the logic in create_session | ||
| is_sqlite = dialect_name == 'sqlite' | ||
| is_postgres = dialect_name == 'postgresql' | ||
|
|
||
| now = datetime.now(timezone.utc) | ||
| assert now.tzinfo is not None # Starts with timezone | ||
|
|
||
| if is_sqlite or is_postgres: | ||
| now = now.replace(tzinfo=None) | ||
|
|
||
| # Both SQLite and PostgreSQL should have timezone stripped | ||
| assert now.tzinfo is None | ||
|
|
||
|
|
||
| def test_database_session_service_preserves_timezone_for_other_dialects(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| """Verifies that timezone info is preserved for dialects that support it.""" | ||
| # For dialects like MySQL with explicit timezone support, we don't strip | ||
| dialect_name = 'mysql' | ||
| is_sqlite = dialect_name == 'sqlite' | ||
| is_postgres = dialect_name == 'postgresql' | ||
|
|
||
| now = datetime.now(timezone.utc) | ||
| assert now.tzinfo is not None | ||
|
|
||
| if is_sqlite or is_postgres: | ||
| now = now.replace(tzinfo=None) | ||
|
|
||
| # MySQL should preserve timezone (if the column type supports it) | ||
| assert now.tzinfo is not None | ||
|
|
||
|
|
||
| def test_database_session_service_respects_pool_pre_ping_override(): | ||
| captured_kwargs = {} | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test currently simulates the logic within
create_sessionrather than testing the method's behavior directly. This makes the test brittle, as it's decoupled from the actual implementation. It would be more robust to test theDatabaseSessionService.create_sessionmethod by mocking the database dependencies and asserting that a naive datetime is used for the specified dialects.