Files
coder/coderd/database/migrations
Michael Suchacz 26524addda test(coderd/database/migrations): step one migration chain for per-migration tests (#29460)
`coderd/database/migrations` was one of the slowest packages in
`test-go-pg` (about 120s at 92% slot utilization). Most of that was
fixed per-test overhead: `testSQLDB` cloned the migrated template
database and then dropped its whole schema, which cost 2-3s per test,
and each `TestMigration000xxx` then stepped the full chain to its
version.

This change creates the test databases from Postgres' stock `template1`
(`dbtestutil.WithDBFrom`), runs the independent per-migration cases as
subtests of one `TestMigrationChain` that steps the chain once in
version order (each case still calls `next()` and asserts its migration
version; assertions are unchanged), and drops `TestMigrate/Twice`, which
the final `Up` of `UpDownUp` already covers. Cases that apply raw
migration SQL without recording the version, assert whole-table counts,
need `UNIQUE` values another case commits, or need an empty database for
a single-transaction chain stay standalone; they also got faster from
the cheaper database setup.

## Before / After

Local, `go test -count=1 -tags=testsmallbatch -parallel=8
./coderd/database/migrations/`:

| | Before | After |
|---|---|---|
| Package wall time | 23.1s | 7.7s |
| Summed top-level test time | about 176s | about 45s |
| TestMigrate (Once / Parallel / Twice / UpDownUp) | 3.8 / 3.7 / 3.7 /
8.5s | 1.5 / 1.4 / removed / 3.8s |
| TestMigrateUpWithFixtures (4 subtests) | 6.0 to 7.7s each | 3.1 to
3.6s each |
| 14 chained TestMigration000xxx cases | 2.9 to 6.1s each | one 4.7s
TestMigrationChain |
| 14 standalone TestMigration000xxx cases | 0.9 to 8.2s each | 0.7 to
6.2s each |

## Validation

- `go test -count=1 -tags=testsmallbatch -parallel=8
./coderd/database/migrations/` passes (7.7s; 8.6s when re-run on this
rebased head)
- `go test -race -count=1 ./coderd/database/migrations/` passes
- `go test -race -count=2 -shuffle=on -run TestMigrationChain` passes
(shared-fixture leakage check)
- `golangci-lint run ./coderd/database/migrations/...` reports 0 issues
- Repository pre-commit hook (gen, fmt, lint/go) passed on the commit

Part of the test-suite speedup series (see #29457); independent of the
other PRs.

## Remote validation

Remote run on a dogfood workspace (separate compute), chat
https://dogfood.cdr.dev/agents/5bbe73d0-866a-4841-9cc3-c5e5b4d97e44,
tested head `6cb9e3602e7` against base `9d973372774` on the same
machine: `coderd/database/migrations` 19.09s to 11.94s (plain) and 8.43s
(`-v` rerun); the speedup there is 1.6x to 2.3x rather than the 3x seen
on the development host, so expect the CI package to land around 50-70s
rather than 40s. `-race -count=3 -shuffle=on -run
'TestMigrationChain|TestMigrate'`: pass; `go test -short` skips
`TestMigrationChain` (`ok 3.9s`); no `Twice` subtest remains and the 14
per-migration tests still run. Endorsed verdict: PASS (speed claim
reproduces within tolerance).


> Xum (an AI agent) authored this PR on behalf of @ibetitsmike.
2026-09-17 20:40:43 +02:00
..
2022-11-14 17:57:33 +00:00