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>
This commit is contained in:
wangjia
2026-06-18 00:01:03 +08:00
parent 9a028ab907
commit f3471ae139
63 changed files with 583 additions and 82 deletions
+87 -12
View File
@@ -1,29 +1,104 @@
// Package db provides a MySQL connection helper.
// Package db provides a database connection helper that dispatches on the
// configured driver (MySQL or SQLite). Switching engines is a single env var:
//
// DB_DRIVER=mysql (default) → DB_DSN is a full MySQL DSN
// DB_DRIVER=sqlite → DB_DSN is the SQLite file path (or :memory:)
package db
import (
"database/sql"
"fmt"
"os"
"strings"
"time"
// Register mysql driver.
// Register the database drivers with database/sql.
_ "github.com/go-sql-driver/mysql"
_ "modernc.org/sqlite"
)
// Open opens a *sql.DB backed by MySQL and pings the server.
// The DSN must include parseTime=true and time_zone='+00:00' (UTC).
func Open(dsn string) (*sql.DB, error) {
db, err := sql.Open("mysql", dsn)
// Normalize canonicalizes a driver name. Empty → "mysql"; "sqlite3" → "sqlite".
func Normalize(driver string) string {
switch strings.ToLower(strings.TrimSpace(driver)) {
case "", "mysql":
return "mysql"
case "sqlite", "sqlite3":
return "sqlite"
default:
return strings.ToLower(strings.TrimSpace(driver))
}
}
// Driver returns the configured driver from DB_DRIVER (default "mysql").
func Driver() string { return Normalize(os.Getenv("DB_DRIVER")) }
// Open opens a *sql.DB using the driver from DB_DRIVER (default mysql) and pings it.
func Open(dsn string) (*sql.DB, error) { return OpenDriver(Driver(), dsn) }
// OpenDriver opens a *sql.DB for the given driver and DSN, configures the pool,
// and pings. For mysql the DSN must already carry parseTime=true and UTC params
// (see internal/store.Open which builds them); for sqlite the DSN is a file path.
func OpenDriver(driver, dsn string) (*sql.DB, error) {
switch Normalize(driver) {
case "sqlite":
return openSQLite(dsn)
default:
return openMySQL(dsn)
}
}
func openMySQL(dsn string) (*sql.DB, error) {
database, err := sql.Open("mysql", dsn)
if err != nil {
return nil, fmt.Errorf("db.Open: %w", err)
}
db.SetMaxOpenConns(30)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(5 * time.Minute)
database.SetMaxOpenConns(30)
database.SetMaxIdleConns(10)
database.SetConnMaxLifetime(5 * time.Minute)
if err := db.Ping(); err != nil {
db.Close()
if err := database.Ping(); err != nil {
database.Close()
return nil, fmt.Errorf("db.Open ping: %w", err)
}
return db, nil
return database, nil
}
func openSQLite(dsn string) (*sql.DB, error) {
database, err := sql.Open("sqlite", sqliteDSN(dsn))
if err != nil {
return nil, fmt.Errorf("db.Open(sqlite): %w", err)
}
// SQLite is a single-writer engine; readers/writers serialize via the
// busy_timeout pragma (set in the DSN). A modest pool is safe under WAL.
database.SetMaxOpenConns(10)
database.SetMaxIdleConns(10)
database.SetConnMaxLifetime(0)
if err := database.Ping(); err != nil {
database.Close()
return nil, fmt.Errorf("db.Open(sqlite) ping: %w", err)
}
return database, nil
}
// sqliteDSN appends the pragmas the server relies on (busy_timeout for write
// contention, foreign_keys enforcement, WAL for file-backed DBs). An empty DSN
// or ":memory:" maps to a shared-cache in-memory DB (so the pool's connections
// see the same database — used by tests).
func sqliteDSN(dsn string) string {
// _txlock=immediate makes every transaction BEGIN IMMEDIATE (acquire the
// write lock up front), giving the pessimistic exclusivity that the MySQL
// "SELECT … FOR UPDATE" paths rely on. busy_timeout lets contended writers
// wait instead of failing with SQLITE_BUSY.
const filePragmas = "_txlock=immediate&_pragma=busy_timeout(5000)&_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)"
const memPragmas = "_txlock=immediate&_pragma=busy_timeout(5000)&_pragma=foreign_keys(1)"
if dsn == "" || dsn == ":memory:" {
return "file::memory:?cache=shared&" + memPragmas
}
sep := "?"
if strings.Contains(dsn, "?") {
sep = "&"
}
return dsn + sep + filePragmas
}
+61
View File
@@ -0,0 +1,61 @@
package db
import (
"testing"
"time"
)
// TestSQLiteTimeRoundTrip de-risks the migration translation: confirm modernc
// SQLite reads a DATETIME column back into a Go time.Time (UTC) correctly.
func TestSQLiteTimeRoundTrip(t *testing.T) {
d, err := OpenDriver("sqlite", ":memory:")
if err != nil {
t.Fatalf("open: %v", err)
}
defer d.Close()
if _, err := d.Exec(`CREATE TABLE t (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME NOT NULL,
flag INTEGER NOT NULL DEFAULT 0,
meta TEXT NULL
)`); err != nil {
t.Fatalf("create: %v", err)
}
want := time.Date(2026, 6, 17, 8, 30, 45, 123456000, time.UTC)
if _, err := d.Exec(`INSERT INTO t (expires_at, flag, meta) VALUES (?, ?, ?)`,
want, true, `{"k":"v"}`); err != nil {
t.Fatalf("insert: %v", err)
}
var created, expires time.Time
var flag bool
var meta string
if err := d.QueryRow(`SELECT created_at, expires_at, flag, meta FROM t WHERE id = 1`).
Scan(&created, &expires, &flag, &meta); err != nil {
t.Fatalf("scan: %v", err)
}
t.Logf("created=%v expires=%v flag=%v meta=%s", created, expires, flag, meta)
if !expires.Equal(want) {
t.Errorf("expires round-trip: got %v, want %v", expires.UTC(), want)
}
if created.IsZero() {
t.Errorf("created_at default not populated")
}
if !flag || meta != `{"k":"v"}` {
t.Errorf("flag/meta mismatch: flag=%v meta=%s", flag, meta)
}
// WHERE comparison against a time param (used by expires_at > ? queries).
var n int
if err := d.QueryRow(`SELECT COUNT(*) FROM t WHERE expires_at > ?`,
want.Add(-time.Hour)).Scan(&n); err != nil {
t.Fatalf("count: %v", err)
}
if n != 1 {
t.Errorf("time comparison: got %d rows, want 1", n)
}
}