beads/internal/storage/schema/progress_test.go
Jim Wordelman e2dddf9380
schema: migration progress output + large-rig warning (be-ldr) (#3919)
* feat(schema): large-rig warning + human migration names (be-8ja)

Add emitLargeRigNotice (one-line stderr warning when the issues table
exceeds 10,000 rows before a migration pass, silent on fresh installs
where the COUNT(*) query errors) and humanMigrationName (turns
"0033_add_date_indexes.up.sql" into "add_date_indexes" for
operator-facing progress text). Also add largeRigThreshold and
issueRowCounter (the latter a package-level var so schema tests can
stub it without a live DB).

This is the surviving, still-unique portion of #3919 after #3914
landed the TTY-gated stderr progress line on main independently: the
large-rig warning and human-readable names were never part of #3914's
scope. Carried over verbatim from the original be-8ja commits
(ce9d6e04, 602ea7c4) authored on this branch; not yet wired into
runMigrations — that lands in a follow-up commit that adapts these
helpers onto main's existing TTY-gated `stderr` writer instead of
introducing a second progressOut writer.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(schema): wire large-rig/human-name helpers onto main's stderr progress line

Adapt the be-8ja helpers (emitLargeRigNotice, humanMigrationName,
issueRowCounter) from the previous commit onto #3914's landed
mechanism instead of introducing a second progress writer:

- Drop the progressOut var and TestProgressOutDefaultsToStderr; reuse
  main's existing TTY-gated `stderr` package var (see defaultStderr).
- Call issueRowCounter/emitLargeRigNotice once at the top of
  runMigrations, ahead of the per-migration loop, unconditionally for
  every runMigrations call (main and ignored sources) — same shape as
  the original be-8ja placement.
- Expand the single "migrating schema: %s" line #3914 landed into
  "Applying migration NNNN: name…" (humanMigrationName) followed by a
  "  done (N.Ns)" timing line after the migration's SQL and cursor row
  commit, still on the same `stderr` writer.
- Update TestRunMigrationsStderrOutput and
  TestRunMigrationsUsesProvidedSource: mockDB.QueryRowContext panics,
  so both now stub issueRowCounter before calling runMigrations, and
  the stderr assertion in the first test accounts for two lines per
  migration instead of one.

Deliberately does not touch preMigrationRepair threading or
commitEachStep: both predate this PR's fork point (main added
preMigrationRepair for migration 0053 after this branch diverged) and
are unrelated to the progress-output feature.
Agent-Signature: claude-sonnet-medium on behalf of matt wilkie

* fix(schema): single large-rig warning + commit-ordered done line (review round 2)

Cross-vendor review (opus approve-with-nits, codex two P2s) found two
issues in the previous commit's wiring:

1. Duplicate large-rig warning. runMigrations was called once for
   mainSource and once for ignoredSource in the same MigrateUp pass,
   and emitLargeRigNotice fired unconditionally at the top of each
   call. A large rig with pending migrations in both sources printed
   the "one-shot" warning twice and issued the COUNT(*) query twice.
   Fix: gate emission on src.cursorTable == mainSource.cursorTable,
   the same pattern migrate() already uses for commitEachStep. Fixed
   the "One-shot" comment to describe the gate. Added
   TestRunMigrationsLargeRigNoticeOnlyOnMainSource: stubs
   issueRowCounter to report a large rig and asserts the warning
   appears on the mainSource call and stays silent on the
   ignoredSource call.

2. "done (N.Ns)" printed before the per-step Dolt commit. On the
   production embedded path (commitEachStep=true), commitMigrationStep
   (DOLT_ADD/DOLT_COMMIT) is the expensive, fallible part of the step,
   but it ran after the done line and its timing — so an operator
   could see "done" and then hit a commit error, and the printed
   timing excluded the commit's cost. Fix: move both the "done" print
   and its time.Since(start) call to after the commitEachStep block,
   so it lands last regardless of whether commitEachStep is set, and a
   failed step commit returns before either print statement runs.

TestRunMigrationsStderrOutput and TestRunMigrationsUsesProvidedSource
both call runMigrations with commitEachStep=false, so the reordering
is a no-op for them — verified unaffected by the full schema suite
run below.
Agent-Signature: claude-sonnet-medium on behalf of matt wilkie

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-07-24 00:40:32 -07:00

99 lines
2.7 KiB
Go

package schema
import (
"bytes"
"errors"
"strings"
"testing"
)
// TestEmitLargeRigNotice covers the be-8ja large-rig warning branch. The
// fresh-install case (count reported via error, typically "table doesn't
// exist") must stay silent — that's the UX contract: on a first-ever bd run,
// there is no rig to warn about.
func TestEmitLargeRigNotice(t *testing.T) {
cases := []struct {
name string
count int64
countErr error
wantEmpty bool
wantSub string
}{
{
name: "fresh_install_table_missing",
count: 0,
countErr: errors.New("Table 'issues' doesn't exist"),
wantEmpty: true,
},
{
name: "small_rig_below_threshold",
count: 9_999,
countErr: nil,
wantEmpty: true,
},
{
name: "at_threshold_no_warning",
count: 10_000,
countErr: nil,
wantEmpty: true,
},
{
name: "one_past_threshold_warns",
count: 10_001,
wantSub: "Large rig detected (10001 issues)",
},
{
name: "typical_large_rig",
count: 49_187,
wantSub: "Large rig detected (49187 issues)",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
emitLargeRigNotice(&buf, tc.count, tc.countErr)
got := buf.String()
if tc.wantEmpty {
if got != "" {
t.Errorf("want no output, got %q", got)
}
return
}
if !strings.Contains(got, tc.wantSub) {
t.Errorf("want substring %q; got %q", tc.wantSub, got)
}
if !strings.Contains(got, "do not interrupt") {
t.Errorf("warning missing operator guidance; got %q", got)
}
if !strings.HasSuffix(got, "\n") {
t.Errorf("warning must end with newline; got %q", got)
}
})
}
}
// TestHumanMigrationName pins the filename → display-name mapping for the
// per-migration progress line. The be-8ja progress output MUST be stable and
// human-readable; regression here would change operator-visible text.
func TestHumanMigrationName(t *testing.T) {
cases := map[string]string{
"0033_add_date_indexes.up.sql": "add_date_indexes",
"0001_initial.up.sql": "initial",
"0027_add_started_at.up.sql": "add_started_at",
"noversion.up.sql": "noversion",
"0099_multi_word_migration_name.up.sql": "multi_word_migration_name",
}
for in, want := range cases {
if got := humanMigrationName(in); got != want {
t.Errorf("humanMigrationName(%q) = %q; want %q", in, got, want)
}
}
}
// Note: be-8ja's original TestProgressOutDefaultsToStderr (pinning a
// package-level progressOut default) doesn't apply after the #3919 slim —
// this branch reuses main's existing `stderr` writer (see defaultStderr,
// TTY-gated), landed independently by #3914. That invariant is exercised by
// #3914's own tests, not duplicated here.