Skip to content
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

Closed
sanrentai opened this issue Jul 6, 2020 · 5 comments
Closed

ORM框架增加AutoMigrate功能 #779

sanrentai opened this issue Jul 6, 2020 · 5 comments
Labels
discuss We need discuss to make decision. feature rejected The proposal or PR is not accepted, which might be conflicted with our design or plan.

Comments

@sanrentai
Copy link
Contributor

在go中定义struct 运行时自动修改数据库的表结构

在gorm框架中有该功能

希望GF也能提供。

@gqcn gqcn added discuss We need discuss to make decision. feature labels Jul 6, 2020
@gqcn
Copy link
Member

gqcn commented Jul 6, 2020

@sanrentai migrate功能其实有的小伙伴提过,gform一直没有增加该功能,主要是考虑到:

  1. 是通过代码生成数据表虽然对一两个人开发的项目比较方便,但是是一种不太严谨的做法,在团队开发中往往都是独立设计数据表,然后单向将数据表字段更新到开发语言的class/struct中。
  2. 该功能需要复杂的tag定义,当项目逻辑比较复杂时不便于维护,也不符合gf框架简便、轻量、强大、易维护的设计理念。

@forcemeter
Copy link

我建议可以取个巧,写一个插件,将 gf 生成的表结构,映射一份到gorm 的模式定义中,通过 gorm 的功能来实现迁移。这样可以相互独立,实现也简单。

@forcemeter
Copy link

forcemeter commented Jul 19, 2020

初步进度了测试,目前看简单的迁移可以直接实现

添加一个独立的迁移文件,读取配置文件,传给 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 类添加一个 TableName 方法

func (r *Entity) TableName() string {
	return "user"
}

目前初步测试,存在的问题有
1,无法处理 gtime
2,驼峰字段映射有差异,如表中的md5字段,gf 会映射为 md_5,gorm 映射为 md5
3,无法处理字符长度,因为 gorm 是在 gorm tag 中对类型、长度、默认值进行约定的

因为我很需要自动迁移的功能,平时的项目中也都在用自动迁移的功能,所以我有计划来推进这个事情。

我建议 gf 在 model 定义时,可以参考 gorm 的定义,最好是兼容,这样可以方便大家进行代码迁移。

@sanrentai @gqcn

@sanrentai
Copy link
Contributor Author

github.com/sanrentai/automigrate

目前实现了 mssql 的功能。

@forcemeter
Copy link

@sanrentai 这样要迁移的东西太多了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss We need discuss to make decision. feature rejected The proposal or PR is not accepted, which might be conflicted with our design or plan.
Projects
None yet
Development

No branches or pull requests

3 participants