Repository navigation
fix(postgres): default created_at and updated_at to now() so a non-UTC session does not shift them - #79
Merged
Conversation
…C session 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. Same bug as bedrock-python/sqlalchemy-foundation-kit#88.
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.
The ORM bases had the bug reported in bedrock-python/sqlalchemy-foundation-kit#88.
created_atandupdated_atdefaulted totimezone('UTC', now()), andupdated_atwas also updated to it. That expression is atimestampwithout a zone, and the cast into thetimestamptzcolumn reads it in the sessionTimeZone, so a session that is not on UTC was off by its offset. All four bases now usenow(), the same function the repositories already use forlocked_atandcompleted_at.How it shows up in practice: the repositories pass
created_atfrom the entity, so through them onlyupdated_atwas wrong. Every update from a non-UTC session wrote it shifted.created_atwas wrong only for rows that other code inserted without it, and on the partitioned tables that column is the partition key.tests/integration/postgres/test_timestamps.pyrunsSET LOCAL TIME ZONE 'Europe/Moscow'and checks two things againstnow()of the writing transaction:updated_ataftermark_processing.All 5 cases fail on master by exactly 3 hours and pass with the fix.
Docs:
docs/migrations.mdhas the new defaults in the DDL and in the Alembic example, and a "Timestamp defaults" section with the migration for existing tables. It has to be written by hand: autogenerate does not see the change even withcompare_server_default=True, because Alembic compares the two defaults by evaluating them on the server, and in a UTC session they are equal.Checks:
ruff format,ruff checkandmypyare clean.cp-kafka:7.6.1(a containerd ingest error). CI runs them.BEGIN_COMMIT_OVERRIDE
fix(postgres): default created_at and updated_at to now() so a non-UTC session does not shift them (#79)
END_COMMIT_OVERRIDE