feat: 实现配置校验与运维命令及优雅停机
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const settingAdminPasswordHash = "admin_password_hash"
|
||||
|
||||
// HasAdminPassword 检查 settings 中是否已有管理员密码哈希。
|
||||
func HasAdminPassword(ctx context.Context, db *sql.DB) (bool, error) {
|
||||
var value string
|
||||
err := db.QueryRowContext(ctx, `SELECT value FROM settings WHERE key = ?`, settingAdminPasswordHash).Scan(&value)
|
||||
if err == sql.ErrNoRows {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return value != "", nil
|
||||
}
|
||||
|
||||
// SetAdminPasswordHash 写入或覆盖管理员密码哈希。
|
||||
func SetAdminPasswordHash(ctx context.Context, db *sql.DB, phc string) error {
|
||||
now := time.Now().UnixMilli()
|
||||
_, err := db.ExecContext(ctx, `
|
||||
INSERT INTO settings(key, value, updated_at) VALUES(?, ?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at
|
||||
`, settingAdminPasswordHash, phc, now)
|
||||
return err
|
||||
}
|
||||
|
||||
// VacuumInto 对打开的写连接执行 VACUUM INTO(可用于运行中备份)。
|
||||
func VacuumInto(ctx context.Context, db *sql.DB, outPath string) error {
|
||||
if outPath == "" {
|
||||
return fmt.Errorf("store: vacuum into path empty")
|
||||
}
|
||||
escaped := strings.ReplaceAll(outPath, "'", "''")
|
||||
_, err := db.ExecContext(ctx, "VACUUM INTO '"+escaped+"'")
|
||||
if err != nil {
|
||||
return fmt.Errorf("vacuum into: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user