Repository navigation
fix(database): use naive datetime to match DB column type #4
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
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -81,8 +81,8 @@ class ModbusDevice(SQLModel, table=True): | |||||||||
| max_retries: int = Field(default=5) | ||||||||||
| retry_delay: float = Field(default=0.1) | ||||||||||
| is_active: bool = Field(default=True, index=True) | ||||||||||
| created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||||||||||
| updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||||||||||
| created_at: datetime = Field(default_factory=lambda: datetime.now()) | ||||||||||
| updated_at: datetime = Field(default_factory=lambda: datetime.now()) | ||||||||||
|
Comment on lines
+84
to
+85
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. 🧩 Analysis chain🏁 Script executed: # First, check the file exists and read the relevant lines with context
cat -n app/database/models.py | head -100Repository: Jonathan0823/nexusbus Length of output: 4110 🏁 Script executed: # Check git history for changes to these lines
git log --oneline -n 20 -- app/database/models.pyRepository: Jonathan0823/nexusbus Length of output: 130 🏁 Script executed: # Get the diff of the most recent changes to see what changed
git diff HEAD~1 HEAD -- app/database/models.py || git show HEAD:app/database/models.py | head -200Repository: Jonathan0823/nexusbus Length of output: 6897 🏁 Script executed: # Search for database column definitions and response models
rg -n "TIMESTAMP|WITHOUT TIME ZONE|ModbusDeviceResponse|PollingTargetResponse" app/database/Repository: Jonathan0823/nexusbus Length of output: 195 🏁 Script executed: # Check the previous commit to see what the original implementation was
git show 0afd866 | head -150Repository: Jonathan0823/nexusbus Length of output: 3409 🏁 Script executed: # Look for database schema/migrations to understand column types
find . -type f -name "*.sql" -o -name "*migration*" -o -name "*schema*" | head -20Repository: Jonathan0823/nexusbus Length of output: 94 🏁 Script executed: # Check if there are any alembic migrations or database config
fd -e py -e sql | grep -E "(migration|alembic|schema)" | head -20Repository: Jonathan0823/nexusbus Length of output: 231 🏁 Script executed: # Search for where created_at/updated_at might be used to understand impact
rg "created_at|updated_at" -B 2 -A 2 app/ --max-count=30Repository: Jonathan0823/nexusbus Length of output: 2760 🏁 Script executed: # Check the migration files to understand database column definitions
cat migrations/001_initial_setup.py | head -100Repository: Jonathan0823/nexusbus Length of output: 2429 🏁 Script executed: # Check the CRUD operations to see how updated_at is being used
cat app/database/crud.py | head -80Repository: Jonathan0823/nexusbus Length of output: 2426 🏁 Script executed: # Check if there's any documentation about timestamp expectations
find . -type f -name "*.md" | xargs grep -l "timestamp\|timezone\|utc" 2>/dev/null | head -5Repository: Jonathan0823/nexusbus Length of output: 209 🏁 Script executed: # Look for any tests that might clarify expected behavior
find tests -name "*.py" 2>/dev/null | xargs grep -l "created_at\|updated_at" 2>/dev/null | head -3Repository: Jonathan0823/nexusbus Length of output: 47 🏁 Script executed: # Search for the database schema definition to confirm TIMESTAMP WITHOUT TIME ZONE
rg "TIMESTAMP" . --type sql --type pyRepository: Jonathan0823/nexusbus Length of output: 47 🏁 Script executed: # Check if there's any existing helper function for datetime handling
rg "utc|UTC|timezone" app/database/ -B 2 -A 2Repository: Jonathan0823/nexusbus Length of output: 291 🏁 Script executed: # Verify the imports at the top of models.py to understand what's available
head -10 app/database/models.pyRepository: Jonathan0823/nexusbus Length of output: 295 🏁 Script executed: # Check app/database/crud.py to see all uses of datetime.now() that were changed
rg "datetime\.now\(\)" app/database/crud.py -B 2 -A 2Repository: Jonathan0823/nexusbus Length of output: 899 🏁 Script executed: # Look at the full git commit to see the exact change from UTC to local time
git show 0afd866 app/database/models.py | head -80Repository: Jonathan0823/nexusbus Length of output: 2719 🏁 Script executed: # Check if there are any tests that verify timestamp behavior
find . -name "*.py" -path "*/test*" -exec grep -l "datetime\|timestamp" {} \;Repository: Jonathan0823/nexusbus Length of output: 91 🏁 Script executed: # Verify the database schema files to understand TIMESTAMP WITHOUT TIME ZONE usage
find . -name "*.py" -path "*migration*" -exec grep -l "TIMESTAMP" {} \;Repository: Jonathan0823/nexusbus Length of output: 47 🏁 Script executed: # Check the test file that references datetime
cat tests/unit/test_cache.pyRepository: Jonathan0823/nexusbus Length of output: 6101 🏁 Script executed: # Look for any datetime serialization or comparison in the codebase
rg "strftime|isoformat|fromisoformat" app/ --type pyRepository: Jonathan0823/nexusbus Length of output: 654 🏁 Script executed: # Check if there's any timezone-related configuration in the app
rg "TZ|timezone|UTC|utc" app/ --type py -B 1 -A 1 | head -50Repository: Jonathan0823/nexusbus Length of output: 2367 🏁 Script executed: # Check if there are any other places where datetime is created for the database
rg "datetime\.now\(\)" app/ --type py -B 2 -A 2Repository: Jonathan0823/nexusbus Length of output: 2328 🏁 Script executed: # Verify the database schema definition in migrations
cat migrations/001_initial_setup.py | grep -A 50 "modbus_devices"Repository: Jonathan0823/nexusbus Length of output: 2042 🏁 Script executed: # Check cache.py to see how it properly uses UTC
cat app/core/cache.py | head -50Repository: Jonathan0823/nexusbus Length of output: 1570 🏁 Script executed: # Verify the actual database schema to confirm TIMESTAMP WITHOUT TIME ZONE
cat migrations/base.py | head -80Repository: Jonathan0823/nexusbus Length of output: 2503 🏁 Script executed: # Check if there's any SQLAlchemy type definition for the datetime columns
rg "DateTime\|TIMESTAMP" migrations/ app/ --type py -B 2 -A 2Repository: Jonathan0823/nexusbus Length of output: 47 🏁 Script executed: # Check the complete CRUD file to understand the full context
wc -l app/database/crud.pyRepository: Jonathan0823/nexusbus Length of output: 89 🏁 Script executed: # Search for any datetime comparisons that might be causing the asyncpg error
rg "created_at|updated_at" app/database/crud.py -B 3 -A 3Repository: Jonathan0823/nexusbus Length of output: 1275 🏁 Script executed: # Get a clearer picture: count total occurrences of datetime.now() without timezone in database code
rg "datetime\.now\(\)" app/database/ --type py | wc -lRepository: Jonathan0823/nexusbus Length of output: 67 🏁 Script executed: # Double-check: are there any database schema definitions showing TIMESTAMP WITHOUT TIME ZONE explicitly?
find . -type f \( -name "*.sql" -o -name "*.py" \) -exec grep -l "TIMESTAMP WITHOUT TIME ZONE\|without time zone" {} \;Repository: Jonathan0823/nexusbus Length of output: 47 Use UTC-naive timestamps, not local-naive, to preserve correct timestamp semantics.
Suggested fix from datetime import datetime, timezone
from typing import Optional, Tuple
VALID_FRAMERS: Tuple[str, ...] = ("RTU", "SOCKET", "ASCII")
VALID_REGISTER_TYPES: Tuple[str, ...] = ("holding", "input", "coil", "discrete")
+
+
+def utc_now_naive() -> datetime:
+ """Return the current UTC time without timezone info for TIMESTAMP WITHOUT TIME ZONE columns."""
+ return datetime.now(timezone.utc).replace(tzinfo=None)
class ModbusDevice(SQLModel, table=True):
...
- created_at: datetime = Field(default_factory=lambda: datetime.now())
- updated_at: datetime = Field(default_factory=lambda: datetime.now())
+ created_at: datetime = Field(default_factory=utc_now_naive)
+ updated_at: datetime = Field(default_factory=utc_now_naive)
class PollingTarget(SQLModel, table=True):
...
- created_at: datetime = Field(default_factory=lambda: datetime.now())
- updated_at: datetime = Field(default_factory=lambda: datetime.now())
+ created_at: datetime = Field(default_factory=utc_now_naive)
+ updated_at: datetime = Field(default_factory=utc_now_naive)Also update -device.updated_at = datetime.now()
+device.updated_at = utc_now_naive()📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
|
|
||||||||||
|
|
||||||||||
| # Pydantic schemas for API | ||||||||||
|
|
@@ -151,8 +151,8 @@ class PollingTarget(SQLModel, table=True): | |||||||||
| count: int = Field(default=1, ge=1, le=125) | ||||||||||
| is_active: bool = Field(default=True, index=True) | ||||||||||
| description: Optional[str] = Field(default=None, max_length=200) | ||||||||||
| created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||||||||||
| updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||||||||||
| created_at: datetime = Field(default_factory=lambda: datetime.now()) | ||||||||||
| updated_at: datetime = Field(default_factory=lambda: datetime.now()) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| # Pydantic schemas for Polling API | ||||||||||
|
|
||||||||||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 519
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 416
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 973
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 89
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 6901
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 10021
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 91
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 448
🏁 Script executed:
Repository: Jonathan0823/nexusbus
Length of output: 291
The suggested helper function
utc_now_naive()does not exist in the codebase and must be created first.The concern about
datetime.now()using server-local time is valid and needs fixing. However, the suggested fix is incomplete—it references a helper that hasn't been defined yet and doesn't address the same issue inapp/database/models.pywherecreated_atandupdated_atdefaults also usedatetime.now().To fix this properly:
utc_now_naive()helper toapp/database/models.pythat returnsdatetime.now(timezone.utc).replace(tzinfo=None)(or equivalent)🤖 Prompt for AI Agents