Skip to content

fix(postgres): default created_at and updated_at to now() so a non-UTC session does not shift them - #79

Merged
AlexeyShalaev merged 1 commit into
masterfrom
fix/timestamp-defaults-session-time-zone
Nov 12, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
fix/timestamp-defaults-session-time-zone

Conversation

@AlexeyShalaev

@AlexeyShalaev AlexeyShalaev commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

The ORM bases had the bug reported in bedrock-python/sqlalchemy-foundation-kit#88. created_at and updated_at defaulted to timezone('UTC', now()), and updated_at was also updated to it. That expression is a timestamp without a zone, and the cast into the timestamptz column reads it in the session TimeZone, so a session that is not on UTC was off by its offset. All four bases now use now(), the same function the repositories already use for locked_at and completed_at.

How it shows up in practice: the repositories pass created_at from the entity, so through them only updated_at was wrong. Every update from a non-UTC session wrote it shifted. created_at was 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.py runs SET LOCAL TIME ZONE 'Europe/Moscow' and checks two things against now() of the writing transaction:

  • the server defaults on an insert into each of the four tables;
  • updated_at after mark_processing.

All 5 cases fail on master by exactly 3 hours and pass with the fix.

Docs:

  • docs/migrations.md has 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 with compare_server_default=True, because Alembic compares the two defaults by evaluating them on the server, and in a UTC session they are equal.
  • I tried that migration on tables created with the 0.3.1 defaults, including the partitioned ones with a default partition. Altering the parent changed the partitions too, and inserts from a Moscow session were exact afterwards.
  • The agents page notes the defaults in the PostgreSQL section and links to that section.

Checks:

  • ruff format, ruff check and mypy are clean.
  • Unit tests: 810 passed, coverage 98.76%.
  • Postgres integration tests: 78 passed.
  • I couldn't run the Kafka integration tests locally, because Docker Desktop fails to pull cp-kafka:7.6.1 (a containerd ingest error). CI runs them.
  • The docs build reports no issues.

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

…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.
@AlexeyShalaev
AlexeyShalaev merged commit 54332ba into master Nov 12, 2026
6 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the fix/timestamp-defaults-session-time-zone branch November 12, 2026 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant