Skip to content

feat: log migration progress and heartbeat - #31789

Merged
kirstaeff merged 16 commits into
mainfrom
kirstaeff/ent-235-migration-progress-heartbeat
Nov 23, 2026
Merged

kirstaeff merged 16 commits into
mainfrom
kirstaeff/ent-235-migration-progress-heartbeat

Conversation

@kirstaeff

@kirstaeff kirstaeff commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Migrations run at server startup with no output, so admins can't tell which migration is running, whether it waits on a lock, or how far it got.

This adds a reporter to the migration driver. It logs:

  • the pending migrations, and each step with its position and duration;
  • the migration lock (debug level);
  • a heartbeat every 10s during a lock wait or a long step, with the connection state from pg_stat_activity and the blocking sessions.

The migration transaction is named coder-migrate/<version>, so other instances and admins can see it. Heartbeat queries run on their own connection and never delay the migration.

API: Up(db, opts...), with WithFS (replaces UpWithFS) and WithLogger. Without a logger, nothing is logged. Server startup turns it on in #31790.

[info]  database migration required  from_version=1  target_version=3  pending=2
[info]  applying database migration  version=3  name=step_3  position=2  total=2
[info]  still applying database migration  elapsed_ms=1000  state=waiting_on_lock  wait_event=relation  blocked_by=[blocker]  version=3  name=step_3  position=2  total=2
[info]  database migration applied, pending commit  version=3  name=step_3  position=2  total=2  duration_ms=1000

Tests compare log output with golden files in coderd/database/migrations/testdata/reporter.

Closes ENT-235

Written with Claude Code and Coder Agents, reviewed and tested by @kirstaeff.

Database migrations run during server startup without any progress
output. Add an optional reporter to the migration driver that logs
which migration is running, its position among the pending ones, how
long each one took, waits for the migration lock, and a heartbeat every
10 seconds read from pg_stat_activity, naming the blocking session when
the migration waits on a lock.

Up now takes options: WithFS replaces UpWithFS and WithLogger enables
reporting. Without WithLogger, migrations run the same queries and
return the same errors as before. ConnectToPostgres takes the new Up
signature; server startup does not pass a logger yet.

Part of ENT-235.
@kirstaeff
kirstaeff added this pull request to stack #31791 November 5, 2026 15:35
@linear-code

linear-code Bot commented Nov 5, 2026

Copy link
Copy Markdown

ENT-235

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@kirstaeff kirstaeff changed the title kirstaeff/ent 235 migration progress heartbeat feat: log migration progress and heartbeat Nov 5, 2026
@kirstaeff

Copy link
Copy Markdown
Collaborator Author

I have read the CLA Document and I hereby sign the CLA

cdrci2 added a commit to coder/cla that referenced this pull request Nov 5, 2026
Most server starts have nothing to migrate. Logging that at info added
a line to every start, which TestServer/SpammyLogs guards against.

@spikecurtis spikecurtis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I haven't reviewed all the tests, but I suspect they'll change anyway if you take my suggestions.

Comment thread coderd/database/migrations/reporter.go Resolved
Comment thread coderd/database/migrations/reporter.go Resolved
Comment thread coderd/database/migrations/reporter.go Resolved
Comment thread coderd/database/migrations/reporter.go Resolved
Comment thread coderd/database/migrations/reporter.go Resolved
Comment thread coderd/database/migrations/reporter.go Resolved
@spikecurtis

Copy link
Copy Markdown
Collaborator

I had another thought about the tests. All the inline assertions about the logs are very verbose and difficult to read.

Maybe what we do instead is the golden file pattern. You'll need to create a test-only sink that emits the logs without test-time variable information, like the timestamp, but still outputs a human-readable format. Then we store the expected output in a golden file we check in and do a straight byte comparison to assert the outputs. We have a make target for other tests that regenerate the golden files, so its easy to make changes to the logging code.

Without WithLogger the reporter logs to a zero slog.Logger, so every
caller runs the same code path and no method needs a nil check.
…e blocking call

Lock now runs the same statements on every run: name the connection,
identify the backend, then a single pg_advisory_xact_lock. The try-lock
only decided whether to log a wait; the heartbeat already reports that.
Acquiring and acquired are logged at debug so a normal start stays quiet.
…der a run context

Every heartbeat derives its context from a run context that close
cancels, so a heartbeat started before or after close is stopped and no
closed flag is needed. A tick re-checks its context and emits under r.mu,
and stop cancels under r.mu, so no line is written after stop returns
without ever waiting for an in-flight pg_stat_activity query.
…tructured fields

Messages no longer embed the step position, so lines of one kind can be
filtered. blocked_by is logged as the PID array, and a NULL wait type is
treated as running.
The reporter tests write logs through a sink without timestamps, with
backend PIDs replaced by names, and compare the output with golden files
in coderd/database/migrations/testdata/reporter. make gen/golden-files
regenerates them.
…cal to Lock

lockWaitStarted returns the heartbeat's stop function and start time, so
Lock stops the heartbeat it started and the reporter no longer stores it.
Also shorten the reporter and WithLogger doc comments.
@kirstaeff
kirstaeff temporarily deployed to main November 13, 2026 11:07 — with GitHub Actions Inactive
@kirstaeff
kirstaeff temporarily deployed to main November 13, 2026 11:11 — with GitHub Actions Inactive

@spikecurtis spikecurtis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Golden files for the tests worked out really well, I think.

I still think the concurrency in the reporter is hard to follow and verify, comment inline.

Comment thread coderd/database/migrations/reporter.go Resolved
…ves on done

FreshInstall duplicated StepBlockedByTableLock; WaitsForMigrationLock
and HeartbeatCheckFails already cover a run from an empty schema.
RequireReceive fails the test if done is ever closed, where TryReceive
would return a nil error.

@spikecurtis spikecurtis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some small nits inline, but I don't need to review again before merging.

Comment thread coderd/database/migrations/reporter.go Resolved
Comment thread coderd/database/migrations/reporter.go Resolved
Comment thread coderd/database/migrations/reporter.go Resolved
@kirstaeff
kirstaeff merged commit 6a45e8c into main Nov 23, 2026
36 checks passed
@kirstaeff
kirstaeff deleted the kirstaeff/ent-235-migration-progress-heartbeat branch November 23, 2026 11:52
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 23, 2026

This branch had an error being deployed

1 failed and 1 inactive deployments
docs-preview — 01607dd3 Deployed Nov 23, 2026 by kirstaeff via cleanup #339
main — 01607dd3 Deployed Nov 20, 2026 by kirstaeff via cla #261001
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants