Reputation: 726
I have a project that use golang-revel
and use GORM
to connect to DB. I don't know how to migrate multiple databases. I have successfuly migrate with one database but struggling to migrate more than 1 database
routes conf
GET /:db/migrate App.Migrate
app.go
func (c App) Migrate() revel.Result {
messages := c.Migration(nil)
return c.Render(messages)
}
base.go
func (c *BaseController) Migration(tables []interface{}) []string {
var (
md = &m.Model{}
mh = &MigrationHelpers{}
mo = &m.Module{}
pr = &m.Privilege{}
messages = []string{}
)
messages = append(messages, "* Running Auto Migration")
// Migrate tables
if tables == nil {
for i, table := range md.Tables() {
messages = append(messages, "* Migrating Table "+i)
if err := c.Txn.AutoMigrate(table).Error; err != nil {
messages = append(messages, err.Error())
}
}
} else {
c.Txn.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(tables)
}
messages = append(messages, "* SCRIPTS AutoMigration Done!")
// Detect New Modules
mh.traverseModule(mo.List(c.Controller), c)
messages = append(messages, "* New MODULES Detection Done!")
// Detect New Privilege
mh.traversePrivilege(pr.List(c.Controller), c)
messages = append(messages, "* New PRIVILEGES Detection Done!")
for _, message := range messages {
fmt.Println(message)
}
return messages
}
Upvotes: 1
Views: 333