-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ORM框架增加AutoMigrate功能 #779
Comments
@sanrentai
|
我建议可以取个巧,写一个插件,将 gf 生成的表结构,映射一份到gorm 的模式定义中,通过 gorm 的功能来实现迁移。这样可以相互独立,实现也简单。 |
初步进度了测试,目前看简单的迁移可以直接实现 添加一个独立的迁移文件,读取配置文件,传给 grom 并进行迁移package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/os/glog"
"strings"
"gmanager/app/model/users"
)
func main() {
database := g.Cfg().Get("database.default.link")
if gutil.IsEmpty(database) {
glog.Fatal("请指定配置文件目录 with --gf.gcfg.path=/opt/config/")
return
}
glog.Info("连接:", database)
db, err := gorm.Open("mysql", strings.Replace(database.(string), "mysql:", "", 1))
defer db.Close()
if err != nil {
panic(err)
}
t := Users.Entity{}
e := db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(t).GetErrors()
if len(e) > 0 {
glog.Fatal(e)
} else {
glog.Info("Success")
}
} 需要手工给每个 Entity 类添加一个
|
github.com/sanrentai/automigrate 目前实现了 mssql 的功能。 |
@sanrentai 这样要迁移的东西太多了 |
在go中定义struct 运行时自动修改数据库的表结构
在gorm框架中有该功能
希望GF也能提供。
The text was updated successfully, but these errors were encountered: