🎨 flag params binding
This commit is contained in:
39
cmd/cmd.go
Normal file
39
cmd/cmd.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"schisandra-cloud-album/cmd/db"
|
||||
"schisandra-cloud-album/global"
|
||||
)
|
||||
|
||||
type Option struct {
|
||||
DB bool
|
||||
}
|
||||
|
||||
// Parse 解析命令行: go run main.go -db
|
||||
func Parse() Option {
|
||||
// go run main.go -db
|
||||
DB := flag.Bool("db", false, "初始化数据库")
|
||||
// 解析命令
|
||||
flag.Parse()
|
||||
return Option{
|
||||
DB: *DB,
|
||||
}
|
||||
}
|
||||
|
||||
func IsStopWeb(option *Option) bool {
|
||||
if option.DB {
|
||||
global.LOG.Infof("停止web项目")
|
||||
return true
|
||||
}
|
||||
return false // 停止web项目
|
||||
}
|
||||
|
||||
func SwitchOption(option *Option) {
|
||||
if option.DB {
|
||||
// 迁移数据库
|
||||
db.MakeMigration()
|
||||
return
|
||||
}
|
||||
flag.Usage()
|
||||
}
|
27
cmd/db/db.go
Normal file
27
cmd/db/db.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package db
|
||||
|
||||
import "schisandra-cloud-album/global"
|
||||
|
||||
func MakeMigration() {
|
||||
var err error
|
||||
global.LOG.Infof("开始迁移数据库")
|
||||
err = global.DB.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(
|
||||
//&models.ImageModel{},
|
||||
//&models.TagModel{},
|
||||
//&models.MessageModel{},
|
||||
//&models.AdvertModel{},
|
||||
//&models.UserModel{},
|
||||
//&models.CommentModel{},
|
||||
//&models.ArticleModel{},
|
||||
//&models.MenuModel{},
|
||||
//&models.MenuImageModel{},
|
||||
//&models.FeedbackModel{},
|
||||
//&models.LoginDataModel{},
|
||||
)
|
||||
if err != nil {
|
||||
global.LOG.Error("数据库迁移失败: %v", err)
|
||||
return
|
||||
}
|
||||
global.LOG.Info("数据库迁移成功")
|
||||
|
||||
}
|
64
cmd/gen/gen.go
Normal file
64
cmd/gen/gen.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const MysqlDsn = "root:LDQ20020618xxx@(1.95.0.111:3306)/schisandra-cloud-album?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
|
||||
func main() {
|
||||
|
||||
// 初始化连接数据库
|
||||
db, err := gorm.Open(mysql.Open(MysqlDsn))
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot establish db connection: %w", err))
|
||||
}
|
||||
|
||||
// 生成实例
|
||||
g := gen.NewGenerator(gen.Config{
|
||||
// 相对执行`go run`时的路径, 会自动创建目录
|
||||
OutPath: "model",
|
||||
|
||||
// WithDefaultQuery 生成默认查询结构体(作为全局变量使用), 即`Q`结构体和其字段(各表模型)
|
||||
// WithoutContext 生成没有context调用限制的代码供查询
|
||||
// WithQueryInterface 生成interface形式的查询代码(可导出), 如`Where()`方法返回的就是一个可导出的接口类型
|
||||
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface,
|
||||
|
||||
// 表字段可为 null 值时, 对应结体字段使用指针类型
|
||||
FieldNullable: true, // gen pointer when field is nullable
|
||||
|
||||
// 表字段默认值与模型结构体字段零值不一致的字段, 在插入数据时需要赋值该字段值为零值的, 结构体字段须是指针类型才能成功, 即`FieldCoverable:true`配置下生成的结构体字段.
|
||||
// 因为在插入时遇到字段为零值的会被GORM赋予默认值. 如字段`age`表默认值为10, 即使你显式设置为0最后也会被GORM设为10提交.
|
||||
// 如果该字段没有上面提到的插入时赋零值的特殊需要, 则字段为非指针类型使用起来会比较方便.
|
||||
FieldCoverable: false, // gen pointer when field has default value, to fix problem zero value cannot be assign: https://gorm.io/docs/create.html#Default-Values
|
||||
|
||||
// 模型结构体字段的数字类型的符号表示是否与表字段的一致, `false`指示都用有符号类型
|
||||
FieldSignable: false, // detect integer field's unsigned type, adjust generated data type
|
||||
// 生成 gorm 标签的字段索引属性
|
||||
FieldWithIndexTag: false, // gen with gorm index tag
|
||||
// 生成 gorm 标签的字段类型属性
|
||||
FieldWithTypeTag: true, // gen with gorm column type tag
|
||||
})
|
||||
// 设置目标 db
|
||||
g.UseDB(db)
|
||||
|
||||
// 自定义字段的数据类型
|
||||
// 统一数字类型为int64,兼容protobuf
|
||||
dataMap := map[string]func(columnType gorm.ColumnType) (dataType string){
|
||||
"tinyint": func(columnType gorm.ColumnType) (dataType string) { return "int64" },
|
||||
"smallint": func(columnType gorm.ColumnType) (dataType string) { return "int64" },
|
||||
"mediumint": func(columnType gorm.ColumnType) (dataType string) { return "int64" },
|
||||
"bigint": func(columnType gorm.ColumnType) (dataType string) { return "int64" },
|
||||
"int": func(columnType gorm.ColumnType) (dataType string) { return "int64" },
|
||||
}
|
||||
// 要先于`ApplyBasic`执行
|
||||
g.WithDataTypeMap(dataMap)
|
||||
|
||||
g.ApplyBasic(
|
||||
g.GenerateAllTable(),
|
||||
)
|
||||
g.Execute()
|
||||
}
|
Reference in New Issue
Block a user