Feat: Decide whether to do database migration based on version.lock

This commit is contained in:
HFO4
2019-11-09 19:04:48 +08:00
parent c5493fd1f1
commit e858cbe2cf
18 changed files with 127 additions and 26 deletions

View File

@@ -1,11 +1,11 @@
package conf
import (
"Cloudreve/pkg/util"
"cloudreve/pkg/util"
"github.com/go-ini/ini"
)
// Database 数据库
// database 数据库
type database struct {
Type string
User string
@@ -19,6 +19,13 @@ var DatabaseConfig = &database{
Type: "UNSET",
}
// system 系统通用配置
type system struct {
Debug bool
}
var SystemConfig = &system{}
var cfg *ini.File
// Init 初始化配置文件
@@ -30,9 +37,16 @@ func Init(path string) {
if err != nil {
util.Log().Panic("无法解析配置文件 '%s': ", path, err)
}
err = mapSection("Database", DatabaseConfig)
if err != nil {
util.Log().Warning("配置文件 %s 分区解析失败: ", "Database", err)
sections := map[string]interface{}{
"Database": DatabaseConfig,
"System": SystemConfig,
}
for sectionName, sectionStruct := range sections {
err = mapSection(sectionName, sectionStruct)
if err != nil {
util.Log().Warning("配置文件 %s 分区解析失败: ", sectionName, err)
}
}
}

View File

@@ -78,5 +78,4 @@ TablePrefix = v3_`
err = mapSection("Database", DatabaseConfig)
asserts.NoError(err)
// TODO 类型不匹配测试
}

11
pkg/conf/version.go Normal file
View File

@@ -0,0 +1,11 @@
package conf
import "io/ioutil"
const BackendVersion = string("3.0.0-b")
// WriteVersionLock 将当前版本信息写入 version.lock
func WriteVersionLock() error {
err := ioutil.WriteFile("version.lock", []byte(BackendVersion), 0644)
return err
}

13
pkg/util/io.go Normal file
View File

@@ -0,0 +1,13 @@
package util
import "os"
// Exists reports whether the named file or directory exists.
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}