How to do database migration with Beego framework?

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

How to do database migration with Beego framework?

问题

我需要为现有表格添加一个新字段,使用Beego应该如何正确进行此操作?

我熟悉Django的south:首先使用manage.py schema_migration生成迁移脚本,然后执行迁移脚本manage.py migrate

Beego有一个命令bee generate migration用于在database/migrations/xxx.go中生成迁移脚本。但我不明白如何使用这个生成的脚本,它似乎没有与任何东西连接。

而且我没有看到任何关于迁移的文档。

英文:

I need to add a new field to existing table, what is the correct process to do this with Beego?

I am familiar with Django's south: first you generate the migration script with manage.py schema_migration, then execute the migration script manage.py migrate.

Beego has a command bee generate migration to generate migration script in database/migrations/xxx.go. But I don't understand how to use this generated script, it doesn't seem to be connected with anything.

And I don't see any documentation mentioning migration.

答案1

得分: 7

遇到了相同的问题,我正在使用MySQL。以下是我是如何解决的:

使用bee generate创建一个迁移文件:

$ bee generate migration user
2016/06/26 13:36:31 [INFO] 使用'user'作为迁移名称
2016/06/26 13:36:32 [INFO] 生成迁移文件:/path/to/project/database/migrations/20160626_140247_user.go
2016/06/26 13:36:32 [SUCC] 生成成功!

现在文件已经生成,下面是文件的内容:

package main

import (
	"github.com/astaxie/beego/migration"
)

// 不要修改
type User_20160626_140247 struct {
	migration.Migration
}

// 不要修改
func init() {
	m := &User_20160626_140247{}
	m.Created = "20160626_140247"
	migration.Register("User_20160626_140247", m)
}

// 执行迁移
func (m *User_20160626_140247) Up() {
	// 使用 m.SQL("CREATE TABLE ...") 来更新模式

}

// 撤销迁移
func (m *User_20160626_140247) Down() {
	// 使用 m.SQL("DROP TABLE ...") 来撤销模式更新

}

更新UpDown方法。在这些方法的注释中,你可以看到可以调用m.SQL来运行原始的SQL查询。在这里,你可以使用alter命令来更新结构。

完成更改后,可以运行bee migrate来应用这些迁移。以下是示例:

$bee migrate -conn="username:password@tcp(127.0.0.1:3306)/mydb"

希望对你有所帮助。

英文:

Came across the same issue, I'm using MySql. Here is how I've done it-

Created a migration file using bee generate:

$ bee generate migration user
2016/06/26 13:36:31 [INFO] Using 'user' as migration name
2016/06/26 13:36:32 [INFO] Migration file generated: /path/to/project/database/migrations/20160626_140247_user.go
2016/06/26 13:36:32 [SUCC] generate successfully created!

Now the file will be generated and below is the content of the file:

package main

import (
	"github.com/astaxie/beego/migration"
)

// DO NOT MODIFY
type User_20160626_140247 struct {
	migration.Migration
}

// DO NOT MODIFY
func init() {
	m := &User_20160626_140247{}
	m.Created = "20160626_140247"
	migration.Register("User_20160626_140247", m)
}

// Run the migrations
func (m *User_20160626_140247) Up() {
	// use m.SQL("CREATE TABLE ...") to make schema update

}

// Reverse the migrations
func (m *User_20160626_140247) Down() {
	// use m.SQL("DROP TABLE ...") to reverse schema update

}

Updated the Up and Down methods. In comment of these methods you can see m.SQL can be invoked to run raw SQL queries. Here you can use alter commands to update the structure.

Once you are done with the changes, you can run bee migrate to apply these migration. Below is the example-

$bee migrate -conn="username:password@tcp(127.0.0.1:3306)/mydb"

Hope this helps.

答案2

得分: 0

我认为你还需要在beego的迁移文件中添加代码。然后执行以下命令:

bee migrate -driver='mysql' -conn='root:@tcp(127.0.0.1:3306)/test'

英文:

I think you also need to add in migration file beego. Then,
bee migrate -driver='mysql' -conn='root:@tcp(127.0.0.1:3306)/test'

答案3

得分: 0

针对PostgreSQL的示例:

bee migrate -driver=postgres -conn="postgres://my_user:my_pass@my_host:my_port/my_db?sslmode=disable"

英文:

Example for postgres
bee migrate -driver=postgres -conn="postgres://my_user:my_pass@my_host:my_port/my_db?sslmode=disable"

huangapple
  • 本文由 发表于 2015年5月12日 15:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/30184456.html
匿名

发表评论

匿名网友

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

确定