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
+8 -2
View File
@@ -10,8 +10,13 @@ import (
// Config holds all application-level configuration.
type Config struct {
// DSN is the MySQL connection string.
// Format: user:password@tcp(host:port)/dbname?parseTime=true&loc=UTC&time_zone=%%27UTC%%27
// Driver selects the database engine: "mysql" (default) or "sqlite".
// Read from DB_DRIVER. Switching engines is a single env var change.
Driver string
// DSN is the database connection string.
// mysql: user:password@tcp(host:port)/dbname?parseTime=true&loc=UTC&time_zone=%%27UTC%%27
// sqlite: a file path (e.g. /var/lib/pangolin/pangolin.db) or :memory:
DSN string
// RedisAddr is the Redis server address (host:port).
@@ -72,6 +77,7 @@ type Config struct {
// Returns an error if required variables are missing.
func FromEnv() (*Config, error) {
c := &Config{
Driver: os.Getenv("DB_DRIVER"),
DSN: os.Getenv("DB_DSN"),
RedisAddr: getEnvDefault("REDIS_ADDR", "127.0.0.1:6379"),
RedisPassword: os.Getenv("REDIS_PASSWORD"),