How to use NewMigrator method of migrator Package

Best Testkube code snippet using migrator.NewMigrator

migrator_integration_test.go

Source:migrator_integration_test.go Github

copy

Full Screen

...18func TestMigrator_Version(t *testing.T) {19 db, err := testutil.NewDBFromEnv()20 require.NoError(t, err)21 defer cleanupDB(t, db)22 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))23 _, err = m.Up()24 require.NoError(t, err)25 latest, err := m.LatestVersion()26 require.NoError(t, err)27 current, err := m.Version()28 require.NoError(t, err)29 require.Equal(t, latest, current)30}31func TestMigrator_Version_NoMigrations(t *testing.T) {32 db, err := testutil.NewDBFromEnv()33 require.NoError(t, err)34 defer cleanupDB(t, db)35 // Create migrator with an empty migration source.36 m := migrations.NewMigrator(db.DB, migrations.Source([]*migrations.Migration{}))37 v, err := m.Version()38 require.NoError(t, err)39 require.Empty(t, v)40}41func TestMigrator_LatestVersion(t *testing.T) {42 db, err := testutil.NewDBFromEnv()43 require.NoError(t, err)44 defer cleanupDB(t, db)45 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))46 v, err := m.LatestVersion()47 require.NoError(t, err)48 require.Equal(t, v, testmigrations.All()[len(testmigrations.All())-1].Id)49}50func TestMigrator_LatestVersion_NoMigrations(t *testing.T) {51 db, err := testutil.NewDBFromEnv()52 require.NoError(t, err)53 defer cleanupDB(t, db)54 // Create migrator with an empty migration source.55 m := migrations.NewMigrator(db.DB, migrations.Source([]*migrations.Migration{}))56 v, err := m.LatestVersion()57 require.NoError(t, err)58 require.Empty(t, v)59}60func TestMigrator_Up(t *testing.T) {61 db, err := testutil.NewDBFromEnv()62 require.NoError(t, err)63 defer cleanupDB(t, db)64 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))65 all := testmigrations.All()66 count, err := m.Up()67 require.NoError(t, err)68 require.Equal(t, len(all), count)69 currentVersion, err := m.Version()70 require.NoError(t, err)71 v, err := m.LatestVersion()72 require.NoError(t, err)73 require.Equal(t, v, currentVersion)74}75func TestMigrator_Up_ApplyPostDeploymentMigrations(t *testing.T) {76 db, err := testutil.NewDBFromEnv()77 require.NoError(t, err)78 defer cleanupDB(t, db)79 m := migrations.NewMigrator(80 db.DB,81 migrations.Source(testmigrations.All()),82 migrations.SkipPostDeployment,83 )84 migs := testmigrations.NonPostDeployment()85 count, err := m.Up()86 require.NoError(t, err)87 require.Equal(t, len(migs), count)88 initialCurrentVersion, err := m.Version()89 require.NoError(t, err)90 initialLatestVersion, err := m.LatestVersion()91 require.NoError(t, err)92 require.Equal(t, initialLatestVersion, initialCurrentVersion)93 // Run post deployment migrations after fully applying all others.94 m = migrations.NewMigrator(95 db.DB,96 migrations.Source(testmigrations.All()),97 )98 all := testmigrations.All()99 count, err = m.Up()100 require.NoError(t, err)101 require.Equal(t, len(all)-len(migs), count)102 currentVersion, err := m.Version()103 require.NoError(t, err)104 latestVersion, err := m.LatestVersion()105 require.NoError(t, err)106 require.Equal(t, latestVersion, currentVersion)107 require.NotEqual(t, initialLatestVersion, latestVersion)108 require.NotEqual(t, initialCurrentVersion, currentVersion)109}110func TestMigrator_Up_SkipPostDeployment(t *testing.T) {111 db, err := testutil.NewDBFromEnv()112 require.NoError(t, err)113 defer cleanupDB(t, db)114 m := migrations.NewMigrator(115 db.DB,116 migrations.Source(testmigrations.All()),117 migrations.SkipPostDeployment,118 )119 migs := testmigrations.NonPostDeployment()120 n := len(migs)121 count, err := m.Up()122 require.NoError(t, err)123 require.Equal(t, n, count)124 currentVersion, err := m.Version()125 require.NoError(t, err)126 v, err := m.LatestVersion()127 require.NoError(t, err)128 require.Equal(t, v, currentVersion)129}130func TestMigrator_UpN(t *testing.T) {131 db, err := testutil.NewDBFromEnv()132 require.NoError(t, err)133 defer cleanupDB(t, db)134 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))135 // apply all except the last two136 all := testmigrations.All()137 n := len(all) - 1 - 2138 nth := all[n-1]139 count, err := m.UpN(n)140 require.NoError(t, err)141 require.Equal(t, n, count)142 v, err := m.Version()143 require.NoError(t, err)144 require.Equal(t, nth.Id, v)145 // resume and apply the remaining146 count, err = m.UpN(0)147 require.NoError(t, err)148 require.Equal(t, len(all)-n, count)149 v, err = m.Version()150 require.NoError(t, err)151 require.Equal(t, all[len(all)-1].Id, v)152 // make sure it's idempotent153 count, err = m.UpN(100)154 require.NoError(t, err)155 require.Zero(t, count)156 v2, err := m.Version()157 require.NoError(t, err)158 require.Equal(t, v, v2)159}160func TestMigrator_UpN_SkipPostDeployment(t *testing.T) {161 db, err := testutil.NewDBFromEnv()162 require.NoError(t, err)163 defer cleanupDB(t, db)164 m := migrations.NewMigrator(165 db.DB,166 migrations.Source(testmigrations.All()),167 migrations.SkipPostDeployment,168 )169 // apply all non postdeployment migrations except the last two170 migs := testmigrations.NonPostDeployment()171 n := len(migs) - 1 - 2172 nth := migs[n-1]173 count, err := m.UpN(n)174 require.NoError(t, err)175 require.Equal(t, n, count)176 v, err := m.Version()177 require.NoError(t, err)178 require.Equal(t, nth.Id, v)179 // resume and apply the remaining180 count, err = m.UpN(0)181 require.NoError(t, err)182 require.Equal(t, len(migs)-n, count)183 v, err = m.Version()184 require.NoError(t, err)185 require.Equal(t, migs[len(migs)-1].Id, v)186 // make sure it's idempotent187 count, err = m.UpN(100)188 require.NoError(t, err)189 require.Zero(t, count)190 v2, err := m.Version()191 require.NoError(t, err)192 require.Equal(t, v, v2)193}194func TestMigrator_UpNPlan(t *testing.T) {195 db, err := testutil.NewDBFromEnv()196 require.NoError(t, err)197 defer cleanupDB(t, db)198 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))199 all := testmigrations.All()200 var allPlan []string201 for _, migration := range all {202 allPlan = append(allPlan, migration.Id)203 }204 // plan all except the last two205 n := len(allPlan) - 1 - 2206 allExceptLastTwoPlan := allPlan[:n]207 plan, err := m.UpNPlan(n)208 require.NoError(t, err)209 require.Equal(t, allExceptLastTwoPlan, plan)210 // apply two migrations and re-plan all (the first two shouldn't be part of the plan anymore)211 _, err = m.UpN(2)212 require.NoError(t, err)213 plan, err = m.UpNPlan(0)214 require.NoError(t, err)215 allExceptFirstTwoPlan := allPlan[2:]216 require.Equal(t, allExceptFirstTwoPlan, plan)217 // make sure it's idempotent218 plan, err = m.UpNPlan(100)219 require.NoError(t, err)220 require.Equal(t, allExceptFirstTwoPlan, plan)221}222func TestMigrator_UpNPlan_SkipPostDeployment(t *testing.T) {223 db, err := testutil.NewDBFromEnv()224 require.NoError(t, err)225 defer cleanupDB(t, db)226 m := migrations.NewMigrator(227 db.DB,228 migrations.Source(testmigrations.All()),229 migrations.SkipPostDeployment,230 )231 migs := testmigrations.NonPostDeployment()232 var allPlan []string233 for _, migration := range migs {234 allPlan = append(allPlan, migration.Id)235 }236 // plan all except the last two237 n := len(allPlan) - 1 - 2238 allExceptLastTwoPlan := allPlan[:n]239 plan, err := m.UpNPlan(n)240 require.NoError(t, err)241 require.Equal(t, allExceptLastTwoPlan, plan)242 // apply two migrations and re-plan all (the first two shouldn't be part of the plan anymore)243 _, err = m.UpN(2)244 require.NoError(t, err)245 plan, err = m.UpNPlan(0)246 require.NoError(t, err)247 allExceptFirstTwoPlan := allPlan[2:]248 require.Equal(t, allExceptFirstTwoPlan, plan)249 // make sure it's idempotent250 plan, err = m.UpNPlan(100)251 require.NoError(t, err)252 require.Equal(t, allExceptFirstTwoPlan, plan)253}254func TestMigrator_Down(t *testing.T) {255 db, err := testutil.NewDBFromEnv()256 require.NoError(t, err)257 defer cleanupDB(t, db)258 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))259 _, err = m.Up()260 require.NoError(t, err)261 all := testmigrations.All()262 count, err := m.Down()263 require.NoError(t, err)264 require.Equal(t, len(all), count)265 currentVersion, err := m.Version()266 require.NoError(t, err)267 require.Empty(t, currentVersion)268}269func TestMigrator_Down_SkipPostDeployment(t *testing.T) {270 db, err := testutil.NewDBFromEnv()271 require.NoError(t, err)272 defer cleanupDB(t, db)273 m := migrations.NewMigrator(274 db.DB,275 migrations.Source(testmigrations.All()),276 migrations.SkipPostDeployment,277 )278 _, err = m.Up()279 require.NoError(t, err)280 migs := testmigrations.NonPostDeployment()281 count, err := m.Down()282 require.NoError(t, err)283 require.Equal(t, len(migs), count)284 currentVersion, err := m.Version()285 require.NoError(t, err)286 require.Empty(t, currentVersion)287}288func TestMigrator_Down_SkipPostDeployment_ExistingPostDeployments(t *testing.T) {289 db, err := testutil.NewDBFromEnv()290 require.NoError(t, err)291 defer cleanupDB(t, db)292 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))293 _, err = m.Up()294 require.NoError(t, err)295 all := testmigrations.All()296 // Configure migrator to skip postdeployment migrations, down migrations297 // should ignore this and operate on all applied migrations.298 migrations.SkipPostDeployment(m)299 count, err := m.Down()300 require.NoError(t, err)301 require.Equal(t, len(all), count)302 currentVersion, err := m.Version()303 require.NoError(t, err)304 require.Empty(t, currentVersion)305}306func TestMigrator_DownN(t *testing.T) {307 db, err := testutil.NewDBFromEnv()308 require.NoError(t, err)309 defer cleanupDB(t, db)310 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))311 _, err = m.Up()312 require.NoError(t, err)313 // rollback all except the first two314 all := testmigrations.All()315 n := len(all) - 2316 second := all[1]317 count, err := m.DownN(n)318 require.NoError(t, err)319 require.Equal(t, n, count)320 v, err := m.Version()321 require.NoError(t, err)322 require.Equal(t, second.Id, v)323 // resume and rollback the remaining two324 count, err = m.DownN(0)325 require.NoError(t, err)326 require.Equal(t, 2, count)327 v, err = m.Version()328 require.NoError(t, err)329 require.Empty(t, v)330 // make sure it's idempotent331 count, err = m.DownN(100)332 require.NoError(t, err)333 require.Zero(t, count)334 v, err = m.Version()335 require.NoError(t, err)336 require.Empty(t, v)337}338func TestMigrator_DownN_SkipPostDeployment(t *testing.T) {339 db, err := testutil.NewDBFromEnv()340 require.NoError(t, err)341 defer cleanupDB(t, db)342 m := migrations.NewMigrator(343 db.DB,344 migrations.Source(testmigrations.All()),345 migrations.SkipPostDeployment,346 )347 _, err = m.Up()348 require.NoError(t, err)349 // rollback all except the first two350 migs := testmigrations.NonPostDeployment()351 n := len(migs) - 2352 second := migs[1]353 count, err := m.DownN(n)354 require.NoError(t, err)355 require.Equal(t, n, count)356 v, err := m.Version()357 require.NoError(t, err)358 require.Equal(t, second.Id, v)359 // resume and rollback the remaining two360 count, err = m.DownN(0)361 require.NoError(t, err)362 require.Equal(t, 2, count)363 v, err = m.Version()364 require.NoError(t, err)365 require.Empty(t, v)366 // make sure it's idempotent367 count, err = m.DownN(100)368 require.NoError(t, err)369 require.Zero(t, count)370 v, err = m.Version()371 require.NoError(t, err)372 require.Empty(t, v)373}374func TestMigrator_DownNPlan(t *testing.T) {375 db, err := testutil.NewDBFromEnv()376 require.NoError(t, err)377 defer cleanupDB(t, db)378 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))379 _, err = m.Up()380 require.NoError(t, err)381 all := testmigrations.All()382 var allPlan []string383 for _, migration := range all {384 allPlan = append(allPlan, migration.Id)385 }386 sort.Sort(sort.Reverse(sort.StringSlice(allPlan))) // down migrations are applied in reverse order387 // plan all except the last two388 n := len(allPlan) - 1 - 2389 allExceptLastTwoPlan := allPlan[:n]390 plan, err := m.DownNPlan(n)391 require.NoError(t, err)392 require.Equal(t, allExceptLastTwoPlan, plan)393 // apply two migrations and re-plan all (the first two shouldn't be part of the plan anymore)394 _, err = m.DownN(2)395 require.NoError(t, err)396 plan, err = m.DownNPlan(0)397 require.NoError(t, err)398 allExceptFirstTwoPlan := allPlan[2:]399 require.Equal(t, allExceptFirstTwoPlan, plan)400 // make sure it's idempotent401 plan, err = m.DownNPlan(100)402 require.NoError(t, err)403 require.Equal(t, allExceptFirstTwoPlan, plan)404}405func TestMigrator_DownNPlan_SkipPostDeploymnet(t *testing.T) {406 db, err := testutil.NewDBFromEnv()407 require.NoError(t, err)408 defer cleanupDB(t, db)409 m := migrations.NewMigrator(410 db.DB,411 migrations.Source(testmigrations.All()),412 migrations.SkipPostDeployment,413 )414 _, err = m.Up()415 require.NoError(t, err)416 migs := testmigrations.NonPostDeployment()417 var migsPlan []string418 for _, migration := range migs {419 migsPlan = append(migsPlan, migration.Id)420 }421 sort.Sort(sort.Reverse(sort.StringSlice(migsPlan))) // down migrations are applied in reverse order422 // plan all except the last two423 n := len(migsPlan) - 1 - 2424 allExceptLastTwoPlan := migsPlan[:n]425 plan, err := m.DownNPlan(n)426 require.NoError(t, err)427 require.Equal(t, allExceptLastTwoPlan, plan)428 // apply two migrations and re-plan all (the first two shouldn't be part of the plan anymore)429 _, err = m.DownN(2)430 require.NoError(t, err)431 plan, err = m.DownNPlan(0)432 require.NoError(t, err)433 allExceptFirstTwoPlan := migsPlan[2:]434 require.Equal(t, allExceptFirstTwoPlan, plan)435 // make sure it's idempotent436 plan, err = m.DownNPlan(100)437 require.NoError(t, err)438 require.Equal(t, allExceptFirstTwoPlan, plan)439}440func TestMigrator_Status_Empty(t *testing.T) {441 db, err := testutil.NewDBFromEnv()442 require.NoError(t, err)443 defer cleanupDB(t, db)444 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))445 all := testmigrations.All()446 statuses, err := m.Status()447 require.NoError(t, err)448 require.Len(t, statuses, len(all))449 var expectedIDs, actualIDs []string450 for _, m := range all {451 expectedIDs = append(expectedIDs, m.Id)452 }453 for id := range statuses {454 actualIDs = append(actualIDs, id)455 }456 require.ElementsMatch(t, expectedIDs, actualIDs)457 for _, s := range statuses {458 require.False(t, s.Unknown)459 require.Nil(t, s.AppliedAt)460 }461}462func TestMigrator_Status_Full(t *testing.T) {463 db, err := testutil.NewDBFromEnv()464 require.NoError(t, err)465 defer cleanupDB(t, db)466 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))467 _, err = m.Up()468 require.NoError(t, err)469 all := testmigrations.All()470 statuses, err := m.Status()471 require.NoError(t, err)472 require.Len(t, statuses, len(all))473 var expectedIDs, actualIDs []string474 for _, m := range all {475 expectedIDs = append(expectedIDs, m.Id)476 }477 for id := range statuses {478 actualIDs = append(actualIDs, id)479 }480 require.ElementsMatch(t, expectedIDs, actualIDs)481 for _, s := range statuses {482 require.False(t, s.Unknown)483 require.NotNil(t, s.AppliedAt)484 }485}486func TestMigrator_Status_PostDeployment(t *testing.T) {487 db, err := testutil.NewDBFromEnv()488 require.NoError(t, err)489 defer cleanupDB(t, db)490 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))491 _, err = m.Up()492 require.NoError(t, err)493 all := testmigrations.All()494 statuses, err := m.Status()495 require.NoError(t, err)496 require.Len(t, statuses, len(all))497 // See migrations/testdata/fixtures/498 postDeploymentID := "20201027124302_create_post_migration_test_two_table"499 standardID := "20200319131542_create_manifests_test_table"500 postDeploymentStatus := statuses[postDeploymentID]501 require.NotNil(t, postDeploymentStatus)502 require.True(t, postDeploymentStatus.PostDeployment)503 standardStatus := statuses[standardID]504 require.NotNil(t, standardStatus)505 require.False(t, standardStatus.PostDeployment)506}507func TestMigrator_Status_Unknown(t *testing.T) {508 db, err := testutil.NewDBFromEnv()509 require.NoError(t, err)510 defer cleanupDB(t, db)511 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))512 _, err = m.Up()513 require.NoError(t, err)514 all := testmigrations.All()515 // temporarily insert fake migration record516 fakeID := "20060102150405_foo"517 fakeAppliedAt := time.Now()518 _, err = db.DB.Exec("INSERT INTO "+migrationTableName+" (id, applied_at) VALUES ($1, $2)", fakeID, fakeAppliedAt)519 require.NoError(t, err)520 defer db.DB.Exec("DELETE FROM "+migrationTableName+" WHERE id = $1", fakeID)521 statuses, err := m.Status()522 require.NoError(t, err)523 require.Len(t, statuses, len(all)+1)524 fakeStatus := statuses[fakeID]525 require.NotNil(t, fakeStatus)526 require.True(t, fakeStatus.Unknown)527 require.Equal(t, fakeAppliedAt.Round(time.Millisecond).UTC(), fakeStatus.AppliedAt.Round(time.Millisecond).UTC())528}529func TestMigrator_HasPending_No(t *testing.T) {530 db, err := testutil.NewDBFromEnv()531 require.NoError(t, err)532 defer cleanupDB(t, db)533 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))534 _, err = m.Up()535 require.NoError(t, err)536 pending, err := m.HasPending()537 require.NoError(t, err)538 require.False(t, pending)539}540func TestMigrator_HasPending_No_SkipPostDeployment(t *testing.T) {541 db, err := testutil.NewDBFromEnv()542 require.NoError(t, err)543 defer cleanupDB(t, db)544 m := migrations.NewMigrator(545 db.DB,546 migrations.Source(testmigrations.All()),547 migrations.SkipPostDeployment,548 )549 _, err = m.Up()550 require.NoError(t, err)551 pending, err := m.HasPending()552 require.NoError(t, err)553 require.False(t, pending)554}555func TestMigrator_HasPending_Yes(t *testing.T) {556 db, err := testutil.NewDBFromEnv()557 require.NoError(t, err)558 defer cleanupDB(t, db)559 m := migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))560 _, err = m.Up()561 require.NoError(t, err)562 _, err = m.DownN(1)563 require.NoError(t, err)564 pending, err := m.HasPending()565 require.NoError(t, err)566 require.True(t, pending)567}568func TestMigrator_HasPending_Yes_PendingPostDeployment(t *testing.T) {569 db, err := testutil.NewDBFromEnv()570 require.NoError(t, err)571 defer cleanupDB(t, db)572 m := migrations.NewMigrator(573 db.DB,574 migrations.Source(testmigrations.All()),575 migrations.SkipPostDeployment,576 )577 _, err = m.Up()578 require.NoError(t, err)579 m = migrations.NewMigrator(db.DB, migrations.Source(testmigrations.All()))580 pending, err := m.HasPending()581 require.NoError(t, err)582 require.True(t, pending)583}584func cleanupDB(t *testing.T, db *datastore.DB) {585 _, err := db.DB.Exec("DELETE FROM " + migrationTableName)586 require.NoError(t, err)587 require.NoError(t, db.Close())588}...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

...118 dbdebug := false119 if c.String("dbdebug") == "true" {120 dbdebug = true121 }122 migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)123 return migrator.Init(ctx)124 },125 },126 {127 Name: "migrate",128 Usage: "migrate database",129 Action: func(c *cli.Context) error {130 ctx, app, err := app.StartCLI(c)131 if err != nil {132 return err133 }134 defer app.Stop()135 dbdebug := false136 if c.String("dbdebug") == "true" {137 dbdebug = true138 }139 migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)140 _, err = migrator.Migrate(ctx)141 return err142 },143 },144 {145 Name: "rollback",146 Usage: "rollback the last migration group",147 Action: func(c *cli.Context) error {148 ctx, app, err := app.StartCLI(c)149 if err != nil {150 return err151 }152 defer app.Stop()153 dbdebug := false154 if c.String("dbdebug") == "true" {155 dbdebug = true156 }157 migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)158 _, err = migrator.Rollback(ctx)159 return err160 },161 },162 {163 Name: "lock",164 Usage: "lock migrations",165 Action: func(c *cli.Context) error {166 ctx, app, err := app.StartCLI(c)167 if err != nil {168 return err169 }170 defer app.Stop()171 dbdebug := false172 if c.String("dbdebug") == "true" {173 dbdebug = true174 }175 migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)176 return migrator.Lock(ctx)177 },178 },179 {180 Name: "unlock",181 Usage: "unlock migrations",182 Action: func(c *cli.Context) error {183 ctx, app, err := app.StartCLI(c)184 if err != nil {185 return err186 }187 defer app.Stop()188 dbdebug := false189 if c.String("dbdebug") == "true" {190 dbdebug = true191 }192 migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)193 return migrator.Unlock(ctx)194 },195 },196 {197 Name: "create_go",198 Usage: "create Go migration",199 Action: func(c *cli.Context) error {200 ctx, app, err := app.StartCLI(c)201 if err != nil {202 return err203 }204 defer app.Stop()205 dbdebug := false206 if c.String("dbdebug") == "true" {207 dbdebug = true208 }209 migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)210 _, err = migrator.CreateGoMigration(ctx, c.Args().Get(0))211 return err212 },213 },214 {215 Name: "create_sql",216 Usage: "create SQL migration",217 Action: func(c *cli.Context) error {218 ctx, app, err := app.StartCLI(c)219 if err != nil {220 return err221 }222 defer app.Stop()223 dbdebug := false224 if c.String("dbdebug") == "true" {225 dbdebug = true226 }227 migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)228 _, err = migrator.CreateSQLMigrations(ctx, c.Args().Get(0))229 return err230 },231 },232 {233 Name: "load_default_fixtures",234 Usage: "load default data",235 Action: func(c *cli.Context) error {236 ctx, app, err := app.StartCLI(c)237 if err != nil {238 return err239 }240 defer app.Stop()241 dbdebug := false...

Full Screen

Full Screen

migrations.go

Source:migrations.go Github

copy

Full Screen

2// Migrator is a struct for handling in-place store migrations.3type Migrator struct {4 keeper Keeper5}6// NewMigrator returns a new Migrator.7func NewMigrator(keeper Keeper) Migrator {8 return Migrator{9 keeper: keeper,10 }11}...

Full Screen

Full Screen

NewMigrator

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db, err := sql.Open("sqlite3", "test.db")4 if err != nil {5 fmt.Println(err)6 }7 defer db.Close()8 m, err := goose.NewMigrator(db, goose.SqliteDialect{})9 if err != nil {10 fmt.Println(err)11 }12 err = m.Up()13 if err != nil {14 fmt.Println(err)15 }16}17The goose.NewMigrator function returns a new instance of the Migrator type, which is used to run migrations. The goose.SqliteDialect{} is used to specify the dialect to use for the database. The goose.SqliteDialect{} is a struct type that implements the goose.Dialect interface. The goose.Dialect interface contains the following methods:18CreateVersionTable(*sql.DB) error19DropVersionTable(*sql.DB) error20InsertVersion(*sql.DB, string) error21SetVersion(*sql.DB, int64) error22Version(*sql.DB) (int64, error)23The goose.SqliteDialect{} type implements the goose.Dialect interface. The goose.SqliteDialect{} type has the following methods:24CreateVersionTable(*sql.DB) error25DropVersionTable(*sql.DB) error26InsertVersion(*sql.DB, string) error27SetVersion(*sql.DB, int64) error28Version(*sql.DB) (int64, error)

Full Screen

Full Screen

NewMigrator

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 err = goose.Run("up", db, "migrations")7 if err != nil {8 log.Fatal(err)9 }10 fmt.Println("Migration done successfully")11}12To run a specific migration, we can use the goose.Run() method as follows:13goose.Run("up", db, "migrations", "1_create_users_table.go")14If we want to run all the migrations up to a specific migration, we can use the goose.Run() method as follows:15goose.Run("up-to", db, "migrations", "1_create_users_table.go")16To run a specific migration down, we can use the goose.Run() method as follows:17goose.Run("down", db, "migrations", "1_create_users_table.go")18To run all the migrations down, we can use the goose.Run() method as follows:19goose.Run("down", db, "migrations")20To check the current version of the database, we can use the goose.Run() method as follows:21goose.Run("status", db, "migrations")22To create a new migration, we can use the goose.Create() method as follows:23goose.Create("create_users_table", "migrations")

Full Screen

Full Screen

NewMigrator

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 m := migration.NewMigrator()4 m.CreateMigration("create_users_table", func() {5 fmt.Println("Create users table")6 })7 m.Run()8}9import (10func main() {11 m := migration.NewMigrator()12 m.CreateMigration("create_users_table", func() {13 fmt.Println("Create users table")14 })15 m.Run()16}17import (18func main() {19 m := migration.NewMigrator()20 m.CreateMigration("create_users_table", func() {21 fmt.Println("Create users table")22 })23 m.Run()24}25import (26func main() {27 m := migration.NewMigrator()28 m.CreateMigration("create_users_table", func() {29 fmt.Println("Create users table")30 })31 m.Run()32}33import (34func main() {35 m := migration.NewMigrator()36 m.CreateMigration("create_users_table", func() {37 fmt.Println("Create users table")38 })39 m.Run()40}41import (42func main() {43 m := migration.NewMigrator()44 m.CreateMigration("create_users_table", func() {45 fmt.Println("Create users table")46 })47 m.Run()48}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful