How to migrate a database using Beego?

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

I am familiar with Django south: first you create a script migration using manage.py schema_migration, and then execute a script migration manage.py migrate.

Beego has a team bee generate migrationto create a script's migration database/migrations/xxx.go. But I do not understand how to use this generated script, it seems to be unrelated to anything.

And I do not see any documentation regarding migration.

+4
source share
2 answers

Going through the same issue, I am using MySql. Here is how I did it -

Created a migration file with 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!

, :

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

}

Up Down. , m.SQL SQL-. alter .

, bee migrate, . -

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

, .

+2

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

0

All Articles