在GORM中是否完全没有迁移文件?

huangapple go评论86阅读模式
英文:

Is there no migration file at all in GORM?

问题

如果我使用GORM,就没有迁移文件吗?到目前为止,根据我的了解,我在谷歌和搜索中没有找到关于GORM的迁移文件的信息。如果我想生成迁移文件,我必须使用CLI。GORM没有生成迁移文件的原因是为了保护您的数据,它不会删除未使用的列。(https://gorm.io/docs/migration.html#Auto-Migration)

我们如何跟踪更改?在Django中,它会生成一个迁移文件,我们可以在每次迁移时跟踪更改。

提前说一声,如果我对某些事情理解有误,我很抱歉...我刚刚开始学习golang和数据库几天。

英文:

If I use GORM, there is no migration file?? So far, I googled and searched for this information, there is no migration file in GORM based on my understanding, and If I want to generate a migration file I have to use CLI. The reason why the GORM didn't generate a migration file is that " It WON’T delete unused columns to protect your data." (https://gorm.io/docs/migration.html#Auto-Migration)

How do we keep track of the changes? In Django, it generates a migration file, and we can keep track of the changes whenever we migrate.

In advance, I am sorry if I understand something wrong... I am just getting started learning golang and database a few days ago.

答案1

得分: 1

我相信GORM没有你想要的解决方案。GORM团队和其他爱好者提供了一些命令行工具,但它们并不能真正满足我们的需求(是的,我也需要同样的工具)。说到底,只有GIT在使用GORM时才是朋友。

顺便说一句,我在FacebookEnt(Go语言的实体框架)中找到了一个很好的解决方案,它是与Go语言中的数据库交互的一个明显更好的选择。他们内置了一个满足你需求的解决方案——WriteTo函数,它将模式更改写入f而不是直接对数据库运行这些更改。

func main() {
    client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
    if err != nil {
        log.Fatalf("failed connecting to mysql: %v", err)
    }
    defer client.Close()
    ctx := context.Background()
    // 将迁移更改转储到SQL脚本中。
    f, err := os.Create("migrate.sql")
    if err != nil {
        log.Fatalf("create migrate file: %v", err)
    }
    defer f.Close()
    if err := client.Schema.WriteTo(ctx, f); err != nil {
        log.Fatalf("failed printing schema changes: %v", err)
    }
}

或者你可以通过将os.Stdout设置为WriteTo的输出位置,直接将更改打印到终端。

client.Schema.WriteTo(ctx, os.Stdout)

参考 | 数据库迁移 - 离线模式

希望这能帮助你在下次使用Ent时有一个更好的选择,Ent是由Facebook为其自身的需求和规模而创建、开源和维护的。你可能还对Ariel Mashraki的文章《Introducing ent》感兴趣。

英文:

I believe GORM doesn't have the solution you want. There are some CLIs from the GORM team and from other enthusiasts but they really don't do what we actually want (Yep, I needed the same tool as well). At the end of the day, only GIT is the friend in the case of using GORM.

P.S. I found a good solution in Facebook's Ent(The entity framework for Go) which is a significantly better option for interacting with DBs in Go. They have a built-in solution for your needs - the WriteTo function which writes the schema changes to f instead of running them against the database.

func main() {
    client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
    if err != nil {
        log.Fatalf("failed connecting to mysql: %v", err)
    }
    defer client.Close()
    ctx := context.Background()
    // Dump migration changes to an SQL script.
    f, err := os.Create("migrate.sql")
    if err != nil {
        log.Fatalf("create migrate file: %v", err)
    }
    defer f.Close()
    if err := client.Schema.WriteTo(ctx, f); err != nil {
        log.Fatalf("failed printing schema changes: %v", err)
    }
}

Or you can simply print the changes to the terminal by setting os.Stdout as a target output location for WriteTo.

client.Schema.WriteTo(ctx, os.Stdout)

The reference | Database Migration - Offline Mode

I hope, this will help you to have a better option next time by using Ent which is created, open-sourced, and maintained by Facebook for its needs and scale. Also, you might be interested in the post from Ariel Mashraki - Introducing ent.

答案2

得分: 0

我们如何跟踪变更?在Django中,它会生成一个迁移文件,我们可以在每次迁移时跟踪变更。

如果你想要像Django一样跟踪迁移,那么atlasgo可能会有很大帮助。

英文:
How do we keep track of the changes? In Django, it generates a migration 
file, and we can keep track of the changes whenever we migrate.

If you want django like track of migrations then atlasgo could help a lot.

huangapple
  • 本文由 发表于 2022年2月23日 15:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/71232581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定