php小编柚子今天为大家分享关于 GORM 运行 Automigrate 的最佳时机。GORM 是一个强大的Go语言ORM库,可以简化数据库操作。Automigrate 是GORM提供的自动迁移工具,可以根据模型定义自动生成数据库表结构。那么何时是使用 GORM 运行 Automigrate 的最佳时机呢?让我们一起来探讨一下。
问题内容
我见过的大多数 go/gorm 示例都显示在打开数据库连接后立即调用 automigrate,包括此处的 gorm 文档。对于 api 服务,这对于每个 api 请求来说都是一次昂贵/需要的调用。因此,我认为,对于 api 服务,应将 automigrate 从常规流程中删除并单独处理。我的理解正确吗?
来自 gorm 文档
...
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// Migrate the schema
db.AutoMigrate(&Product{})
...
解决方法
并不是每个 api 请求都会发生这种情况。差远了。每次启动应用程序时都会发生这种情况,所以基本上:连接到 main
中的数据库,并在那里运行 automigrate
。将连接作为依赖项传递给您的处理程序/服务包/任何您需要的地方。 http 处理程序可以在那里访问它。
基本上是这样的:
package main
func main() {
db, err := gorm.open(sqlite.open("test.db"), &gorm.config{})
if err != nil {
fmt.printf("failed to connect to db: %v", err)
os.exit(1)
}
// see below how this is handled
frepo := foo.new(db) // all repos here
frepo.migrate() // this handles migrations
// create request handlers
fhandler := handlers.newfoo(frepo) // migrations have already been handled
mux := http.newservemux()
mux.handlefunc("/foo/list", fhandler.list) // set up handlers
// start server etc...
}
在某个包中包含与数据库交互的代码,如下所示:
package foo
// the db connection interface as you use it
type connection interface {
create()
find()
automigrate(any)
}
type foo struct {
db connection
}
func new(db connection) *foo {
return &foo{
db: db,
}
}
func (f *foo) migrate() {
f.db.automigrate(&stuff{}) // all types this repo deals with go here
}
func (f *foo) getall() ([]stuff, error) {
ret := []stuff{}
res := f.db.find(&ret)
return ret, res.error
}
然后以合理的方式构建您的处理程序,并向它们提供存储库(又名 foo 包内容):
package handlers
type FooRepo interface {
GetAll() ([]Stuff, error)
}
type FooHandler struct {
repo FooRepo
}
func NewFoo(repo FooRepo) *FooHandler {
return &FooHandler{
repo: repo,
}
}
func (f *FooHandler) List(res http.ResponseWriter, req *http.Request) {
all, err := f.repo.GetAll()
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
// write response as needed
}
每当您部署应用程序的更新版本时,main
函数都会调用 automigrate
,并且应用程序将处理请求,而无需不断地重新连接到数据库或一次又一次尝试处理迁移。
我不知道为什么您认为您的应用程序必须运行每个请求的设置,特别是考虑到您的 main 函数(或从 main
调用的某些函数)显式创建了一个 http 服务器并侦听在请求的特定端口上。在开始侦听请求之前,应处理数据库连接和后续迁移。它不是处理请求的一部分,永远...