5a9b5f1d8e
顺带修 000020 sqlite 索引名冲突:ALTER TABLE RENAME 不会带着重命名索引, legacy_codes 上遗留的 idx_codes_status 与 codes 库自己迁移里同名索引撞名, 一并改成 idx_legacy_codes_status(up/down 对称)。
42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package store_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/wangjia/pangolin/server/internal/config"
|
|
"github.com/wangjia/pangolin/server/internal/store"
|
|
)
|
|
|
|
// TestApplyCodesLibMigrations verifies the shared codes library's tables are
|
|
// created on top of a fully-migrated pangolin sqlite DB (post-000020, 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)
|
|
}
|
|
}
|
|
}
|