Files
coder/coderd/database/migrations/outcome_test.go
T
Kiryl e35a124b0d feat: report database migration outcome (#31849)
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._
2026-11-23 13:50:50 +01:00

66 lines
2.1 KiB
Go

package migrations_test
import (
"testing"
"github.com/golang-migrate/migrate/v4"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/database/migrations"
"github.com/coder/quartz"
)
// TestOutcome covers the outcome line of runs through Up that do not commit.
// TestReporter's cases cover the committed line.
func TestOutcome(t *testing.T) {
t.Parallel()
if testing.Short() {
t.SkipNow()
}
t.Run("RolledBack", func(t *testing.T) {
t.Parallel()
db := testSQLDB(t)
require.NoError(t, migrations.Up(db, migrations.WithFS(migrationFS(`CREATE TABLE a (id int PRIMARY KEY);`))))
sink := migrations.NewGoldenSink()
fsys := migrationFS(
`CREATE TABLE a (id int PRIMARY KEY);`,
`CREATE TABLE b (id int);`,
`INSERT INTO a VALUES (1), (1);`,
)
err := migrations.Up(db, testOptions(sink, quartz.NewMock(t), "test/rolled-back", fsys)...)
require.Error(t, err)
requireGolden(t, sink, "rolled_back")
// The error names the migration and gives PostgreSQL's message and
// code, but neither the SQL nor PostgreSQL's detail text, which
// contains the duplicate row's values.
require.Contains(t, err.Error(),
`up: migration 3 (step_3): migration failed: duplicate key value violates unique constraint "a_pkey" (sqlstate=23505)`)
require.NotContains(t, err.Error(), "INSERT")
require.NotContains(t, err.Error(), "Key (id)")
require.NotContains(t, err.Error(), "\n")
var pgErr *pq.Error
require.ErrorAs(t, err, &pgErr)
})
t.Run("Dirty", func(t *testing.T) {
t.Parallel()
db := testSQLDB(t)
require.NoError(t, migrations.Up(db, migrations.WithFS(migrationFS(`CREATE TABLE a (id int);`))))
_, err := db.Exec(`UPDATE schema_migrations SET dirty = true`)
require.NoError(t, err)
sink := migrations.NewGoldenSink()
fsys := migrationFS(`CREATE TABLE a (id int);`, `CREATE TABLE b (id int);`)
err = migrations.Up(db, testOptions(sink, quartz.NewMock(t), "test/dirty", fsys)...)
require.ErrorAs(t, err, new(migrate.ErrDirty))
// A dirty schema stops the run before the pending migrations are
// counted, so there is no "migration required" line.
requireGolden(t, sink, "dirty")
})
}