parent
96b84bb5e5
commit
9fc08292a0
@ -0,0 +1,9 @@
|
|||||||
|
package scripts
|
||||||
|
|
||||||
|
import "github.com/cloudreve/Cloudreve/v3/models/scripts/invoker"
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
invoker.Register("ResetAdminPassword", ResetAdminPassword(0))
|
||||||
|
invoker.Register("CalibrateUserStorage", UserStorageCalibration(0))
|
||||||
|
invoker.Register("UpgradeTo3.4.0", UpgradeTo340(0))
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package scripts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpgradeTo340 int
|
||||||
|
|
||||||
|
// Run upgrade from older version to 3.4.0
|
||||||
|
func (script UpgradeTo340) Run(ctx context.Context) {
|
||||||
|
// 取回老版本 aria2 设定
|
||||||
|
old := model.GetSettingByType([]string{"aria2"})
|
||||||
|
if len(old) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入到新版本的节点设定
|
||||||
|
n, err := model.GetNodeByID(1)
|
||||||
|
if err != nil {
|
||||||
|
util.Log().Error("找不到主机节点, %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
n.Aria2Enabled = old["aria2_rpcurl"] != ""
|
||||||
|
n.Aria2OptionsSerialized.Options = old["aria2_options"]
|
||||||
|
n.Aria2OptionsSerialized.Server = old["aria2_rpcurl"]
|
||||||
|
|
||||||
|
interval, err := strconv.Atoi(old["aria2_interval"])
|
||||||
|
if err != nil {
|
||||||
|
interval = 10
|
||||||
|
}
|
||||||
|
n.Aria2OptionsSerialized.Interval = interval
|
||||||
|
n.Aria2OptionsSerialized.TempPath = old["aria2_temp_path"]
|
||||||
|
n.Aria2OptionsSerialized.Token = old["aria2_token"]
|
||||||
|
if err := model.DB.Save(&n).Error; err != nil {
|
||||||
|
util.Log().Error("无法保存主机节点 Aria2 配置信息, %s", err)
|
||||||
|
} else {
|
||||||
|
model.DB.Where("type = ?", "aria2").Delete(model.Setting{})
|
||||||
|
util.Log().Info("Aria2 配置信息已成功迁移至 3.4.0+ 版本的模式")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
package scripts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUpgradeTo340_Run(t *testing.T) {
|
||||||
|
a := assert.New(t)
|
||||||
|
script := UpgradeTo340(0)
|
||||||
|
|
||||||
|
// skip
|
||||||
|
{
|
||||||
|
mock.ExpectQuery("SELECT(.+)settings").WillReturnRows(sqlmock.NewRows([]string{"name"}))
|
||||||
|
script.Run(context.Background())
|
||||||
|
a.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// node not found
|
||||||
|
{
|
||||||
|
mock.ExpectQuery("SELECT(.+)settings").WillReturnRows(sqlmock.NewRows([]string{"name"}).AddRow("1"))
|
||||||
|
mock.ExpectQuery("SELECT(.+)nodes").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||||
|
script.Run(context.Background())
|
||||||
|
a.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// success
|
||||||
|
{
|
||||||
|
mock.ExpectQuery("SELECT(.+)settings").WillReturnRows(sqlmock.NewRows([]string{"name", "value"}).
|
||||||
|
AddRow("aria2_rpcurl", "expected_aria2_rpcurl").
|
||||||
|
AddRow("aria2_interval", "expected_aria2_interval").
|
||||||
|
AddRow("aria2_temp_path", "expected_aria2_temp_path").
|
||||||
|
AddRow("aria2_token", "expected_aria2_token").
|
||||||
|
AddRow("aria2_options", "{}"))
|
||||||
|
|
||||||
|
mock.ExpectQuery("SELECT(.+)nodes").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
script.Run(context.Background())
|
||||||
|
a.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// failed
|
||||||
|
{
|
||||||
|
mock.ExpectQuery("SELECT(.+)settings").WillReturnRows(sqlmock.NewRows([]string{"name", "value"}).
|
||||||
|
AddRow("aria2_rpcurl", "expected_aria2_rpcurl").
|
||||||
|
AddRow("aria2_interval", "expected_aria2_interval").
|
||||||
|
AddRow("aria2_temp_path", "expected_aria2_temp_path").
|
||||||
|
AddRow("aria2_token", "expected_aria2_token").
|
||||||
|
AddRow("aria2_options", "{}"))
|
||||||
|
|
||||||
|
mock.ExpectQuery("SELECT(.+)nodes").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").WillReturnError(errors.New("error"))
|
||||||
|
mock.ExpectRollback()
|
||||||
|
script.Run(context.Background())
|
||||||
|
a.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue