问题描述
有没有办法获取包中的所有结构(在本例中为实体)以生成自动迁移列表?
我拆分了实体和迁移包,现在我们有一个专用于 gorm 使用的所有实体的包,这就是我目前管理迁移的方式,对添加的每个新实体完全手动,我们必须修改添加新实体的迁移主代码到migrationsList
package entity
type ClockOffset struct {
Model
ID string `json:"id" gorm:"primarykey"`
LastSyncclock uint32 `json:"last_sync_clock" gorm:"not null"`
LastSyncTimestamp uint32 `json:"last_sync_timestamp" gorm:"not null"`
}
type InflightMessage struct {
Model
ID string `json:"id" gorm:"primarykey"`
Token uint8 `gorm:"not null;uniqueIndex:idx_gateway_id_token"`
Tag string `gorm:"not null"`
}
// ... other entities ...
package main
func main() {
var migrationsList []*gormigrate.Migration
migrationsList = append(migrationsList,&gormigrate.Migration{
ID: "001-ClockOffset-CreateTable",Migrate: func(tx repo.Database) error {
return tx.Migrator().CreateTable(&entity.ClockOffset{})
},Rollback: func(tx repo.Database) error {
return tx.Migrator().DropTable(&entity.ClockOffset{})
},})
migrationsList = append(migrationsList,&gormigrate.Migration{
ID: "002-InflightMessage-CreateTable",Migrate: func(tx repo.Database) error {
return tx.Migrator().CreateTable(&entity.InflightMessage{})
},Rollback: func(tx repo.Database) error {
return tx.Migrator().DropTable(&entity.InflightMessage{})
},})
m := gormigrate.New(repo.Db(),gormigrate.DefaultOptions,migrationsList)
if err := m.Migrate(); err != nil {
panic(err)
}
}
我想找到一种自动方式来读取包实体循环中存在的所有结构并将它们附加到 migrationsList
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)