需要测试相关功能

This commit is contained in:
2026-07-28 20:47:41 +08:00
parent 6904e4993f
commit ef6c75aef9
5 changed files with 231 additions and 260 deletions
+18 -14
View File
@@ -22,14 +22,13 @@ var DB *sql.DB
// ============================================================
const (
SchemaVersion = "v2" // 当前数据库 Schema 版本(与 schemaHistory 中的版本对应)
SchemaVersion = "v2" // 当前数据库 Schema 版本
)
// ============================================================
// 数据模型
// ============================================================
// GlobalConfig 全局配置表
type GlobalConfig struct {
ID int `json:"id"`
ServerAddr string `json:"serverAddr"`
@@ -42,10 +41,9 @@ type GlobalConfig struct {
HeartbeatInterval int `json:"heartbeatInterval"`
HeartbeatTimeout int `json:"heartbeatTimeout"`
PoolCount int `json:"poolCount"`
WireProtocolV2 bool `json:"wireProtocolV2"` // v2 协议全局开关
WireProtocolV2 bool `json:"wireProtocolV2"`
}
// Proxy 隧道表(不含 wire_protocol_v2
type Proxy struct {
ID int `json:"id"`
Name string `json:"name"`
@@ -56,7 +54,6 @@ type Proxy struct {
Enabled bool `json:"enabled"`
}
// User 用户表
type User struct {
ID int `json:"id"`
Username string `json:"username"`
@@ -186,7 +183,6 @@ func createTables() error {
// 迁移引擎
// ============================================================
// getCurrentSchemaVersion 读取当前数据库的 Schema 版本
func getCurrentSchemaVersion() string {
var version string
err := DB.QueryRow("SELECT value FROM app_config WHERE key = 'schema_version'").Scan(&version)
@@ -205,7 +201,6 @@ func getCurrentSchemaVersion() string {
return version
}
// setSchemaVersion 更新 Schema 版本
func setSchemaVersion(version string) error {
_, err := DB.Exec(`
INSERT INTO app_config (key, value) VALUES ('schema_version', ?)
@@ -214,7 +209,6 @@ func setSchemaVersion(version string) error {
return err
}
// backupDatabase 备份数据库文件
func backupDatabase() (string, error) {
src := "./frpc-console.db"
if _, err := os.Stat(src); os.IsNotExist(err) {
@@ -244,7 +238,6 @@ func backupDatabase() (string, error) {
return dst, nil
}
// restoreDatabase 从备份恢复数据库
func restoreDatabase(backupPath string) error {
srcFile, err := os.Open(backupPath)
if err != nil {
@@ -266,7 +259,6 @@ func restoreDatabase(backupPath string) error {
return nil
}
// runMigrations 执行迁移
func runMigrations() error {
currentVer := getCurrentSchemaVersion()
targetVer := SchemaVersion
@@ -274,7 +266,14 @@ func runMigrations() error {
log.Printf("📌 当前数据库 Schema: %s, 目标版本: %s", currentVer, targetVer)
if currentVer == targetVer {
log.Println("✅ Schema 已是最新,无需迁移")
// 检查数据库是否包含有效数据
var userCount int
err := DB.QueryRow("SELECT COUNT(*) FROM users").Scan(&userCount)
if err != nil || userCount == 0 {
log.Println(" 数据库为空或无效,无需迁移,直接初始化")
return nil
}
log.Println("✅ Schema 已是最新,数据库有效")
return nil
}
@@ -297,6 +296,14 @@ func runMigrations() error {
if currentSchema == nil || schemaVersionsEqual(currentSchema, targetSchema) {
log.Println(" 迁移类型: 轻量复制(Schema 无变更)")
// 验证数据库是否有效
var userCount int
err := DB.QueryRow("SELECT COUNT(*) FROM users").Scan(&userCount)
if err != nil || userCount == 0 {
log.Println(" 数据库为空或无效,跳过迁移,直接初始化")
return nil
}
log.Println(" 数据库有效,继续使用")
} else {
log.Println(" 迁移类型: 重型迁移(Schema 有变更,新建表 + 搬数据)")
if err := heavyMigration(currentSchema, targetSchema); err != nil {
@@ -318,7 +325,6 @@ func runMigrations() error {
return nil
}
// heavyMigration 重型迁移
func heavyMigration(oldDef, newDef *SchemaVersionDef) error {
if oldDef == nil {
return fmt.Errorf("旧 Schema 定义为空,无法执行重型迁移")
@@ -366,7 +372,6 @@ func heavyMigration(oldDef, newDef *SchemaVersionDef) error {
return nil
}
// buildCreateTableSQL 根据 Schema 定义生成 CREATE TABLE 语句
func buildCreateTableSQL(tableName string, def *SchemaVersionDef) string {
var cols []string
var primaryKey string
@@ -400,7 +405,6 @@ func buildCreateTableSQL(tableName string, def *SchemaVersionDef) string {
return fmt.Sprintf("CREATE TABLE %s (\n %s\n)", tableName, strings.Join(cols, ",\n "))
}
// buildInsertSQL 构建 INSERT INTO new_table SELECT ... FROM old_table
func buildInsertSQL(oldTable, newTable string, oldDef, newDef *SchemaVersionDef) (string, error) {
newCols := make([]string, 0, len(newDef.Columns))
for name := range newDef.Columns {