A fresh `bd init` against a shared Dolt sql-server under concurrent load could
permanently brick the database at migration 0040. Two compounding causes:
1. An undrained `CALL DOLT_*` result set on the pinned migration connection
("busy buffer" -> "driver: bad connection") failed the SEPARATE version-record
INSERT that follows it, so schema_migrations never advanced past the
partially-applied migration.
2. Migrations 0040/0041 are non-idempotent (0040 bare-INSERTs its
dolt_nonlocal_tables rows; 0041 DELETEs then commits them without
--skip-empty), so the init retry loop's replay died on "duplicate primary key
given: [wisps]" (0040) or "nothing to commit" (0041).
drainCall() fixes cause 1 (pure Go, no migration-content change). For cause 2,
the shipped 0040/0041 bytes are frozen and content-hashed
(scripts/check-migration-hygiene.sh check C): editing them would change the
recorded content_hash and fork every already-upgraded clone (the #4259 skew the
smart remote-migrate gate, `bd doctor`, and merge-settle all detect as a genuine
schema fork). Per the migration_repairs.go doctrine, a failing migration also
cannot be fixed forward with a NEW migration (a database bricked at 40 never
reaches it). So idempotency is delivered as pre-migration repairs keyed to
versions 40 and 41 that normalise dolt_nonlocal_tables so each frozen body
replays cleanly — leaving 0040/0041's released bytes (and their content hashes)
untouched.
- schema.go: drainCall() consumes every CALL DOLT_* result set on the pinned
migration connection — migration bodies via execMigrationBody, the
apply-migrations DOLT_COMMIT, DOLT_RESET, DOLT_ADD, commitSeededDoltIgnore's
add/commit pair, and commitMigrationStep's per-step add/commit;
GET_LOCK/RELEASE_LOCK stay single-result SELECTs.
- lock.go: the fresh-bootstrap heal's DOLT_RESET('--hard') drains too — it is
immediately followed by a full MigrateUp pass on that same pinned conn.
- migration_repairs.go: keyed preMigrationRepairs registry;
repairPartial0040NonlocalInsert / repairPartial0041NonlocalDelete.
- migration_body_drain_test.go: drain-classification guards +
TestFrozenNonIdempotentMigrationsHaveRepairs (a frozen non-idempotent
nonlocal-table migration must have a registered repair, not edited SQL).
- initschema_idempotent_test.go (integration): forge the committed partial-0040
and partial-0041 brick states on a real sql-server; assert re-init recovers.
- lock_test.go / schema_test.go: mock the converted DOLT_ADD/RESET/COMMIT sites
as queries because drainCall uses QueryContext.
Rebased onto current main and reworked per maphew's review on #4504:
- Corrected the drainCall doc-comment mechanism. go-sql-driver's mysqlConn.exec
DOES end in handleOk.discardResults(), so the happy path was never the gap.
The real asymmetry is the error path: exec returns early — without discarding
— when readResultSetHeaderPacket/skipColumns/skipRows fails mid-stream, while
mysqlRows.Close() drains unconditionally. That asymmetry is reachable here
precisely because MigrateUp and commitMigrationStep swallow "nothing to
commit" and keep using the same pinned connection.
- Extended the conversion to the CALL DOLT_* sites main added since the original
base (commitMigrationStep, commitSeededDoltIgnore, lock.go's
DOLT_RESET('--hard')), which are the hot ones.
- Lowered TestRunMigrationsLargeRigNoticeOnlyOnMainSource's mock bounds to 39,
matching the two tests already lowered. It merges silently outside every
conflict region and would otherwise reach 40/41 and panic on the mockDB stubs.
- Scoped the repair commits: they stage dolt_nonlocal_tables BY NAME instead of
DOLT_COMMIT('-Am', ...), so on the bounded-migrate path (upTo != 0, per-step
commit off) they no longer sweep migrations 1..upTo's uncommitted working set
into a repair-labelled commit. Verified against a real dolt server that a
scoped add stages only that table and leaves the rest of the working set
untouched.
- Conflict resolution kept main's repairs at versions 47 and 53 (including
#4690's ensureDependenciesIDColumn) in the new keyed registry.
Verified: go build, go vet, scripts/check-migration-hygiene.sh, the full
internal/storage/schema suite, and both integration tests in
initschema_idempotent_test.go against a real dolt sql-server.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Executed-By: beads/crew/codd