122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
package migrate
|
||
|
||
import (
|
||
"SciencesServer/app/common/migrate"
|
||
"SciencesServer/serve/orm"
|
||
"SciencesServer/serve/orm/logic"
|
||
"fmt"
|
||
"github.com/spf13/cobra"
|
||
"gopkg.in/yaml.v2"
|
||
"os"
|
||
)
|
||
|
||
type Config struct {
|
||
Debug bool `json:"debug" yaml:"debug"`
|
||
Mode string `json:"mode" yaml:"mode"`
|
||
MaxLifetime int `json:"max_lifetime" yaml:"max_lifetime"`
|
||
MaxOpenConns int `json:"max_open_conns" yaml:"max_open_conns"`
|
||
MaxIdleConns int `json:"max_idle_conns" yaml:"max_idle_conns"`
|
||
Engines map[string]map[string]interface{} `json:"engines" yaml:"engines"`
|
||
}
|
||
|
||
type mysql struct {
|
||
host string
|
||
port int
|
||
database string
|
||
username string
|
||
password string
|
||
}
|
||
|
||
type sqlite struct {
|
||
path string
|
||
name string
|
||
}
|
||
|
||
var (
|
||
Cmd = &cobra.Command{
|
||
Use: "init",
|
||
Short: "初始化配置",
|
||
Example: "serve init -m mysql -d sciences -u root -p 123456",
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
run()
|
||
},
|
||
}
|
||
file string
|
||
mode string
|
||
|
||
_mysql = new(mysql)
|
||
_sqlite = new(sqlite)
|
||
)
|
||
|
||
func init() {
|
||
Cmd.PersistentFlags().StringVarP(&file, "file", "f", "./default_engine.yaml", "文件存储,现支持yaml文件,默认./default_engine.yaml")
|
||
Cmd.PersistentFlags().StringVarP(&mode, "mode", "m", "mysql", "数据引擎,支持mysql和sqlite,默认mysql")
|
||
Cmd.PersistentFlags().StringVarP(&_mysql.host, "mysql_host", "H", "127.0.0.1", "主机名,默认127.0.0.1")
|
||
Cmd.PersistentFlags().IntVarP(&_mysql.port, "mysql_port", "P", 3306, "端口号,默认为3306")
|
||
Cmd.PersistentFlags().StringVarP(&_mysql.database, "mysql_database", "d", "", "数据库,默认为空")
|
||
Cmd.PersistentFlags().StringVarP(&_mysql.username, "mysql_username", "u", "root", "用户名,默认为root")
|
||
Cmd.PersistentFlags().StringVarP(&_mysql.password, "mysql_password", "p", "", "密码,默认为空")
|
||
Cmd.PersistentFlags().StringVarP(&_sqlite.path, "sqlite_path", "a", "data", "Sqlite文件存放地址,默认data")
|
||
Cmd.PersistentFlags().StringVarP(&_sqlite.name, "sqlite_name", "n", "app.db", "Sqlite文件存放地址,默认app.db")
|
||
}
|
||
|
||
func run() {
|
||
fmt.Println("========================\n=== 初始化项目配置 ===\n========================")
|
||
|
||
// 初始化数据引擎
|
||
engine := orm.NewInstance(
|
||
orm.WithDBMode(mode),
|
||
orm.WithMysqlOption(&logic.Mysql{
|
||
Host: _mysql.host, Port: _mysql.port,
|
||
Username: _mysql.username, Password: _mysql.password,
|
||
Database: _mysql.database, Parameters: "charset=utf8mb4,utf8&parseTime=True&loc=Local",
|
||
}),
|
||
orm.WithSqliteOption(&logic.Sqlite{Path: _sqlite.path, Name: _sqlite.name}),
|
||
).Init()
|
||
|
||
fmt.Println("========================\n=== 数据引擎创建成功 ===\n========================")
|
||
// 迁移数据
|
||
migrate.NewInstance(migrate.WithGormDBOption(engine.Engine)).Handle()
|
||
|
||
if err := saveFile(file, &Config{
|
||
Debug: true,
|
||
Mode: mode,
|
||
MaxLifetime: 3600,
|
||
MaxOpenConns: 2000,
|
||
MaxIdleConns: 1000,
|
||
Engines: map[string]map[string]interface{}{
|
||
"mysql": map[string]interface{}{
|
||
"host": _mysql.host,
|
||
"port": _mysql.port,
|
||
"database": _mysql.database,
|
||
"username": _mysql.username,
|
||
"password": _mysql.password,
|
||
"parameters": "charset=utf8mb4,utf8&parseTime=True&loc=Local",
|
||
},
|
||
"sqlite": map[string]interface{}{
|
||
"path": _sqlite.path,
|
||
"name": _sqlite.name,
|
||
},
|
||
},
|
||
}); err != nil {
|
||
fmt.Errorf("数据初始化文件错误:%v", err)
|
||
return
|
||
}
|
||
fmt.Println("========================\n=== 数据初始化成功 ===\n========================")
|
||
}
|
||
|
||
func saveFile(_file string, data *Config) error {
|
||
file, err := os.OpenFile(_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer file.Close()
|
||
|
||
enc := yaml.NewEncoder(file)
|
||
|
||
err = enc.Encode(data)
|
||
|
||
return err
|
||
}
|