-
Notifications
You must be signed in to change notification settings - Fork 851
fix: rely on naive datetime for sqlalchemy #1833
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
Changes from all commits
f4dba25
b6b4a71
9a2185e
70e2c70
ea1f3cb
3bc629b
0db13c2
7aa92b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Database drivers for CI testing | ||
|
|
||
| # PostgreSQL drivers | ||
| psycopg2-binary>=2.9,<3 | ||
| asyncpg>=0.27,<1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from datetime import datetime | ||
| from typing import Optional | ||
|
|
||
|
|
||
| # TODO: Remove this function in next major release (v4.0.0) after updating all | ||
| # DateTime columns to DateTime(timezone=True). See issue #1832 for context. | ||
| def normalize_datetime_for_db(dt: Optional[datetime]) -> Optional[datetime]: | ||
| """ | ||
| Normalize timezone-aware datetime to naive UTC datetime for database storage. | ||
|
|
||
| Ensures compatibility with existing databases using TIMESTAMP WITHOUT TIME ZONE. | ||
| SQLAlchemy DateTime columns without timezone=True create naive timestamp columns | ||
| in databases like PostgreSQL. This function strips timezone information from | ||
| timezone-aware datetimes (which are already in UTC) to enable safe comparisons. | ||
|
|
||
| Args: | ||
| dt: A timezone-aware or naive datetime object, or None | ||
|
|
||
| Returns: | ||
| A naive datetime in UTC, or None if input is None | ||
|
|
||
| Example: | ||
| >>> from datetime import datetime, timezone | ||
| >>> aware_dt = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc) | ||
| >>> naive_dt = normalize_datetime_for_db(aware_dt) | ||
| >>> naive_dt.tzinfo is None | ||
| True | ||
| """ | ||
| if dt is None: | ||
| return None | ||
| if dt.tzinfo is not None: | ||
| return dt.replace(tzinfo=None) | ||
|
Comment on lines
+31
to
+32
Member
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. 🪬 question(non-blocking): Should we check that
Contributor
Author
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. So when the If we set |
||
| return dt | ||
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 new job spins up a postgrrrresql db that can then be used to run our unit tests against, it should prevent this type of issue from coming up again 🚀
I tried adding mysql as well but it was failing, I'll it to my TODOs as a follow up PR