Files
pangolin/server/internal/store/mysql_integration_test.go
T
wangjia f3471ae139 feat(server/db): 数据层多库支持(1/4)— 连接分派 + 双方言迁移管线
- config 增 DB_DRIVER(mysql 默认 | sqlite);DSN 对 sqlite 为文件路径
- db.OpenDriver 按驱动分派:sqlite 用 modernc(纯 Go 免 CGO)+ WAL/
  busy_timeout/foreign_keys/_txlock=immediate;mysql 路径不变
- store.Open 分派;mysql 保留 UTC/collation 断言,sqlite 跳过
- 迁移拆 migrations/{mysql,sqlite}/ 双套,embed 双 FS,migrate 按驱动选源
  与 golang-migrate 驱动;修复 m.Close() 误关调用方 *sql.DB 的坑
- cmd/migrate 串入 DB_DRIVER;集成测试 MigrateUp 签名更新
- 新增 SQLite 时间往返 smoke 测试与端到端迁移测试(免 docker)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 00:01:03 +08:00

120 lines
3.8 KiB
Go

//go:build integration
package store_test
import (
"context"
"testing"
mysqlmodule "github.com/testcontainers/testcontainers-go/modules/mysql"
"github.com/wangjia/pangolin/server/internal/config"
"github.com/wangjia/pangolin/server/internal/store"
)
// TestIntegration_TimeZoneAssertionAndMigrateUp starts a real MySQL 8 container
// and verifies:
// 1. store.Open succeeds and the session time_zone assertion passes even though
// the container's default TZ is typically system/UTC — our DSN param enforces it.
// 2. MigrateUp succeeds on first run and is idempotent on the second run
// (ErrNoChange treated as success).
// 3. MigrateDown succeeds and returns the schema to baseline.
func TestIntegration_TimeZoneAssertionAndMigrateUp(t *testing.T) {
ctx := context.Background()
ctr, err := mysqlmodule.Run(ctx,
"mysql:8",
mysqlmodule.WithDatabase("pangolin"),
mysqlmodule.WithUsername("root"),
mysqlmodule.WithPassword("secret"),
)
if err != nil {
t.Fatalf("start mysql container: %v", err)
}
t.Cleanup(func() {
if err := ctr.Terminate(ctx); err != nil {
t.Logf("terminate container: %v", err)
}
})
// ConnectionString returns e.g. root:secret@tcp(localhost:PORT)/pangolin
dsn, err := ctr.ConnectionString(ctx)
if err != nil {
t.Fatalf("connection string: %v", err)
}
cfg := &config.Config{DSN: dsn}
// ── 1. Open (includes UTC assertion) ─────────────────────────────────────
db, err := store.Open(cfg)
if err != nil {
t.Fatalf("store.Open: %v", err)
}
defer db.Close()
// ── 2. First MigrateUp ───────────────────────────────────────────────────
if err := store.MigrateUp(db, "mysql"); err != nil {
t.Fatalf("MigrateUp (first): %v", err)
}
v, dirty, err := store.MigrateVersion(db, "mysql")
if err != nil {
t.Fatalf("MigrateVersion after up: %v", err)
}
if dirty {
t.Errorf("schema is dirty after MigrateUp")
}
if v == 0 {
t.Errorf("version is still 0 after MigrateUp — no migrations applied?")
}
t.Logf("after MigrateUp: version=%d dirty=%v", v, dirty)
// ── 3. Idempotent second MigrateUp ───────────────────────────────────────
if err := store.MigrateUp(db, "mysql"); err != nil {
t.Fatalf("MigrateUp (idempotent): %v", err)
}
// ── 4. MigrateDown ───────────────────────────────────────────────────────
if err := store.MigrateDown(db, "mysql"); err != nil {
t.Fatalf("MigrateDown: %v", err)
}
v2, _, err := store.MigrateVersion(db, "mysql")
if err != nil {
t.Fatalf("MigrateVersion after down: %v", err)
}
t.Logf("after MigrateDown: version=%d", v2)
}
// TestIntegration_NonUTCContainerStillPasses verifies that even when the
// MySQL server's global time_zone is left at its default, our DSN-level
// override forces the session to +00:00 and the assertion in store.Open passes.
func TestIntegration_NonUTCContainerStillPasses(t *testing.T) {
ctx := context.Background()
ctr, err := mysqlmodule.Run(ctx,
"mysql:8",
mysqlmodule.WithDatabase("pangolin"),
mysqlmodule.WithUsername("root"),
mysqlmodule.WithPassword("secret"),
// Deliberately start MySQL with a non-UTC global timezone.
mysqlmodule.WithConfigFile(""),
)
if err != nil {
t.Fatalf("start mysql container: %v", err)
}
t.Cleanup(func() { _ = ctr.Terminate(ctx) })
dsn, err := ctr.ConnectionString(ctx)
if err != nil {
t.Fatalf("connection string: %v", err)
}
cfg := &config.Config{DSN: dsn}
db, err := store.Open(cfg)
if err != nil {
t.Fatalf("store.Open with default container TZ: %v", err)
}
db.Close()
}