a62a2b1797
ci-pangolin / Lint — shellcheck (pull_request) Successful in 11s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (pull_request) Successful in 25s
ci-pangolin / Cleartext Scan — Android 禁明文 (pull_request) Successful in 19s
ci-pangolin / OpenAPI Sync Check (pull_request) Successful in 41s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (pull_request) Successful in 20s
ci-pangolin / Codegen Drift — token 生成物未漂移 (pull_request) Successful in 4s
ci-pangolin / DS-flow — 原型/跨端同源/代码色单源闸 (pull_request) Successful in 5s
ci-pangolin / Go — build + test (pull_request) Failing after 14s
ci-pangolin / E2E Smoke — L4 进程级端到端 (pull_request) Failing after 11s
ci-pangolin / Go — integration (mysql/redis testcontainers) (pull_request) Failing after 4m44s
ci-pangolin / Golden — 视觉回归 (全量:components/auth/desktop/tablet) (pull_request) Failing after 20s
ci-pangolin / Flutter — analyze + test (pull_request) Failing after 11m55s
CreateOrder 下单时的 HasPaidPurchase 只是裸 SELECT 无锁,并发/多挂起单可绕过 promo SKU「每账号限购一次」。两层修: ① webhook.go settle 在锁行、开通前对 item.Promo 的 SKU 复查一次(排除本单), 命中说明另一笔同 user+SKU 订单已抢先 settle,跳过发放(不二次 +N 天)、 仍 ack(否则 pay 无限重投)。新增 store.HasPaidPurchaseExcludingTx / MarkDuplicatePromoTx——重复单标记 canceled 而非 paid,避免自撞下面的 唯一索引、也避免整笔 500 触发死循环重投。 ② migration 000027(sqlite):部分唯一索引 ux_pay_promo_paid ON pay_purchases(user_id, sku) WHERE status='paid' AND sku='pro_month_promo', 兜底防止任何路径把同一用户的 promo 单二次写成 paid。mysql 8 不支持部分 索引,000027 mysql 侧是 no-op 占位(仅对齐编号),该场景 mysql 只靠①的 应用层复查兜底——两库防线强度不同,已在迁移文件与代码注释中记录。 副作用:新增迁移把 sqlite 迁移顶点从 26 推到 27,同步更新 internal/store/sqlite_migrate_test.go 的版本断言,以及 internal/store/codes_lib_migrate_test.go 手动 Steps(-1) 序列(补一步跳过 000027,才能精确落在 000022 边界,这条测试是硬编码步数的)。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
108 lines
3.6 KiB
Go
108 lines
3.6 KiB
Go
package store_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/wangjia/pangolin/server/internal/config"
|
|
"github.com/wangjia/pangolin/server/internal/store"
|
|
)
|
|
|
|
// TestSQLiteMigrateUpDown verifies the SQLite migration set applies cleanly,
|
|
// is idempotent, seeds correctly, and rolls back — no container required, so it
|
|
// runs in normal CI (unlike the MySQL integration test behind //go:build integration).
|
|
func TestSQLiteMigrateUpDown(t *testing.T) {
|
|
cfg := &config.Config{Driver: "sqlite", DSN: ":memory:"}
|
|
db, err := store.Open(cfg)
|
|
if err != nil {
|
|
t.Fatalf("store.Open(sqlite): %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
// 1. MigrateUp.
|
|
if err := store.MigrateUp(db, "sqlite"); err != nil {
|
|
t.Fatalf("MigrateUp: %v", err)
|
|
}
|
|
v, dirty, err := store.MigrateVersion(db, "sqlite")
|
|
if err != nil {
|
|
t.Fatalf("MigrateVersion: %v", err)
|
|
}
|
|
if dirty {
|
|
t.Fatalf("schema dirty after MigrateUp")
|
|
}
|
|
if v != 27 {
|
|
t.Errorf("version = %d, want 27", v)
|
|
}
|
|
|
|
// 2. Core tables exist.
|
|
for _, tbl := range []string{
|
|
"users", "devices", "plans", "subscriptions", "legacy_code_batches", "legacy_codes",
|
|
"usage_daily", "audit_log", "providers", "nodes", "node_events",
|
|
"directory_version", "provision_idempotency", "replacements", "admins",
|
|
"connect_credentials", "usage_device_daily", "sessions", "usage_hourly", "usage_device_hourly",
|
|
"pay_purchases", "referrals", "reward_claims", "notices",
|
|
} {
|
|
var name string
|
|
err := db.QueryRow(
|
|
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`, tbl,
|
|
).Scan(&name)
|
|
if err != nil {
|
|
t.Errorf("table %q missing: %v", tbl, err)
|
|
}
|
|
}
|
|
|
|
// 2b. idx_subs_user_exp survives the 000024 subscriptions table rebuild
|
|
// (SQLite table-rebuild migrations silently drop indexes unless recreated;
|
|
// this guards against that regression — see 000024_invite_rewards.{up,down}.sql).
|
|
var idxCount int
|
|
if err := db.QueryRow(
|
|
`SELECT COUNT(*) FROM sqlite_master WHERE type='index' AND name='idx_subs_user_exp'`,
|
|
).Scan(&idxCount); err != nil {
|
|
t.Fatalf("query idx_subs_user_exp: %v", err)
|
|
}
|
|
if idxCount != 1 {
|
|
t.Errorf("idx_subs_user_exp count = %d, want 1 (index lost on subscriptions rebuild?)", idxCount)
|
|
}
|
|
|
|
// 3. Seed: 3 plans + directory_version singleton.
|
|
var plans int
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM plans`).Scan(&plans); err != nil {
|
|
t.Fatalf("count plans: %v", err)
|
|
}
|
|
if plans != 3 {
|
|
t.Errorf("plans seeded = %d, want 3", plans)
|
|
}
|
|
var dv int
|
|
if err := db.QueryRow(`SELECT version FROM directory_version WHERE id=1`).Scan(&dv); err != nil {
|
|
t.Errorf("directory_version singleton missing: %v", err)
|
|
}
|
|
|
|
// 4. Added columns from later migrations are present (000011 / 000013).
|
|
if _, err := db.Exec(`SELECT reality_prk, reality_short_id, sub_token, totp_enabled FROM nodes
|
|
LEFT JOIN users ON 0=1 LIMIT 0`); err != nil {
|
|
// Separate queries — the join above is just a cheap column-existence probe.
|
|
if _, e := db.Exec(`SELECT reality_prk, reality_short_id FROM nodes LIMIT 0`); e != nil {
|
|
t.Errorf("nodes reality cols missing: %v", e)
|
|
}
|
|
if _, e := db.Exec(`SELECT sub_token, totp_secret_enc, totp_enabled FROM users LIMIT 0`); e != nil {
|
|
t.Errorf("users totp cols missing: %v", e)
|
|
}
|
|
}
|
|
|
|
// 5. Idempotent second MigrateUp.
|
|
if err := store.MigrateUp(db, "sqlite"); err != nil {
|
|
t.Fatalf("MigrateUp (idempotent): %v", err)
|
|
}
|
|
|
|
// 6. MigrateDown returns to baseline.
|
|
if err := store.MigrateDown(db, "sqlite"); err != nil {
|
|
t.Fatalf("MigrateDown: %v", err)
|
|
}
|
|
v2, _, err := store.MigrateVersion(db, "sqlite")
|
|
if err != nil {
|
|
t.Fatalf("MigrateVersion after down: %v", err)
|
|
}
|
|
if v2 != 0 {
|
|
t.Errorf("version after down = %d, want 0", v2)
|
|
}
|
|
}
|