test(server): mysql 集成测试对齐 codes 库新表(断言不变,环境搭建换 ApplyMigrations)(#codes-lib)

This commit is contained in:
wangjia
2026-07-10 15:03:43 +08:00
parent ef4ed47759
commit db3428f622
+19 -30
View File
@@ -23,6 +23,7 @@ import (
tcredis "github.com/testcontainers/testcontainers-go/modules/redis"
"github.com/wangjia/pangolin/server/internal/codes"
"github.com/wangjia/pangolin/server/internal/store"
)
// --------------------------------------------------------------------------
@@ -102,28 +103,6 @@ func applySchema(db *sql.DB) error {
ad_gate BOOLEAN NOT NULL DEFAULT FALSE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
`CREATE TABLE IF NOT EXISTS code_batches (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
channel ENUM('store','tg','line','manual') NOT NULL,
created_by VARCHAR(64) NOT NULL,
note VARCHAR(255) NULL,
created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
`CREATE TABLE IF NOT EXISTS codes (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
code_hash CHAR(64) NOT NULL UNIQUE,
plan_id BIGINT UNSIGNED NOT NULL,
duration_days INT NOT NULL,
batch_id BIGINT UNSIGNED NOT NULL,
status ENUM('unused','redeemed','void') NOT NULL DEFAULT 'unused',
redeemed_by BIGINT UNSIGNED NULL,
redeemed_at DATETIME(6) NULL,
FOREIGN KEY (plan_id) REFERENCES plans(id),
FOREIGN KEY (batch_id) REFERENCES code_batches(id),
INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
`CREATE TABLE IF NOT EXISTS subscriptions (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
@@ -154,10 +133,17 @@ func applySchema(db *sql.DB) error {
return fmt.Errorf("schema exec: %w\nSQL: %s", err, stmt)
}
}
// Apply the shared codes library migrations (creates codes_batches, codes, codes_audit_log).
if err := store.ApplyCodesLibMigrations(context.Background(), db, "mysql"); err != nil {
return fmt.Errorf("apply codes lib migrations: %w", err)
}
return nil
}
// insertCode inserts a test code directly into the DB and returns it.
// insertCode inserts a test code directly into the new codes_batches and codes
// tables (library schema) for testing.
func insertCode(t *testing.T, db *sql.DB, plaintext string, plan codes.PlanCode, durationDays int) {
t.Helper()
canonical, err := codes.Canonicalize(plaintext)
@@ -166,13 +152,14 @@ func insertCode(t *testing.T, db *sql.DB, plaintext string, plan codes.PlanCode,
}
hash := codes.Hash(canonical)
var planID int64
if err := db.QueryRow(`SELECT id FROM plans WHERE code=?`, string(plan)).Scan(&planID); err != nil {
t.Fatalf("plan lookup: %v", err)
}
// Create the entitlement payload JSON for a duration entitlement.
payload := fmt.Sprintf(`{"plan":"%s","days":%d}`, string(plan), durationDays)
// Insert batch into codes_batches.
_, err = db.Exec(
`INSERT INTO code_batches (channel, created_by, created_at) VALUES ('manual','test',UTC_TIMESTAMP(6))`,
`INSERT INTO codes_batches (channel, entitlement_kind, entitlement_payload, created_by, created_at)
VALUES ('manual', 'duration', ?, 'test', UTC_TIMESTAMP(6))`,
payload,
)
if err != nil {
t.Fatalf("batch insert: %v", err)
@@ -180,9 +167,11 @@ func insertCode(t *testing.T, db *sql.DB, plaintext string, plan codes.PlanCode,
var batchID int64
db.QueryRow(`SELECT LAST_INSERT_ID()`).Scan(&batchID)
// Insert code into codes.
_, err = db.Exec(
`INSERT INTO codes (code_hash, plan_id, duration_days, batch_id) VALUES (?,?,?,?)`,
hash, planID, durationDays, batchID,
`INSERT INTO codes (code_hash, batch_id, entitlement_kind, entitlement_payload, status, created_at)
VALUES (?, ?, 'duration', ?, 'unused', UTC_TIMESTAMP(6))`,
hash, batchID, payload,
)
if err != nil {
t.Fatalf("code insert: %v", err)