Files
pangolin/server/internal/store/codes_lib_migrate_test.go
T
wangjia a307c6ca2c
Deploy Server / deploy-server (push) Failing after 41s
fix(test): codes-lib 迁移往返测试改用 m.Migrate(21) 显式定位版本
TestCodesLibMigrateRoundTrip 硬编码 6 次 Steps(-1),假设 000027 是迁移栈顶;本功能
加了 000028_routing_profiles 后 6 步落在版本 22、跳过了 000022 down,导致断言失败。
根因不是迁移 SQL(000022 up/down 正确、codes-lib 表无 FK——推翻了 FK 假设),而是
测试的脆弱步数计数被新增迁移打乱。改用 m.Migrate(21) 显式降到 000022 down 之后的
边界,不受栈顶新增迁移影响。全仓 go test ./... 恢复 0 FAIL。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A79VtQA1BwTuQN1ThpvYpo
2026-07-29 07:08:56 +08:00

166 lines
6.5 KiB
Go

package store_test
import (
"context"
"database/sql"
"testing"
"github.com/golang-migrate/migrate/v4"
migratesqlite "github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/wangjia/pangolin/server/internal/config"
"github.com/wangjia/pangolin/server/internal/store"
"github.com/wangjia/pangolin/server/migrations"
)
// TestApplyCodesLibMigrations verifies the shared codes library's tables are
// created on top of a fully-migrated pangolin sqlite DB (post-000022, the
// legacy tables are renamed away so the lib's `codes` table name is free).
func TestApplyCodesLibMigrations(t *testing.T) {
ctx := context.Background()
db, err := store.Open(&config.Config{Driver: "sqlite", DSN: ":memory:"})
if err != nil {
t.Fatalf("open: %v", err)
}
defer db.Close()
if err := store.MigrateUp(db, "sqlite"); err != nil {
t.Fatalf("MigrateUp: %v", err)
}
if err := store.ApplyCodesLibMigrations(ctx, db, "sqlite"); err != nil {
t.Fatalf("ApplyCodesLibMigrations: %v", err)
}
// 幂等:重复调用不报错(库自带 codes_schema_migrations 追踪表)。
if err := store.ApplyCodesLibMigrations(ctx, db, "sqlite"); err != nil {
t.Fatalf("ApplyCodesLibMigrations 2nd: %v", err)
}
for _, tbl := range []string{"codes", "codes_batches", "codes_audit_log", "codes_schema_migrations"} {
var name string
if err := db.QueryRow(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`, tbl,
).Scan(&name); err != nil {
t.Errorf("lib table %q missing: %v", tbl, err)
}
}
}
// newSQLiteStepper builds a golang-migrate instance identical to the one
// internal/store.MigrateUp/MigrateDown use internally (same embedded FS,
// same sqlite driver), except it exposes Steps() so the test can land
// exactly on the 000022 boundary instead of only being able to invoke a
// full up-to-latest / down-to-zero round trip like the public API does.
// Mirrors internal/store/migrate.go's newMigrator — kept in the test
// because that constructor is unexported.
func newSQLiteStepper(t *testing.T, database *sql.DB) *migrate.Migrate {
t.Helper()
src, err := iofs.New(migrations.SQLiteFS, "sqlite")
if err != nil {
t.Fatalf("iofs source: %v", err)
}
drv, err := migratesqlite.WithInstance(database, &migratesqlite.Config{})
if err != nil {
t.Fatalf("sqlite driver: %v", err)
}
m, err := migrate.NewWithInstance("iofs", src, "sqlite", drv)
if err != nil {
t.Fatalf("new migrator: %v", err)
}
return m
}
// TestCodesLibMigrateRoundTrip exercises the real wired path end-to-end:
// MigrateUp -> ApplyCodesLibMigrations -> step 000022 back down -> assert
// legacy restored/lib gone -> step back up -> ApplyCodesLibMigrations again
// -> finally the full `store.MigrateDown` (== cmd/migrate's "down" command)
// all the way to version 0, which is the exact call the reviewer reported
// as failing hard.
//
// Reproduces the reviewer-found collision: 000022's down does
// legacy_codes -> codes renames, but the codes-lib's own `codes` table
// (created by ApplyCodesLibMigrations, untracked by golang-migrate) is
// still sitting there, so the rename used to fail with "table already
// exists" (sqlite) / "ALTER TABLE ... table already exists" (mysql
// RENAME TABLE semantics). 000022's down script must DROP the lib-owned
// tables before renaming legacy_* back.
func TestCodesLibMigrateRoundTrip(t *testing.T) {
ctx := context.Background()
db, err := store.Open(&config.Config{Driver: "sqlite", DSN: ":memory:"})
if err != nil {
t.Fatalf("open: %v", err)
}
defer db.Close()
// 1. Up + wire in the codes-lib's own tables (the real cmd/migrate up path).
if err := store.MigrateUp(db, "sqlite"); err != nil {
t.Fatalf("MigrateUp: %v", err)
}
if err := store.ApplyCodesLibMigrations(ctx, db, "sqlite"); err != nil {
t.Fatalf("ApplyCodesLibMigrations: %v", err)
}
// 2. Step exactly 000022 back down (21 <- 22). This is the precise seam
// the reviewer's repro hit: the lib's `codes` table collides with the
// name 000022's down script renames legacy_codes back to.
m := newSQLiteStepper(t, db)
// 所有排在 000022 之上的迁移(000023..最新,如 pay_purchases / invite_rewards /
// notices / routing_profiles …)都与本冲突无关 —— 一次性降到版本 21,恰好落在
// 000022(codes_lib_legacy_rename)down 之后的边界。用版本号显式定位而非硬编码
// Steps(-1) 计数,避免栈顶新增迁移(如 000028)后步数漂移、跳过 000022 down。
if err := m.Migrate(21); err != nil {
t.Fatalf("migrate down to v21 (past 000022 down): %v (this is the reviewer-reported collision — "+
"000022 down must DROP the codes-lib tables before renaming legacy_* back)", err)
}
// 3. Legacy tables restored to their original pangolin names/shape;
// lib-owned tables gone.
for _, tbl := range []string{"codes", "code_batches"} {
var name string
if err := db.QueryRow(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`, tbl,
).Scan(&name); err != nil {
t.Errorf("legacy table %q not restored: %v", tbl, err)
}
}
// Original pangolin `codes` shape (plan_id/duration_days), not the
// codes-lib shape (entitlement_kind/entitlement_payload).
if _, err := db.Exec(`SELECT plan_id, duration_days, redeemed_by FROM codes LIMIT 0`); err != nil {
t.Errorf("codes table not restored to pangolin shape: %v", err)
}
for _, tbl := range []string{"codes_batches", "codes_audit_log", "codes_schema_migrations"} {
var name string
err := db.QueryRow(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`, tbl,
).Scan(&name)
if err == nil {
t.Errorf("lib table %q should have been dropped by the 000022 down step, still present", tbl)
} else if err != sql.ErrNoRows {
t.Errorf("checking lib table %q gone: %v", tbl, err)
}
}
// 4. Step 000022 back up + reapply the lib migrations: idempotent, must
// stay green.
if err := m.Steps(1); err != nil {
t.Fatalf("step 000022 up (2nd round-trip): %v", err)
}
if err := store.ApplyCodesLibMigrations(ctx, db, "sqlite"); err != nil {
t.Fatalf("ApplyCodesLibMigrations (2nd round-trip): %v", err)
}
// 5. Finally, the exact call the reviewer reported as failing hard:
// `cmd/migrate down` == store.MigrateDown, a full rollback to version 0.
// Must succeed cleanly (no "table already exists").
if err := store.MigrateDown(db, "sqlite"); err != nil {
t.Fatalf("store.MigrateDown (full, == cmd/migrate down): %v", err)
}
v, _, err := store.MigrateVersion(db, "sqlite")
if err != nil {
t.Fatalf("MigrateVersion after full down: %v", err)
}
if v != 0 {
t.Errorf("version after full MigrateDown = %d, want 0", v)
}
}