2021-11-22 19:53:42 +08:00
|
|
|
package invoker
|
2020-12-06 16:49:49 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-11-22 19:53:42 +08:00
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
2020-12-06 16:49:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DBScript interface {
|
|
|
|
Run(ctx context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
var availableScripts = make(map[string]DBScript)
|
|
|
|
|
|
|
|
func RunDBScript(name string, ctx context.Context) error {
|
|
|
|
if script, ok := availableScripts[name]; ok {
|
2021-11-22 19:53:42 +08:00
|
|
|
util.Log().Info("开始执行数据库脚本 [%s]", name)
|
2020-12-06 16:49:49 +08:00
|
|
|
script.Run(ctx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("数据库脚本 [%s] 不存在", name)
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:53:42 +08:00
|
|
|
func Register(name string, script DBScript) {
|
2020-12-06 16:49:49 +08:00
|
|
|
availableScripts[name] = script
|
|
|
|
}
|
2021-11-22 19:53:42 +08:00
|
|
|
|
|
|
|
func ListPrefix(prefix string) []string {
|
|
|
|
var scripts []string
|
|
|
|
for name := range availableScripts {
|
|
|
|
if name[:len(prefix)] == prefix {
|
|
|
|
scripts = append(scripts, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scripts
|
|
|
|
}
|