Skip to content

feat: report database migration outcome - #31849

Merged
kirstaeff merged 34 commits into
mainfrom
kirstaeff/ent-236-migration-outcome
Nov 23, 2026
Merged

kirstaeff merged 34 commits into
mainfrom
kirstaeff/ent-236-migration-outcome

Conversation

@kirstaeff

@kirstaeff kirstaeff commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

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_status on 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 an error field.

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.

[info]  committed database migrations  status=success  count=2  from_version=1  final_version=3  transaction_status=committed  total_duration_ms=0
[erro]  migration failed, transaction rolled back, no changes saved  status=failure  count=1  version=3  name=step_3  final_version=1  sqlstate=23505  transaction_status=aborted  total_duration_ms=0  failed_duration_ms=0  error="migration 3 (step_3): migration failed: duplicate key value violates unique constraint \"a_pkey\" (sqlstate=23505)"

Closes ENT-236

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.
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.
@kirstaeff
kirstaeff added this pull request to stack #31791 November 6, 2026 15:59
@linear-code

linear-code Bot commented Nov 6, 2026

Copy link
Copy Markdown

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
Reverts 66acd2f and 1620f5f. The upgrade guide changes for
migration progress logs will follow in a separate pull request.
… 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.
…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
Comment thread coderd/database/migrations/outcome.go Resolved
Comment thread coderd/database/migrations/outcome.go Resolved
Comment thread coderd/database/migrations/outcome.go Resolved
Comment thread coderd/database/migrations/outcome.go Resolved
Comment thread coderd/database/migrations/outcome.go Resolved
Comment thread coderd/database/migrations/outcome.go Resolved
Comment thread coderd/database/migrations/outcome.go Resolved
@kirstaeff
kirstaeff requested a review from a team November 19, 2026 08:02
@kirstaeff

Copy link
Copy Markdown
Collaborator Author

Reworked on top of the new reporter from #31789 before your review landed, so some lines you commented on have moved.

@jdomeracki-coder jdomeracki-coder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
kirstaeff merged commit e35a124 into main Nov 23, 2026
29 checks passed
@kirstaeff
kirstaeff deleted the kirstaeff/ent-236-migration-outcome branch November 23, 2026 12:50
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 23, 2026

This branch had an error being deployed

1 failed deployment
docs-preview — 086bd067 Deployed Nov 23, 2026 by kirstaeff via cleanup #342
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.

3 participants