Skip to content

Require a created_at when creating a record with a created_at column - #557

Open
erikrozendaal wants to merge 2 commits into
masterfrom
improvement/require-created-at
Open

erikrozendaal wants to merge 2 commits into
masterfrom
improvement/require-created-at

Conversation

@erikrozendaal

Copy link
Copy Markdown
Member

A projection should be the same however often its events are replayed. Records created without a created_at broke this: the persistors filled in the current time for created_at and updated_at, so every replay gave such records other timestamps, and code that reads these timestamps as the time something happened got other answers after a replay.

Creating a record whose table has a created_at column now requires a created_at, usually the created_at of the event, which is also used as updated_at unless that is given. Without one, all persistors fail instead of using the current time. This is a breaking change for projectors that relied on the current time, described in the changelog, and goes together with #556, which keeps updated_at stable for records updated using a block.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty created_at strings bypass validation in bulk creation and replay, allowing NULL or replay-dependent timestamps instead of consistently rejecting missing values.

1 open issue
What changed in this PR

This PR makes projection creation more deterministic by requiring created_at for records whose tables have that column, rather than filling it with the current time.

Changes:

  • Adds shared validation and updates both persistors to enforce it, including timestamps supplied by creation blocks.
  • Updates timestamp expectations and existing test fixtures.
  • Documents the breaking change and updates projector examples.
File Description
spec/​lib/​sequent/​core/​persistors/​replay_optimized_postgres_persistor_spec.rb Supplies explicit timestamps in replay test fixtures.
spec/​lib/​sequent/​core/​persistors/​persistor_values_spec.rb Tests missing timestamps and block-provided timestamps.
spec/​lib/​sequent/​core/​persistors/​persistor_defaults_spec.rb Supplies timestamps while testing other column defaults.
spec/​lib/​sequent/​core/​persistors/​active_record_persistor_spec.rb Adds a timestamp to the batch-update fixture.
lib/​sequent/​core/​persistors/​replay_optimized_postgres_persistor.rb Validates creation timestamps and removes the current-time argument default.
lib/​sequent/​core/​persistors/​persistor.rb Defines the shared timestamp validator and error.
lib/​sequent/​core/​persistors/​active_record_persistor.rb Validates single and bulk creation and handles block-set timestamps.
docs/​docs/​concepts/​projector.md Explains the requirement and updates creation examples.
CHANGELOG.md Documents the breaking timestamp behavior.

Review effort: Balanced

@erikrozendaal
erikrozendaal marked this pull request as ready for review November 4, 2026 11:07
Without a created_at, the persistors used the current time for
created_at and updated_at, so a record got other timestamps every time
its events were replayed. Creating such a record without a created_at,
after the values and the block are applied, now fails with a
MissingCreatedAtError, in the ActiveRecordPersistor and the
ReplayOptimizedPostgresPersistor alike. A created_at set by the block is
also used as updated_at, like one given in the values.
create_or_update_record no longer defaults its created_at to the
current time.
create_records and the ReplayOptimizedPostgresPersistor checked the
given value, so created_at: '' passed and stored NULL or the database
default. The check now uses the value cast to the type of the column,
like the ActiveRecordPersistor's create_record effectively did.
@erikrozendaal
erikrozendaal force-pushed the improvement/require-created-at branch from 1a60d97 to ccd1592 Compare November 4, 2026 18:57

This branch has not been deployed

No deployments
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.

2 participants