Repository navigation
Conversation
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.
… 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
… into kirstaeff/ent-236-migration-outcome # Conflicts: # coderd/database/migrations/migrate.go # coderd/database/migrations/reporter.go # coderd/database/migrations/reporter_test.go
| case outcomeCommitted: | ||
| fields := []slog.Field{slog.F("status", "success"), count} | ||
| fields = append(fields, versionField("from_version", res.FromVersion)...) | ||
| fields = append(fields, versionField("to_version", res.SchemaVersion)...) |
There was a problem hiding this comment.
These field names are inconsistent and confusing. Sometimes this is called "schema_version" sometimes "to_version". We should always call it the same thing. "schema_version" is imprecise because all these versions are versions of the schema. How about "final_version"
| // migrations. | ||
| type runResult struct { | ||
| StartedAt time.Time | ||
| FinishedAt time.Time |
There was a problem hiding this comment.
Logs automatically come with timestamps. Why do we need to separately have timestamps in this result struct?
| SchemaVersion int | ||
| // Applied lists the migrations that ran in the migration transaction. | ||
| // They are only saved if it committed. | ||
| Applied []appliedMigration |
There was a problem hiding this comment.
We only ever log the count of these. We don't need to store all this extra information about them
| } | ||
| fields = append(fields, versionField("schema_version", res.SchemaVersion)...) | ||
| if res.Failed != nil && res.Failed.pgErr != nil { | ||
| fields = append(fields, slog.F("sqlstate", string(res.Failed.pgErr.Code))) |
There was a problem hiding this comment.
You don't have to explicitly cast field values, i.e. here string() is redundant. slog uses reflection to figure out what to do with them.
| slog.F("status", "failure"), count, slog.F("schema_version", dirty.Version), total, dirtyErr) | ||
|
|
||
| case outcomeFailedBeforeStart: | ||
| // The error is reported by the caller, as without reporting. |
There was a problem hiding this comment.
The error is reported by the caller in all error cases, why is this one special in that we don't log a report?
| case outcomeCommittedBeforeFailure: | ||
| fields := []slog.Field{slog.F("status", "failure"), count} | ||
| fields = append(fields, versionField("from_version", res.FromVersion)...) | ||
| fields = append(fields, versionField("to_version", res.SchemaVersion)...) |
There was a problem hiding this comment.
why don't we include the error here?
| if res.Failed != nil { | ||
| fields = append(fields, slog.F("failed_duration_ms", res.Failed.duration.Milliseconds())) | ||
| } | ||
| r.logger.Error(ctx, "migration failed, transaction rolled back, no changes saved", fields...) |
There was a problem hiding this comment.
why don't we include the err here?
| require.Equal(t, "up: timeout: can't acquire database lock", err.Error()) | ||
| require.Equal(t, outcomeLockTimeout, res.Outcome) | ||
|
|
||
| line := sink.Entries(func(e slog.SinkEntry) bool { |
There was a problem hiding this comment.
these tests should be converted to golden files
| t.SkipNow() | ||
| } | ||
|
|
||
| t.Run("Committed", func(t *testing.T) { |
There was a problem hiding this comment.
These tests should also be converted to golden files.
… cover it with golden files
… into kirstaeff/ent-236-migration-outcome
|
Reworked on top of the new reporter from #31789 before your review landed, so some lines you commented on have moved. |
Migration progress is logged since the previous PRs in this stack, but nothing says how a run ended. On failure, the CLI prints the PostgreSQL error together with the failing SQL, without saying which migration failed or whether anything was saved.
After the migrations return, the reporter now asks PostgreSQL how the migration transaction ended (
pg_xact_statuson the transaction ID captured when it started, plus the saved schema version) and logs exactly one outcome line: committed, rolled back, committed before a failure, commit outcome unknown, batch lock timeout, or dirty schema. Asking PostgreSQL instead of trusting the Go error tells a commit that was saved apart from one that was not, for example when the connection breaks at the wrong moment.When a migration fails, the error names it and gives PostgreSQL's short message and SQLSTATE. It no longer includes the SQL or PostgreSQL's detail text, which can contain row values.
Output from upgrading a v2.37.0 database (schema 585, 54 pending migrations) on a disposable PostgreSQL. A successful run, with another session holding a lock on
templates(steps 2 to 54 omitted):A failing run, where the column migration 586 adds already exists:
Differences from RFC 1:
version,name).Resulttype. It can be exported when upgrade telemetry needs it.Closes ENT-236
This PR was written with Claude Code. I reviewed every change and ran the upgrades above myself, including a stop request during an upgrade, which still committed fully.