Repository navigation
feat: report database migration outcome - #31849
Merged
Merged
Conversation
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.
ConnectToPostgres now passes a logger named "migrations" to the migration function, so coder server reports migration progress, lock waits and heartbeats while it starts. Commands that connect without migrating are unchanged. Part of ENT-237.
Most server starts have nothing to migrate. Logging that at info added a line to every start, which TestServer/SpammyLogs guards against.
…rstaeff/ent-237-migration-logging-server-integration
Name the migrations logger and its main lines in the upgrade best practices, now that coder server logs migration progress on startup.
…rstaeff/ent-237-migration-logging-server-integration
After the migrations return, ask PostgreSQL how the migration transaction ended (pg_xact_status on the transaction ID captured when it started, and the saved schema version) and log exactly one outcome line: committed, rolled back, committed before a failure, commit outcome unknown, batch lock timeout, or dirty schema. "No change" is logged at debug level so starts with nothing to migrate stay quiet. A failed migration's error now names the migration and gives PostgreSQL's short message and SQLSTATE, without the SQL or PostgreSQL's detail text, which can contain row values. The upgrade guide lists the outcome line. Part of ENT-236.
The migration lock is Coder's lock that lets one replica migrate at a time. The page's database locks are table locks held by other sessions, which blocked_by reports. Also say what steady heartbeats mean during a liveness probe restart.
… into kirstaeff/ent-236-migration-outcome # Conflicts: # docs/install/operate/upgrade/best-practices.md
… into kirstaeff/ent-236-migration-outcome # Conflicts: # docs/install/operate/upgrade/best-practices.md
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.
…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.
… and drop the reporter mutex
…artbeat and simplify the reporter
…r_internal_test.go
…ration-progress-heartbeat
…ress-heartbeat' into kirstaeff/ent-237-migration-logging-server-integration
… into kirstaeff/ent-236-migration-outcome # Conflicts: # coderd/database/migrations/migrate.go # coderd/database/migrations/reporter.go # coderd/database/migrations/reporter_test.go
spikecurtis
reviewed
Nov 18, 2026
… cover it with golden files
… store the heartbeat context
…rstaeff/ent-237-migration-logging-server-integration
… into kirstaeff/ent-236-migration-outcome
Collaborator
Author
|
Reworked on top of the new reporter from #31789 before your review landed, so some lines you commented on have moved. |
…rstaeff/ent-237-migration-logging-server-integration
… into kirstaeff/ent-236-migration-outcome
spikecurtis
approved these changes
Nov 19, 2026
jdomeracki-coder
approved these changes
Nov 20, 2026
jdomeracki-coder
left a comment
Contributor
There was a problem hiding this comment.
benign workflows change
Base automatically changed from
kirstaeff/ent-237-migration-logging-server-integration
to
main
November 23, 2026 12:10
…ration-outcome # Conflicts: # Makefile # coderd/database/migrations/migrate.go # coderd/database/migrations/reporter.go # coderd/database/migrations/testdata/reporter/heartbeat_check_fails.golden # coderd/database/migrations/testdata/reporter/late_heartbeat_discarded.golden # coderd/database/migrations/testdata/reporter/step_blocked_by_table_lock.golden # coderd/database/migrations/testdata/reporter/success.golden # coderd/database/migrations/testdata/reporter/up_to_date.golden # coderd/database/migrations/testdata/reporter/waits_for_migration_lock.golden
kirstaeff
had a problem deploying
to
docs-preview
November 23, 2026 12:50 — with
GitHub Actions
Failure
This branch had an error being deployed
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Nothing said how a migration run ended. On failure, the CLI printed the PostgreSQL error with the failing SQL, but not which migration failed or whether anything was saved.
After a failed run, the reporter asks PostgreSQL how the transaction ended (
pg_xact_statuson the captured transaction ID, plus the saved schema version). Every run logs one outcome line: committed, rolled back, committed before failure, unknown, lock timeout, dirty schema, or failed before start. Every failure line has anerrorfield.The error names the failed migration and gives PostgreSQL's short message and SQLSTATE, without the SQL or detail text, which can contain row values.
Closes ENT-236
Written with Claude Code and Coder Agents, reviewed and tested by @kirstaeff.