Repository navigation
fix: default created_at and updated_at to now() so a non-UTC session does not shift them - #89
Merged
Conversation
…does not shift them
timezone('UTC', now()) is a timestamp without zone, and the cast into the timestamptz column reads it in the session TimeZone. In a Europe/Moscow session both columns ended up 3 hours in the past.
Closes #88
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #88.
DatetimeColumnsMixinnow usesnow()for thecreated_atandupdated_atserver defaults and for theupdated_atonupdate.timezone('UTC', now())is atimestampwithout a zone, and the cast into thetimestamptzcolumn reads it in the sessionTimeZone, so in a non-UTC session every value was off by that zone's offset.The new integration test sets
SET LOCAL TIME ZONE 'Europe/Moscow'and checks that both columns equalnow()of the writing transaction, once on insert and once on update. On master both cases fail by exactly 3 hours (18:38:04stored,21:38:04expected). The unit tests now check that the defaults render asnow().The upgrade needs a hand-written migration, and the issue's last paragraph got that wrong. The model change fixes
onupdateat once, because SQLAlchemy sends it with each UPDATE. A table created on 0.5.1 or earlier keepsDEFAULT timezone('UTC'::text, now())in the database, though. Autogenerate does not pick that up even withcompare_server_default=True. On PostgreSQL, Alembic compares two defaults by runningSELECT <db default> = <model default>on the server, and in a UTC session the two are equal. I checked this on a table created with the 0.5.1 DDL:compare_metadatareturned[]with the flag off and with it on. Afterop.alter_column(..., server_default=sa.text("now()"))an insert from a Moscow session was exact.The quick start now explains the default and gives that migration. The agents page has the new defaults in the Base ORM table and a row under "Fixed since 0.2.0" with the workaround for older versions: set the session time zone to UTC.
Checks:
ruff format,ruff check,mypyclean. The full suite has 667 passed, coverage 94.24%. The docs build reports no issues.BEGIN_COMMIT_OVERRIDE
fix: default created_at and updated_at to now() so a non-UTC session does not shift them (#89)
END_COMMIT_OVERRIDE