当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.AutoMigrate方法代码示例

本文整理汇总了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
}
开发者ID:teefax,项目名称:tmail,代码行数:8,代码来源:database.go

示例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}
}
开发者ID:8legd,项目名称:qor-qor,代码行数:7,代码来源:database.go

示例3: MakeWebhooksContext

func MakeWebhooksContext(db gorm.DB) (*WebhookContext, error) {
	db.AutoMigrate(&Webhook{})

	return &WebhookContext{
		Db: db,
	}, nil
}
开发者ID:yutopp,项目名称:subako,代码行数:7,代码来源:webhooks.go

示例4: AddTables

func AddTables(db *gorm.DB) error {
	err := db.AutoMigrate(UserFileInfo{}).Error
	if err != nil {
		return err
	}
	return db.AutoMigrate(UserFile{}).Error
}
开发者ID:slnaing07,项目名称:revel-modz,代码行数:7,代码来源:userfiles.go

示例5: MakeMiniLogger

func MakeMiniLogger(db gorm.DB) (*MiniLogger, error) {
	db.AutoMigrate(&MiniLog{})

	return &MiniLogger{
		Db: db,
	}, nil
}
开发者ID:yutopp,项目名称:subako,代码行数:7,代码来源:mini_logger.go

示例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")

}
开发者ID:itpkg,项目名称:chaos,代码行数:9,代码来源:models.go

示例7: MakeDailyTasksContext

func MakeDailyTasksContext(db gorm.DB, point Crontab) (*DailyTasksContext, error) {
	db.AutoMigrate(&DailyTask{})

	return &DailyTasksContext{
		Db:    db,
		Point: point,
	}, nil
}
开发者ID:yutopp,项目名称:subako,代码行数:8,代码来源:daily_tasks.go

示例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}
}
开发者ID:NeoChow,项目名称:qor,代码行数:57,代码来源:publish.go

示例9: InitTable

func InitTable(DB *gorm.DB) *gorm.DB {

	DB.DropTable(&User{})
	DB.AutoMigrate(&User{})

	account.InitTable(DB)

	createTestUser(DB)
	return DB
}
开发者ID:dots-the-game,项目名称:beaver,代码行数:10,代码来源:usermodel.go

示例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
}
开发者ID:xjplke,项目名称:eventhorizon,代码行数:12,代码来源:eventstore.go

示例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")
}
开发者ID:itpkg,项目名称:chaos,代码行数:13,代码来源:engine.go

示例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
}
开发者ID:xjplke,项目名称:eventhorizon,代码行数:13,代码来源:readrepository.go

示例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.
}
开发者ID:bankaiteam,项目名称:apiPOC,代码行数:13,代码来源:server.go

示例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)
}
开发者ID:IIC2173-2015-2-Grupo2,项目名称:news-api,代码行数:50,代码来源:main.go

示例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{})
}
开发者ID:thiagoao-dev,项目名称:GoLang-SimpleApi,代码行数:19,代码来源:migrations.go


注:本文中的github.com/jinzhu/gorm.DB.AutoMigrate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。