mirror of https://github.com/synctv-org/synctv
Feat: db update
parent
4b228748a2
commit
74fa432ad7
@ -0,0 +1,56 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/synctv-org/synctv/internal/model"
|
||||
)
|
||||
|
||||
type dbVersion struct {
|
||||
NextVersion string
|
||||
Upgrade func() error
|
||||
}
|
||||
|
||||
var dbVersions = map[string]dbVersion{
|
||||
"0.0.1": {
|
||||
NextVersion: "",
|
||||
Upgrade: nil,
|
||||
},
|
||||
}
|
||||
|
||||
func upgradeDatabase() error {
|
||||
setting := model.Setting{
|
||||
Name: "database_version",
|
||||
Type: model.SettingTypeString,
|
||||
Group: model.SettingGroupDatabase,
|
||||
Value: "0.0.1",
|
||||
}
|
||||
err := FirstOrCreateSettingItemValue(&setting)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
currentVersion := setting.Value
|
||||
version, ok := dbVersions[currentVersion]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
currentVersion = version.NextVersion
|
||||
for currentVersion != "" {
|
||||
version, ok := dbVersions[currentVersion]
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
log.Infof("Upgrading database to version %s", currentVersion)
|
||||
if version.Upgrade != nil {
|
||||
err = version.Upgrade()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = UpdateSettingItemValue("database_version", currentVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
currentVersion = version.NextVersion
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,35 +1,59 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
"github.com/synctv-org/synctv/internal/model"
|
||||
)
|
||||
|
||||
func BeforeSetBoolFunc(s BoolSetting, v bool) error {
|
||||
return db.UpdateSettingItemValue(s.Name(), s.Stringify(v))
|
||||
}
|
||||
|
||||
func BeforeSetStringFunc(s StringSetting, v string) error {
|
||||
return db.UpdateSettingItemValue(s.Name(), s.Stringify(v))
|
||||
}
|
||||
|
||||
func BeforeSetInt64Func(s Int64Setting, v int64) error {
|
||||
return db.UpdateSettingItemValue(s.Name(), s.Stringify(v))
|
||||
}
|
||||
|
||||
func BeforeSetFloat64Func(s Float64Setting, v float64) error {
|
||||
return db.UpdateSettingItemValue(s.Name(), s.Stringify(v))
|
||||
}
|
||||
|
||||
var (
|
||||
DisableCreateRoom = newBoolSetting("disable_create_room", false, model.SettingGroupRoom)
|
||||
RoomMustNeedPwd = newBoolSetting("room_must_need_pwd", false, model.SettingGroupRoom)
|
||||
CreateRoomNeedReview = newBoolSetting("create_room_need_review", false, model.SettingGroupRoom)
|
||||
RoomTTL = newInt64Setting("room_ttl", int64(time.Hour*48), model.SettingGroupRoom)
|
||||
UserMaxRoomCount = newInt64Setting("user_max_room_count", 3, model.SettingGroupRoom)
|
||||
DisableCreateRoom = NewBoolSetting("disable_create_room", false, model.SettingGroupRoom, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
RoomMustNeedPwd = NewBoolSetting("room_must_need_pwd", false, model.SettingGroupRoom, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
CreateRoomNeedReview = NewBoolSetting("create_room_need_review", false, model.SettingGroupRoom, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
RoomTTL = NewInt64Setting("room_ttl", int64(time.Hour*48), model.SettingGroupRoom, WithBeforeSetInt64(BeforeSetInt64Func))
|
||||
UserMaxRoomCount = NewInt64Setting("user_max_room_count", 3, model.SettingGroupRoom, WithBeforeSetInt64(BeforeSetInt64Func))
|
||||
)
|
||||
|
||||
var (
|
||||
DisableUserSignup = newBoolSetting("disable_user_signup", false, model.SettingGroupUser)
|
||||
SignupNeedReview = newBoolSetting("signup_need_review", false, model.SettingGroupUser)
|
||||
DisableUserSignup = NewBoolSetting("disable_user_signup", false, model.SettingGroupUser, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
SignupNeedReview = NewBoolSetting("signup_need_review", false, model.SettingGroupUser, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
)
|
||||
|
||||
var (
|
||||
MovieProxy = newBoolSetting("movie_proxy", true, model.SettingGroupProxy)
|
||||
LiveProxy = newBoolSetting("live_proxy", true, model.SettingGroupProxy)
|
||||
AllowProxyToLocal = newBoolSetting("allow_proxy_to_local", false, model.SettingGroupProxy)
|
||||
MovieProxy = NewBoolSetting("movie_proxy", true, model.SettingGroupProxy, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
LiveProxy = NewBoolSetting("live_proxy", true, model.SettingGroupProxy, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
AllowProxyToLocal = NewBoolSetting("allow_proxy_to_local", false, model.SettingGroupProxy, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
)
|
||||
|
||||
var (
|
||||
// can watch live streams through the RTMP protocol (without authentication, insecure).
|
||||
RtmpPlayer = newBoolSetting("rtmp_player", false, model.SettingGroupRtmp)
|
||||
RtmpPlayer = NewBoolSetting("rtmp_player", false, model.SettingGroupRtmp, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
// default use http header host
|
||||
CustomPublishHost = newStringSetting("custom_publish_host", "", model.SettingGroupRtmp)
|
||||
CustomPublishHost = NewStringSetting("custom_publish_host", "", model.SettingGroupRtmp, WithBeforeSetString(BeforeSetStringFunc))
|
||||
// disguise the .ts file as a .png file
|
||||
TsDisguisedAsPng = newBoolSetting("ts_disguised_as_png", true, model.SettingGroupRtmp)
|
||||
TsDisguisedAsPng = NewBoolSetting("ts_disguised_as_png", true, model.SettingGroupRtmp, WithBeforeSetBool(BeforeSetBoolFunc))
|
||||
)
|
||||
|
||||
var (
|
||||
DatabaseVersion = NewStringSetting("database_version", "0.0.1", model.SettingGroupDatabase, WithBeforeSetString(func(ss StringSetting, s string) error {
|
||||
return errors.New("not support change database version")
|
||||
}))
|
||||
)
|
||||
|
Loading…
Reference in New Issue