beads/internal/storage/schema/schema_test.go
Afik 674561360d
schema: make DROP COLUMN migrations idempotent (fixes migration 0032 loop) (#3418)
* cmd/bd: extract sanitizeDBName to its own file for CGO_ENABLED=0 lint

sanitizeDBName lived in store_factory.go which is //go:build cgo,
but init.go (no tag) calls it. The pre-commit golangci-lint runs
with CGO_ENABLED=0 and flagged it as undefined. Move the pure-Go
function to its own untagged file and drop the now-unused strings
import from store_factory.go. No behavior change.

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

* schema: tolerate "does not have column" errors in migration runner

Broadens isConcurrentInitError to also treat "does not have column"
as a benign migration error, symmetric with the existing "duplicate
column" tolerance for ADD races. Makes DROP COLUMN migrations
idempotent across databases bootstrapped with different base schemas.

Observed failure: migration 0032 (DROP COLUMN applied_at) loops
forever on any database that lands at v31 without the column. Dolt
has no DROP COLUMN IF EXISTS, so the SQL itself can't be guarded;
handling it in the runner keeps the migration file trivial.

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

* Add migration 0032 idempotence tests (bd-7mh.3)

* Document bd execution metadata workflow (bd-b1a)

* Revert "Document bd execution metadata workflow (bd-b1a)"

This reverts commit 0d6a96babd.

* Reapply "Document bd execution metadata workflow (bd-b1a)"

This reverts commit df90cf5404.

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-04-27 09:15:20 -07:00

72 lines
1.9 KiB
Go

package schema
import (
"context"
"database/sql"
"fmt"
"strings"
"testing"
)
type execOnlyDB struct {
exec func(query string) error
}
func (db execOnlyDB) ExecContext(_ context.Context, query string, _ ...any) (sql.Result, error) {
if db.exec == nil {
return nil, nil
}
return nil, db.exec(query)
}
func (execOnlyDB) QueryContext(context.Context, string, ...any) (*sql.Rows, error) {
panic("QueryContext should not be called")
}
func (execOnlyDB) QueryRowContext(context.Context, string, ...any) *sql.Row {
panic("QueryRowContext should not be called")
}
func TestMigration0032ToleratesMissingAppliedAtColumn(t *testing.T) {
var recorded bool
db := execOnlyDB{
exec: func(query string) error {
switch {
case strings.Contains(query, "DROP COLUMN applied_at"):
return fmt.Errorf(`Error 1105 (HY000): table "schema_migrations" does not have column "applied_at"`)
case strings.Contains(query, "INSERT IGNORE INTO schema_migrations"):
recorded = true
}
return nil
},
}
applied, err := runMigrations(context.Background(), db, 31, false)
if err != nil {
t.Fatalf("runMigrations returned error for already-missing applied_at: %v", err)
}
if applied != 1 {
t.Fatalf("applied migrations = %d, want 1", applied)
}
if !recorded {
t.Fatal("migration 0032 was not recorded after already-missing applied_at")
}
}
func TestAllMigrationsSQLBootstrapsSchemaMigrationsBeforeDrop(t *testing.T) {
sql := AllMigrationsSQL()
bootstrap := strings.Index(sql, "CREATE TABLE IF NOT EXISTS schema_migrations")
if bootstrap < 0 {
t.Fatal("AllMigrationsSQL missing schema_migrations bootstrap")
}
dropAppliedAt := strings.Index(sql, "ALTER TABLE schema_migrations DROP COLUMN applied_at")
if dropAppliedAt < 0 {
t.Fatal("AllMigrationsSQL missing migration 0032 applied_at drop")
}
if bootstrap > dropAppliedAt {
t.Fatal("schema_migrations bootstrap appears after migration 0032 applied_at drop")
}
}