本文整理汇总了Golang中github.com/jinzhu/gorm.DB.AutoMigrate方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.AutoMigrate方法的具体用法?Golang DB.AutoMigrate怎么用?Golang DB.AutoMigrate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.DB
的用法示例。
在下文中一共展示了DB.AutoMigrate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: AutoMigrateDB
// AutoMigrateDB will keep tables reflecting structs
func AutoMigrateDB(DB gorm.DB) error {
// if tables exists check if they reflects struts
if err := DB.AutoMigrate(&User{}, &Alias{}, &RcptHost{}, &RelayIpOk{}, &QMessage{}, &Route{}, &DkimConfig{}).Error; err != nil {
return errors.New("Unable autoMigrateDB - " + err.Error())
}
return nil
}
示例2: New
func New(db *gorm.DB) i18n.Backend {
db.AutoMigrate(&Translation{})
if err := db.Model(&Translation{}).AddUniqueIndex("idx_translations_key_with_locale", "locale", "key").Error; err != nil {
fmt.Printf("Failed to create unique index for translations key & locale, got: %v\n", err.Error())
}
return &Backend{DB: db}
}
示例3: MakeWebhooksContext
func MakeWebhooksContext(db gorm.DB) (*WebhookContext, error) {
db.AutoMigrate(&Webhook{})
return &WebhookContext{
Db: db,
}, nil
}
示例4: AddTables
func AddTables(db *gorm.DB) error {
err := db.AutoMigrate(UserFileInfo{}).Error
if err != nil {
return err
}
return db.AutoMigrate(UserFile{}).Error
}
示例5: MakeMiniLogger
func MakeMiniLogger(db gorm.DB) (*MiniLogger, error) {
db.AutoMigrate(&MiniLog{})
return &MiniLogger{
Db: db,
}, nil
}
示例6: Migrate
//Migrate db:migrate
func (p *Engine) Migrate(db *gorm.DB) {
db.AutoMigrate(
&Article{}, &Tag{}, &Comment{}, &Attachment{},
)
db.Model(&Tag{}).AddUniqueIndex("idx_cms_tag_name_lang", "name", "lang")
}
示例7: MakeDailyTasksContext
func MakeDailyTasksContext(db gorm.DB, point Crontab) (*DailyTasksContext, error) {
db.AutoMigrate(&DailyTask{})
return &DailyTasksContext{
Db: db,
Point: point,
}, nil
}
示例8: New
func New(db *gorm.DB) *Publish {
tableHandler := gorm.DefaultTableNameHandler
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
tableName := tableHandler(db, defaultTableName)
if db != nil {
if IsPublishableModel(db.Value) {
// Set join table handler
typ := utils.ModelType(db.Value)
if !injectedJoinTableHandler[typ] {
injectedJoinTableHandler[typ] = true
scope := db.NewScope(db.Value)
for _, field := range scope.GetModelStruct().StructFields {
if many2many := utils.ParseTagOption(field.Tag.Get("gorm"))["MANY2MANY"]; many2many != "" {
db.SetJoinTableHandler(db.Value, field.Name, &publishJoinTableHandler{})
db.AutoMigrate(db.Value)
}
}
}
var forceDraftTable bool
if forceDraftTable, ok := db.Get("publish:force_draft_table"); ok {
if forceMode, ok := forceDraftTable.(bool); ok && forceMode {
forceDraftTable = true
}
}
if IsDraftMode(db) || forceDraftTable {
return DraftTableName(tableName)
}
}
}
return tableName
}
db.AutoMigrate(&PublishEvent{})
db.Callback().Create().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
db.Callback().Create().Before("gorm:commit_or_rollback_transaction").
Register("publish:sync_to_production_after_create", syncCreateFromProductionToDraft)
db.Callback().Create().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)
db.Callback().Delete().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
db.Callback().Delete().Replace("gorm:delete", deleteScope)
db.Callback().Delete().Before("gorm:commit_or_rollback_transaction").
Register("publish:sync_to_production_after_delete", syncDeleteFromProductionToDraft)
db.Callback().Delete().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)
db.Callback().Update().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
db.Callback().Update().Before("gorm:commit_or_rollback_transaction").
Register("publish:sync_to_production", syncUpdateFromProductionToDraft)
db.Callback().Update().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)
db.Callback().RowQuery().Register("publish:set_table_in_draft_mode", setTableAndPublishStatus(false))
db.Callback().Query().Before("gorm:query").Register("publish:set_table_in_draft_mode", setTableAndPublishStatus(false))
return &Publish{DB: db}
}
示例9: InitTable
func InitTable(DB *gorm.DB) *gorm.DB {
DB.DropTable(&User{})
DB.AutoMigrate(&User{})
account.InitTable(DB)
createTestUser(DB)
return DB
}
示例10: NewSqlEventStore
// NewSqlEventStore creates a new SqlEventStore.
func NewSqlEventStore(eventBus EventBus, db gorm.DB) (*SqlEventStore, error) {
db.AutoMigrate(&sqlAggregateRecord{})
db.AutoMigrate(&sqlEventRecord{})
s := &SqlEventStore{
eventBus: eventBus,
factories: make(map[string]func() Event),
db: db,
}
return s, nil
}
示例11: Migrate
//Migrate db:migrate
func (p *Engine) Migrate(db *gorm.DB) {
i18n.Migrate(db)
db.AutoMigrate(
&Setting{}, &Notice{},
&User{}, &Role{}, &Permission{}, &Log{},
)
db.Model(&User{}).AddUniqueIndex("idx_user_provider_type_id", "provider_type", "provider_id")
db.Model(&Role{}).AddUniqueIndex("idx_roles_name_resource_type_id", "name", "resource_type", "resource_id")
db.Model(&Permission{}).AddUniqueIndex("idx_permissions_user_role", "user_id", "role_id")
}
示例12: NewSqlReadRepository
// NewMongoReadRepository creates a new MongoReadRepository.
func NewSqlReadRepository(db gorm.DB, factory func() interface{}) (*SqlReadRepository, error) {
r := &SqlReadRepository{
db: db,
factory: factory,
}
model := factory()
db.AutoMigrate(model)
return r, nil
}
示例13: migrateDb
func migrateDb(db gorm.DB) {
// Create table
db.CreateTable(&model.User{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&model.User{})
// Automating Migration
db.AutoMigrate(&model.User{})
db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&model.User{})
// Feel free to change your struct, AutoMigrate will keep your database up-to-date.
// AutoMigrate will ONLY add *new columns* and *new indexes*,
// WON'T update current column's type or delete unused columns, to protect your data.
// If the table is not existing, AutoMigrate will create the table automatically.
}
示例14: main
func main() {
// Neo4J Database setup
var db *neoism.Database
if os.Getenv("NEO4J_HOST") != "" && os.Getenv("NEO4J_PORT") != "" {
if connected, err := database.Connect(
os.Getenv("NEO4J_USER"),
os.Getenv("NEO4J_PASS"),
os.Getenv("NEO4J_HOST"),
os.Getenv("NEO4J_PORT"),
); err != nil {
log.Fatal(err)
} else {
db = connected
}
}
// Postgres Database setup
var pgdb *gorm.DB
if connected, err := gorm.Open("postgres", "user=postgres dbname=newsapi sslmode=disable host=db"); err != nil {
// if connected, err := gorm.Open("postgres", "user=newsapi dbname=newsapi sslmode=disable"); err != nil {
log.Fatal(err)
} else {
pgdb = &connected
pgdb.AutoMigrate(&models.User{})
}
// Setup analytics client
var analytics *ga.Client
if token := os.Getenv("ANALYTICS_TOKEN"); token == "" {
fmt.Printf("Analytics token not provided.\n")
} else if client, err := ga.NewClient(token); err != nil {
log.Fatal(err)
} else {
fmt.Printf("Analytics activated.\n")
analytics = client
}
// Setup environment
if environment := os.Getenv("ENVIRONMENT"); environment == "PRODUCTION" {
gin.SetMode(gin.ReleaseMode)
} else {
gin.SetMode(gin.DebugMode)
if db != nil {
db.Session.Log = true
}
}
// Start
Server(db, pgdb, analytics).Run(port)
}
示例15: MigrationsUp
func MigrationsUp(db *gorm.DB) {
log.Println("Starting migrations")
// Drop table
db.DropTable(&User{}, &Phone{}, &Email{}, &Language{})
// Create table
//db.CreateTable(&User{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&User{}, &Phone{}, &Email{}, &Language{})
// ModifyColumn
//db.Model(&User{}).ModifyColumn("description", "text")
// DropColumn
//db.Model(&User{}).DropColumn("description")
// Automating Migration
db.AutoMigrate(&User{}, &Phone{}, &Email{}, &Language{})
}