| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package cmd
- import (
- "fmt"
- "git.wanbits.cc/sin/flytalk/config"
- "git.wanbits.cc/sin/flytalk/models"
- "git.wanbits.cc/sin/flytalk/tools"
- "github.com/spf13/cobra"
- "io/ioutil"
- "os"
- "path/filepath"
- "strings"
- )
- var installCmd = &cobra.Command{
- Use: "install",
- Short: "install database specified in config file",
- Run: func(cmd *cobra.Command, args []string) {
- install()
- },
- }
- func install() {
- _, err := config.LoadConf(Confile)
- if err != nil {
- panic(err)
- }
- models.InitDb()
- sqlFile := filepath.Join(config.C.Basic.DataDir, "flytalk.sql")
- fileExists, _ := tools.IsFileExist(sqlFile)
- if !fileExists {
- fmt.Printf("sql-file not exists:%v\n", sqlFile)
- os.Exit(1)
- }
- sqls, _ := ioutil.ReadFile(sqlFile)
- sqlArr := strings.Split(string(sqls), "|")
- for _, sql := range sqlArr {
- if sql == "" {
- continue
- }
- models.Execute(sql)
- }
- }
- // gorm 的实现功能不强, 很难通过映射的方式去完整的还原表结构
- //func installByGorm() {
- // _, err := config.LoadConf(Confile)
- // if err != nil {
- // panic(err)
- // }
- // models.InitDb()
- // models.DB.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(
- // &models.Message{},
- // &models.User{},
- // &models.Visitor{},
- // &models.Role{},
- // &models.User_role{},
- //
- // &models.About{},
- // &models.Config{},
- // &models.Ipblack{},
- // &models.Welcome{},
- // )
- // if models.DB.Error != nil {
- // panic(models.DB.Error)
- // }
- //}
|