英文:
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
时才是朋友。
顺便说一句,我在Facebook
的Ent
(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 CLI
s 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论