From ece51d7e3d53ce97c688a2d42e57ca89003277c9 Mon Sep 17 00:00:00 2001 From: wangjia <809946525@qq.com> Date: Thu, 18 Jun 2026 00:01:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor(server/db):=20=E5=A4=9A=E5=BA=93(2/4)?= =?UTF-8?q?=E2=80=94=20=E6=97=B6=E9=97=B4=E7=AD=89=20DB=20=E7=AB=AF?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=80=80=E5=9B=9E=20Go(=E5=8F=AF=E7=A7=BB?= =?UTF-8?q?=E6=A4=8D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit auth/admin/httpapi:把 UTC_TIMESTAMP(6) 等 DB 端取值改为 Go time.Now().UTC() 作为 ? 参数传入(两库精度一致、可测、天然跨方言)。仅这三个文件不涉及 upsert/锁,故独立成提交;其余域文件的同类改动与 dialect 改动同语句交错,合入(3/4)。 Co-Authored-By: Claude Opus 4.8 --- server/internal/admin/store.go | 8 ++++---- server/internal/auth/store.go | 11 ++++++----- server/internal/httpapi/account.go | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/server/internal/admin/store.go b/server/internal/admin/store.go index 152dfd1..3aff831 100644 --- a/server/internal/admin/store.go +++ b/server/internal/admin/store.go @@ -63,8 +63,8 @@ func (s *DBStore) GetAdminByUsername(ctx context.Context, username string) (*Adm func (s *DBStore) CreateAdmin(ctx context.Context, username, pwHash string, totpSecretEnc []byte) (int64, error) { res, err := s.db.ExecContext(ctx, `INSERT INTO admins (username, pw_hash, totp_secret, status, created_at) - VALUES (?, ?, ?, 'active', UTC_TIMESTAMP(6))`, - username, pwHash, totpSecretEnc) + VALUES (?, ?, ?, 'active', ?)`, + username, pwHash, totpSecretEnc, time.Now().UTC()) if err != nil { return 0, fmt.Errorf("admin.CreateAdmin: %w", err) } @@ -149,8 +149,8 @@ func (s *DBStore) WriteAudit(ctx context.Context, actor, action, target, metaJSO } _, err := s.db.ExecContext(ctx, `INSERT INTO audit_log (actor, action, target, meta, at) - VALUES (?, ?, ?, ?, UTC_TIMESTAMP(6))`, - actor, action, target, metaJSON) + VALUES (?, ?, ?, ?, ?)`, + actor, action, target, metaJSON, time.Now().UTC()) if err != nil { return fmt.Errorf("admin.WriteAudit: %w", err) } diff --git a/server/internal/auth/store.go b/server/internal/auth/store.go index daa558a..24aff05 100644 --- a/server/internal/auth/store.go +++ b/server/internal/auth/store.go @@ -66,10 +66,11 @@ func (s *SQLStore) CreateUserWithTrial(ctx context.Context, email, pwHash string } }() + now := time.Now().UTC() res, err := tx.ExecContext(ctx, `INSERT INTO users (uuid, email, pw_hash, dp_uuid, status, created_at) - VALUES (?, ?, ?, ?, 'active', UTC_TIMESTAMP(6))`, - userUUID, email, pwHash, dpUUID) + VALUES (?, ?, ?, ?, 'active', ?)`, + userUUID, email, pwHash, dpUUID, now) if err != nil { if isDuplicateKey(err) { return nil, ErrEmailTaken @@ -86,11 +87,11 @@ func (s *SQLStore) CreateUserWithTrial(ctx context.Context, email, pwHash string if err := tx.QueryRowContext(ctx, `SELECT id FROM plans WHERE code='pro'`).Scan(&proID); err != nil { return nil, fmt.Errorf("auth: lookup pro plan: %w", err) } - expires := time.Now().UTC().AddDate(0, 0, trialDays) + expires := now.AddDate(0, 0, trialDays) if _, err := tx.ExecContext(ctx, `INSERT INTO subscriptions (user_id, plan_id, expires_at, source, created_at) - VALUES (?, ?, ?, 'trial', UTC_TIMESTAMP(6))`, - userID, proID, expires); err != nil { + VALUES (?, ?, ?, 'trial', ?)`, + userID, proID, expires, now); err != nil { return nil, fmt.Errorf("auth: insert trial subscription: %w", err) } diff --git a/server/internal/httpapi/account.go b/server/internal/httpapi/account.go index 2b52c44..595b702 100644 --- a/server/internal/httpapi/account.go +++ b/server/internal/httpapi/account.go @@ -77,10 +77,10 @@ func (a *AccountAPI) GetMe(w http.ResponseWriter, r *http.Request) { SELECT p.code, p.max_devices, p.daily_minutes, s.expires_at FROM subscriptions s JOIN plans p ON p.id = s.plan_id - WHERE s.user_id = ? AND s.expires_at > UTC_TIMESTAMP() + WHERE s.user_id = ? AND s.expires_at > ? ORDER BY s.expires_at DESC LIMIT 1 - `, uid).Scan(&planCode, &maxDevices, &dailyMinutes, &expiresAt) + `, uid, time.Now().UTC()).Scan(&planCode, &maxDevices, &dailyMinutes, &expiresAt) if err == sql.ErrNoRows { planCode = "free" if err := a.db.QueryRowContext(ctx,