// CreateTimestampTable creates the DB table for TUFFile func CreateTimestampTable(db gorm.DB) error { query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&TimestampKey{}) if query.Error != nil { return query.Error } return nil }
//ExtractDBSQL --- func ExtractDBSQL() (gormdb chaospeddler.GormDB) { var err error var db *gorm.DB appEnv, _ := cfenv.Current() service, _ := appEnv.Services.WithName("sql-info") host := fmt.Sprintf("%v", service.Credentials["hostname"]) port := fmt.Sprintf("%v", service.Credentials["port"]) dbname := fmt.Sprintf("%v", service.Credentials["name"]) user := fmt.Sprintf("%v", service.Credentials["username"]) pass := fmt.Sprintf("%v", service.Credentials["password"]) connectionString := user + ":" + pass + "@tcp(" + host + ":" + port + ")" + "/" + dbname + "?charset=utf8&parseTime=True&loc=Local" lo.G.Error("connection string: ", connectionString) if db, err = gorm.Open("mysql", connectionString); err == nil { db.DB() db.DB().Ping() db.DB().SetMaxIdleConns(10) db.DB().SetMaxOpenConns(100) db.SingularTable(true) db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate( new(chaospeddler.ServiceInstance), new(chaospeddler.ServiceBinding), ) gormdb = db } else { lo.G.Error("there was an error connecting to mysql: ", err) panic(err) } return }
func (createResourcePublishInterface) Discard(db *gorm.DB, event publish.PublishEventInterface) error { if event, ok := event.(*publish.PublishEvent); ok { var product Product db.Set("publish:draft_mode", true).First(&product, event.Argument) pb.Discard(&product) } return nil }
func (handler publishJoinTableHandler) Add(h gorm.JoinTableHandlerInterface, db *gorm.DB, source1 interface{}, source2 interface{}) error { // production mode if !isDraftMode(db) { if err := handler.JoinTableHandler.Add(h, db.Set(publishDraftMode, true), source1, source2); err != nil { return err } } return handler.JoinTableHandler.Add(h, db, source1, source2) }
// Create all tables func createTables(db *gorm.DB) { db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.User{}) db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.Item{}) db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.UserFavoriteItem{}) db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.ItemImpression{}) db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.ItemTag{}) db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.Tag{}) db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.UserTag{}) }
func (handler publishJoinTableHandler) Delete(h gorm.JoinTableHandlerInterface, db *gorm.DB, sources ...interface{}) error { // production mode if !IsDraftMode(db) { if err := handler.JoinTableHandler.Delete(h, db.Set(publishDraftMode, true), sources...); err != nil { return err } } return handler.JoinTableHandler.Delete(h, db, sources...) }
// CreateTUFTable creates the DB table for TUFFile func CreateTUFTable(db gorm.DB) error { // TODO: gorm query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&TUFFile{}) if query.Error != nil { return query.Error } query = db.Model(&TUFFile{}).AddUniqueIndex( "idx_gun", "gun", "role", "version") return query.Error }
func checkBrandPosition(db *gorm.DB, t *testing.T, description string) { var brands []Brand if err := db.Set("l10n:mode", "locale").Find(&brands).Error; err != nil { t.Errorf("no error should happen when find brands, but got %v", err) } for i, brand := range brands { if brand.Position != i+1 { t.Errorf("Brand %v(%v)'s position should be %v after %v, but got %v", brand.ID, brand.LanguageCode, i+1, description, brand.Position) } } }
// CreateKeyTable creates the DB table for TUFFile func CreateKeyTable(db gorm.DB) error { query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&Key{}) if query.Error != nil { return query.Error } query = db.Model(&Key{}).AddUniqueIndex( "idx_gun_role", "gun", "role") if query.Error != nil { return query.Error } return nil }
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. }
func move(db *gorm.DB, value sortingInterface, pos int) (err error) { var startedTransaction bool var tx = db.Set("publish:publish_event", true) if t := tx.Begin(); t.Error == nil { startedTransaction = true tx = t } scope := db.NewScope(value) for _, field := range scope.PrimaryFields() { if field.DBName != "id" { tx = tx.Where(fmt.Sprintf("%s = ?", field.DBName), field.Field.Interface()) } } currentPos := value.GetPosition() var results *gorm.DB if pos > 0 { results = tx.Model(newModel(value)). Where("position > ? AND position <= ?", currentPos, currentPos+pos). UpdateColumn("position", gorm.Expr("position - ?", 1)) } else { results = tx.Model(newModel(value)). Where("position < ? AND position >= ?", currentPos, currentPos+pos). UpdateColumn("position", gorm.Expr("position + ?", 1)) } if err = results.Error; err == nil { var rowsAffected = int(results.RowsAffected) if pos < 0 { rowsAffected = -rowsAffected } value.SetPosition(currentPos + rowsAffected) err = tx.Model(value).UpdateColumn("position", gorm.Expr("position + ?", rowsAffected)).Error } // Create Publish Event createPublishEvent(tx, value) if startedTransaction { if err == nil { tx.Commit() } else { tx.Rollback() } } return err }
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{}) }
func initializeGridPieceTable(db gorm.DB) { db.CreateTable(&GridPiece{}) db.Set("gorm:taple_operations", "ENGINE=InnoDB").CreateTable(&GridPiece{}) pieces := []GridPiece{ GridPiece{Type: BlackRook, Y: 1, X: 1}, GridPiece{Type: BlackKnight, Y: 1, X: 2}, GridPiece{Type: BlackBishop, Y: 1, X: 3}, GridPiece{Type: BlackQueen, Y: 1, X: 4}, GridPiece{Type: BlackKing, Y: 1, X: 5}, GridPiece{Type: BlackBishop, Y: 1, X: 6}, GridPiece{Type: BlackKnight, Y: 1, X: 7}, GridPiece{Type: BlackRook, Y: 1, X: 8}, GridPiece{Type: BlackPawn, Y: 2, X: 1}, GridPiece{Type: BlackPawn, Y: 2, X: 2}, GridPiece{Type: BlackPawn, Y: 2, X: 3}, GridPiece{Type: BlackPawn, Y: 2, X: 4}, GridPiece{Type: BlackPawn, Y: 2, X: 5}, GridPiece{Type: BlackPawn, Y: 2, X: 6}, GridPiece{Type: BlackPawn, Y: 2, X: 7}, GridPiece{Type: BlackPawn, Y: 2, X: 8}, GridPiece{Type: WhiteRook, Y: 8, X: 1}, GridPiece{Type: WhiteKnight, Y: 8, X: 2}, GridPiece{Type: WhiteBishop, Y: 8, X: 3}, GridPiece{Type: WhiteQueen, Y: 8, X: 4}, GridPiece{Type: WhiteKing, Y: 8, X: 5}, GridPiece{Type: WhiteBishop, Y: 8, X: 6}, GridPiece{Type: WhiteKnight, Y: 8, X: 7}, GridPiece{Type: WhiteRook, Y: 8, X: 8}, GridPiece{Type: WhitePawn, Y: 7, X: 1}, GridPiece{Type: WhitePawn, Y: 7, X: 2}, GridPiece{Type: WhitePawn, Y: 7, X: 3}, GridPiece{Type: WhitePawn, Y: 7, X: 4}, GridPiece{Type: WhitePawn, Y: 7, X: 5}, GridPiece{Type: WhitePawn, Y: 7, X: 6}, GridPiece{Type: WhitePawn, Y: 7, X: 7}, GridPiece{Type: WhitePawn, Y: 7, X: 8}, } for _, piece := range pieces { db.NewRecord(piece) db.Create(&piece) } }
func initializePieceTable(db gorm.DB) { db.CreateTable(&Piece{}) db.Set("gorm:taple_operations", "ENGINE=InnoDB").CreateTable(&Piece{}) pieces := []Piece{ Piece{Type: BlackRook, Top: 82, Left: 79}, Piece{Type: BlackKnight, Top: 82, Left: 154}, Piece{Type: BlackBishop, Top: 82, Left: 234}, Piece{Type: BlackQueen, Top: 82, Left: 308}, Piece{Type: BlackKing, Top: 82, Left: 383}, Piece{Type: BlackBishop, Top: 82, Left: 458}, Piece{Type: BlackKnight, Top: 82, Left: 533}, Piece{Type: BlackRook, Top: 82, Left: 608}, Piece{Type: BlackPawn, Top: 2, Left: 79}, Piece{Type: BlackPawn, Top: 160, Left: 154}, Piece{Type: BlackPawn, Top: 160, Left: 234}, Piece{Type: BlackPawn, Top: 160, Left: 308}, Piece{Type: BlackPawn, Top: 160, Left: 383}, Piece{Type: BlackPawn, Top: 160, Left: 458}, Piece{Type: BlackPawn, Top: 160, Left: 533}, Piece{Type: BlackPawn, Top: 160, Left: 608}, Piece{Type: WhiteRook, Top: 611, Left: 79}, Piece{Type: WhiteKnight, Top: 611, Left: 154}, Piece{Type: WhiteBishop, Top: 611, Left: 234}, Piece{Type: WhiteQueen, Top: 611, Left: 308}, Piece{Type: WhiteKing, Top: 611, Left: 383}, Piece{Type: WhiteBishop, Top: 611, Left: 458}, Piece{Type: WhiteKnight, Top: 611, Left: 533}, Piece{Type: WhiteRook, Top: 611, Left: 608}, Piece{Type: WhitePawn, Top: 533, Left: 79}, Piece{Type: WhitePawn, Top: 533, Left: 154}, Piece{Type: WhitePawn, Top: 533, Left: 234}, Piece{Type: WhitePawn, Top: 533, Left: 308}, Piece{Type: WhitePawn, Top: 533, Left: 383}, Piece{Type: WhitePawn, Top: 533, Left: 458}, Piece{Type: WhitePawn, Top: 533, Left: 533}, Piece{Type: WhitePawn, Top: 533, Left: 608}, } for _, piece := range pieces { db.NewRecord(piece) db.Create(&piece) } }
func checkHasProductInLocale(db *gorm.DB, locale string, t *testing.T) { var count int if db.Set("l10n:locale", locale).Count(&count); count != 1 { t.Errorf("should has only one product for locale %v, but found %v", locale, count) } }
func (createResourcePublishInterface) Publish(db *gorm.DB, event *publish.PublishEvent) error { var product Product db.Set("publish:draft_mode", true).First(&product, event.Argument) pb.Publish(&product) return nil }
// CreateChangefeedTable creates the DB table for Changefeed func CreateChangefeedTable(db gorm.DB) error { query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&Change{}) return query.Error }