go-zero 代码自动生成辅助工具,支持gorm。
GOPROXY=https://goproxy.cn/,direct go install github.com/qiaogw/gocode@latest
首先 gocode init,在当前目录和dbconf下生成config.yaml,根据业务自行编辑。
支持mysql、postgres
必须有以下字段:
type BaseModel struct {
Id string `json:"id" db:"id" gorm:"column:id;primaryKey;autoIncrement;comment:主键编码"`
}
type ControlBy struct {
CreateBy int64 `json:"createBy" db:"create_by" gorm:"column:create_by;index;comment:创建者"`
UpdateBy int64 `json:"updateBy" db:"update_by" gorm:"column:update_by;index;comment:更新者"`
}
type ModelTime struct {
CreatedAt time.Time `json:"createdAt" db:"created_at" gorm:"column:created_at;comment:创建时间"`
UpdatedAt time.Time `json:"updatedAt" db:"updated_at" gorm:"column:updated_at;comment:最后更新时间"`
DeletedAt gorm.DeletedAt `json:"-" db:"deleted_at" gorm:"index;comment:删除时间"` // 软删除
}
若业务要求更改以上字段,则需要修改生成的 common/golab/model.go文件。 生成的文件最好在goland中编辑,对rpc和api文件利用goctl插件进行生成,插件有bug,api下的handel有包重复import, 此时可以利用goland 进行格式化和清理优化.
在当前backup目录下创建以数据库为名的文件夹,将所有表导出为json文件.
将 backup 的对应数据库json文件导入的 -d 数据库(需要建立对应配置文件,参见初始化,文件名为数据库名称)。