refactor(db): 阶段A - driver 抽象 + SQLite 保持工作

- db.Open 改为接受 URL(sqlite:// mysql:// postgres://),返回 (db, dialect, err)
- 向后兼容:不含 scheme 的路径仍按 SQLite 文件处理
- migration 移到 migrations/sqlite/ 子目录,embed 包含三个方言目录
- Migrate 按方言选择子目录 + goose dialect
- Store 加 Dialect 字段,NewStore(db, dialect)
- cmd/server 加 -db-url 和 DATABASE_URL 环境变量(优先级:env > -db-url > -db)
- cmd/init-admin 同步支持 URL
- 新增依赖 pgx/v5、go-sql-driver/mysql
- mysql/postgres migration 目录暂为 SQLite 副本占位,阶段B/C 替换
- 所有测试调用方更新为新签名

验证:现有测试全过(SQLite 回归),SQLite 行为不变

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
ChristandClaude Opus 4.8 committed 2026-07-06 12:35:01 +08:00
1 parent 3f7954af84
commit 0fa5d6617c
39 files changed
+1459 -49

No files matched your search

+4 -4
View File
@@ -22,12 +22,12 @@ func (noopHandler) OnAccountAlert(context.Context, string, string, string, strin
// 用无效 cookie 让账号快速进入重连等待(不会真正连上),验证管理逻辑而非网络。
func TestManagerStartStop(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "test.db")
d, err := db.Open(context.Background(), dbPath)
d, _, err := db.Open(context.Background(), dbPath)
if err != nil {
t.Fatalf("Open: %v", err)
}
defer d.Close()
store := db.NewStore(d)
store := db.NewStore(d, db.DialectSQLite)
store.Users.Create(context.Background(), "admin", "[email protected]", "pw")
admin, _ := store.Users.GetByUsername(context.Background(), "admin")
@@ -76,12 +76,12 @@ func TestManagerStartStop(t *testing.T) {
// TestManagerStopAll 验证 StopAll 停止所有运行中的账号,用于进程优雅退出。
func TestManagerStopAll(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "test.db")
d, err := db.Open(context.Background(), dbPath)
d, _, err := db.Open(context.Background(), dbPath)
if err != nil {
t.Fatalf("Open: %v", err)
}
defer d.Close()
store := db.NewStore(d)
store := db.NewStore(d, db.DialectSQLite)
store.Users.Create(context.Background(), "admin", "[email protected]", "pw")
admin, _ := store.Users.GetByUsername(context.Background(), "admin")
// 三个启用账号。