4

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.

NeoWang
  • 17,361
  • 24
  • 78
  • 126

3 Answers3

7

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.

ritesh
  • 2,245
  • 2
  • 25
  • 37
  • I'm having trouble with this, is there a way this migration script will work without having to actually type in any RAW SQL in the up and down functions? I didn't seem to have this issue when I was using gorm so I'm asking. – Eduardo Castillo Feb 13 '17 at 21:58
  • @Ritesh What if I want to run migrations in my prod server? mysql remote access is revoked. I've ssh access, though. Is there any way other than allowing mysql remote access? – musafar006 Jun 17 '19 at 10:03
0

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'

Kyaw Myint Thein
  • 540
  • 4
  • 10
  • 2
    Please [edit] with more information. Code-only and "try this" answers are [discouraged](//meta.stackexchange.com/questions/196187), because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge. – Mogsdad Oct 08 '15 at 19:53
  • Is it any way to apply migrations without entering db credentials every time? – Oleksandr Savchenko May 02 '17 at 12:51
0

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

vitams
  • 585
  • 6
  • 7