Best Testkube code snippet using migrator.Run
migrator.gorm.go
Source:migrator.gorm.go
...30 handler *handler.Handler31 dbConnName string32 dbGorm *gorm.DB33 migrationHistorySvc *GormMigrationHistoryService34 runMigratorItems map[string][]IGormMigratorRunner35 rollBackMigratorItems map[string][]IGormMigratorRunner36}37// SetHandler set Handler38func (bgm *BaseGormMigrator) SetHandler(h *handler.Handler) {39 bgm.handler = h40}41// GetHandler get Handler42func (bgm *BaseGormMigrator) GetHandler() *handler.Handler {43 return bgm.handler44}45// SetDBConnName set dbConnName46func (bgm *BaseGormMigrator) SetDBConnName(dbConnName string) error {47 bgm.dbConnName = dbConnName48 if dbConnName != "" {49 dbGorm, err := bgm.handler.GetGormDB(bgm.dbConnName)50 if err != nil {51 return err52 }53 bgm.SetGorm(dbGorm)54 }55 return nil56}57// GetDBConnName get dbConnName58func (bgm *BaseGormMigrator) GetDBConnName() string {59 return bgm.dbConnName60}61// SetGorm set dbGorm62func (bgm *BaseGormMigrator) SetGorm(db *gorm.DB) {63 bgm.dbGorm = db64}65// GetGorm get dbGorm66func (bgm *BaseGormMigrator) GetGorm() *gorm.DB {67 return bgm.dbGorm68}69// GetRunMigratorItems get runMigratorItems70func (bgm *BaseGormMigrator) GetRunMigratorItems(mt GormMigrationType) []IGormMigratorRunner {71 val, ok := bgm.runMigratorItems[string(mt)]72 if !ok {73 var valnil []IGormMigratorRunner74 val = valnil75 }76 return val77}78// GetRollBackMigratorItems get rollBackMigratorItems79func (bgm *BaseGormMigrator) GetRollBackMigratorItems(mt GormMigrationType) []IGormMigratorRunner {80 val, ok := bgm.rollBackMigratorItems[string(mt)]81 if !ok {82 var valnil []IGormMigratorRunner83 val = valnil84 }85 return val86}87// RunMigrates run Migration88func (bgm *BaseGormMigrator) RunMigrates(h *handler.Handler, dbConnName string, items ...IGormMigratorRunner) error {89 return bgm._run(h, dbConnName, GMTMigrate, items...)90}91// RollBackMigrates rollback Migrate92func (bgm *BaseGormMigrator) RollBackMigrates(h *handler.Handler, dbConnName string, items ...IGormMigratorRunner) error {93 return bgm._rollback(h, dbConnName, GMTMigrate, items...)94}95// RunSeeds run Seed96func (bgm *BaseGormMigrator) RunSeeds(h *handler.Handler, dbConnName string, items ...IGormMigratorRunner) error {97 return bgm._run(h, dbConnName, GMTSeed, items...)98}99// RollBackSeeds roolback Seed100func (bgm *BaseGormMigrator) RollBackSeeds(h *handler.Handler, dbConnName string, items ...IGormMigratorRunner) error {101 return bgm._rollback(h, dbConnName, GMTSeed, items...)102}103func (bgm *BaseGormMigrator) _run(h *handler.Handler, dbConnName string, mt GormMigrationType, items ...IGormMigratorRunner) error {104 if h != nil {105 bgm.handler = h106 }107 if dbConnName != "" {108 bgm.dbConnName = dbConnName109 dbGorm, err := bgm.handler.GetGormDB(bgm.dbConnName)110 if err != nil {111 return err112 }113 bgm.dbGorm = dbGorm114 }115 if bgm.dbGorm != nil {116 if items != nil && len(items) > 0 {117 if bgm.runMigratorItems == nil {118 bgm.runMigratorItems = make(map[string][]IGormMigratorRunner)119 }120 var executedList []IGormMigratorRunner121 for _, ir := range items {122 if ir != nil {123 hasBeenExecuted, err := bgm.migrationHistorySvc.HasBeenExecuted(ir.GetID())124 if err != nil {125 return err126 }127 if hasBeenExecuted == false {128 if err := ir.Run(bgm.handler, bgm.dbGorm); err != nil {129 fmt.Printf("Error while running %s.Run: %s\n", ir.GetID(), err.Error())130 if err := bgm.migrationHistorySvc.SaveRunExecution(ir.GetID(), string(mt), err.Error()); err != nil {131 return err132 }133 return err134 }135 executedList = append(executedList, ir)136 if err := bgm.migrationHistorySvc.SaveRunExecution(ir.GetID(), string(mt), ""); err != nil {137 return err138 }139 }140 }141 }142 bgm.runMigratorItems[string(mt)] = executedList143 }144 }145 return nil146}147func (bgm *BaseGormMigrator) _rollback(h *handler.Handler, dbConnName string, mt GormMigrationType, items ...IGormMigratorRunner) error {148 if h != nil {149 bgm.handler = h150 }151 if dbConnName != "" {152 bgm.dbConnName = dbConnName153 dbGorm, err := bgm.handler.GetGormDB(bgm.dbConnName)154 if err != nil {155 return err156 }157 bgm.dbGorm = dbGorm158 }159 if bgm.dbGorm != nil {160 if items != nil && len(items) > 0 {161 if bgm.rollBackMigratorItems == nil {162 bgm.rollBackMigratorItems = make(map[string][]IGormMigratorRunner)163 }164 var executedList []IGormMigratorRunner165 for _, ir := range items {166 if ir != nil {167 hasBeenExecuted, err := bgm.migrationHistorySvc.HasBeenExecuted(ir.GetID())168 if err != nil {169 return err170 }171 if hasBeenExecuted {172 if err := ir.RollBack(bgm.handler, bgm.dbGorm); err != nil {173 fmt.Printf("Error while running %s.RollBack: %s\n", ir.GetID(), err.Error())174 if err := bgm.migrationHistorySvc.SaveRollBackExecution(ir.GetID(), err.Error()); err != nil {175 return err176 }177 return err178 }...
migrate_test.go
Source:migrate_test.go
...35 Down: func(db *gorm.DB) error {36 return db.Migrator().DropTable(&TestTable1{})37 },38 }, "test")39 migrate.Run(migrate.Up)40 So(g.DB().Migrator().HasTable(&migrate.Migration{}), ShouldEqual, true)41 So(g.DB().Migrator().HasTable(&TestTable1{}), ShouldEqual, true)42 So(g.DB("test").Migrator().HasTable(&migrate.Migration{}), ShouldEqual, true)43 So(g.DB("test").Migrator().HasTable(&TestTable1{}), ShouldEqual, true)44 migrate.Add(migrate.MigrationFile{45 FileName: "test_create_test_table_2",46 Up: func(db *gorm.DB) error {47 return db.Migrator().AutoMigrate(&TestTable2{})48 },49 Down: func(db *gorm.DB) error {50 return db.Migrator().DropTable(&TestTable2{})51 },52 }, "default")53 migrate.Run(migrate.Up)54 So(g.DB().Migrator().HasTable(&TestTable1{}), ShouldEqual, true)55 So(g.DB().Migrator().HasTable(&TestTable2{}), ShouldEqual, true)56 So(g.DB("test").Migrator().HasTable(&TestTable1{}), ShouldEqual, true)57 migrate.Run(migrate.Reset)58 So(g.DB().Migrator().HasTable(&TestTable1{}), ShouldEqual, false)59 So(g.DB().Migrator().HasTable(&TestTable2{}), ShouldEqual, false)60 So(g.DB("test").Migrator().HasTable(&TestTable1{}), ShouldEqual, false)61 migrate.Run(migrate.Up)62 So(g.DB().Migrator().HasTable(&TestTable1{}), ShouldEqual, true)63 So(g.DB().Migrator().HasTable(&TestTable2{}), ShouldEqual, true)64 So(g.DB("test").Migrator().HasTable(&TestTable1{}), ShouldEqual, true)65 migrate.Add(migrate.MigrationFile{66 FileName: "test_create_test_table_2",67 Up: func(db *gorm.DB) error {68 return db.Migrator().AutoMigrate(&TestTable2{})69 },70 Down: func(db *gorm.DB) error {71 return db.Migrator().DropTable(&TestTable2{})72 },73 }, "test")74 migrate.Run(migrate.Up)75 So(g.DB().Migrator().HasTable(&TestTable1{}), ShouldEqual, true)76 So(g.DB().Migrator().HasTable(&TestTable2{}), ShouldEqual, true)77 So(g.DB("test").Migrator().HasTable(&TestTable1{}), ShouldEqual, true)78 So(g.DB("test").Migrator().HasTable(&TestTable2{}), ShouldEqual, true)79 migrate.Run(migrate.Rollback)80 So(g.DB().Migrator().HasTable(&TestTable1{}), ShouldEqual, false)81 So(g.DB().Migrator().HasTable(&TestTable2{}), ShouldEqual, false)82 So(g.DB("test").Migrator().HasTable(&TestTable1{}), ShouldEqual, true)83 So(g.DB("test").Migrator().HasTable(&TestTable2{}), ShouldEqual, false)84 migrate.Run(migrate.Reset)85 })86}
migrate.go
Source:migrate.go
...5 "github.com/spf13/cobra"6)7var CmdMigrate = &cobra.Command{8 Use: "migrate",9 Short: "Run database migration.",10 // ææ migrate ä¸çåå½ä»¤é½ä¼æ§è¡ä»¥ä¸ä»£ç 11}12var CmdMigrateUp = &cobra.Command{13 Use: "up",14 Short: "Run unmigrated migrations.",15 Run: runUp,16}17var CmdMigrateRollback = &cobra.Command{18 Use: "down",19 // 设置å«å migrate down == migrate rollback20 Aliases: []string{"rollback"},21 Short: "Reverse the up command.",22 Run: runDown,23}24var CmdMigrateReset = &cobra.Command{25 Use: "reset",26 Short: "Rollback all database migrations.",27 Run: runReset,28}29var CmdMigrateRefresh = &cobra.Command{30 Use: "refresh",31 Short: "Reset and re-run all migrations.",32 Run: runRefresh,33}34var CmdMigrateFresh = &cobra.Command{35 Use: "fresh",36 Short: "Drop all tables and re-run all migrations.",37 Run: runFresh,38}39func init() {40 CmdMigrate.AddCommand(41 CmdMigrateUp,42 CmdMigrateRollback,43 CmdMigrateReset,44 CmdMigrateRefresh,45 CmdMigrateFresh,46 )47}48func migrator() *migrate.Migrator {49 // 注å database/migrations ä¸çææè¿ç§»æ件50 migrations.Initialize()51 // åå§å migrator...
Run
Using AI Code Generation
1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer db.Close()7 m, err := migrate.New(8 if err != nil {9 log.Fatal(err)10 }11 if err := m.Up(); err != nil && err != migrate.ErrNoChange {12 log.Fatal(err)13 }14 fmt.Println("Migration done")15}16import (17func main() {18 if err != nil {19 log.Fatal(err)20 }21 defer db.Close()22 m, err := migrate.New(23 if err != nil {24 log.Fatal(err)25 }26 if err := m.Run(1, 1); err != nil && err != migrate.ErrNoChange {27 log.Fatal(err)28 }29 fmt.Println("Migration done")30}31import (
Run
Using AI Code Generation
1import (2func main() {3 goose.SetBaseFS(afero.NewOsFs())4 goose.SetDialect("postgres")5 goose.SetTableName("goose_db_version")6 goose.Run("status", nil, "db/migrations")7}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!