How to use Version method of migrations Package

Best Testkube code snippet using migrations.Version

migrator_test.go

Source:migrator_test.go Github

copy

Full Screen

...54 args args55 wantErr bool56 wantUp bool57 wantDown bool58 wantVersion int59 }{60 {61 name: "run test migration",62 fields: fields{migrationsTable: DefaultMigrationsTable},63 args: args{ctx: context.TODO(), migrations: []*migrations.Migration{{64 Id: 0,65 Up: func(ctx context.Context, tx *sql.Tx) error {66 up = true67 return nil68 },69 Down: func(ctx context.Context, tx *sql.Tx) error {70 down = true71 return nil72 },73 }}},74 wantUp: false,75 wantDown: true,76 wantVersion: -1,77 },78 {79 name: "run migration out of order",80 fields: fields{migrationsTable: DefaultMigrationsTable},81 args: args{ctx: context.TODO(), migrations: []*migrations.Migration{{82 Id: 1,83 Up: func(ctx context.Context, tx *sql.Tx) error {84 up = true85 return nil86 },87 Down: func(ctx context.Context, tx *sql.Tx) error {88 down = true89 return nil90 },91 }}},92 wantUp: false,93 wantDown: false,94 wantErr: true,95 wantVersion: 0,96 },97 {98 name: "run error migration",99 fields: fields{migrationsTable: DefaultMigrationsTable},100 args: args{ctx: context.TODO(), migrations: []*migrations.Migration{{101 Id: 0,102 Up: func(ctx context.Context, tx *sql.Tx) error {103 return fmt.Errorf("error")104 },105 Down: func(ctx context.Context, tx *sql.Tx) error {106 return fmt.Errorf("error")107 },108 }}},109 wantErr: true,110 wantUp: false,111 wantDown: false,112 wantVersion: 0,113 },114 }115 for _, tt := range tests {116 t.Run(tt.name, func(t *testing.T) {117 up = false118 down = false119 db, cleanup := CreateTestDb(t)120 defer cleanup()121 m := &SQLLiteMigrator{122 db: db,123 migrationsTable: tt.fields.migrationsTable,124 }125 {126 tx, err := db.Begin()127 require.NoError(t, err)128 require.NoError(t, m.setVersion(context.TODO(), tx, 0))129 require.NoError(t, err)130 require.NoError(t, tx.Commit())131 tx.Rollback()132 }133 if err := m.Down(tt.args.ctx, tt.args.migrations); (err != nil) != tt.wantErr {134 t.Errorf("Down() error = %v, wantErr %v", err, tt.wantErr)135 }136 require.Equal(t, tt.wantUp, up)137 require.Equal(t, tt.wantDown, down)138 // verify the version is correct139 var version int140 {141 tx, err := db.Begin()142 require.NoError(t, err)143 version, err = m.version(context.TODO(), tx)144 require.NoError(t, err)145 require.NoError(t, tx.Commit())146 tx.Rollback()147 }148 require.Equal(t, tt.wantVersion, version)149 })150 }151}152func TestSQLLiteMigrator_Up(t *testing.T) {153 var up int154 var down bool155 type fields struct {156 migrationsTable string157 }158 type args struct {159 ctx context.Context160 migrations migrations.Migrations161 }162 tests := []struct {163 name string164 fields fields165 args args166 wantErr bool167 wantUp int168 wantDown bool169 wantVersion int170 }{171 {172 name: "run test migration",173 fields: fields{migrationsTable: DefaultMigrationsTable},174 args: args{ctx: context.TODO(), migrations: migrations.Migrations{{175 Id: 0,176 Up: func(ctx context.Context, tx *sql.Tx) error {177 up++178 return nil179 },180 Down: func(ctx context.Context, tx *sql.Tx) error {181 down = true182 return nil183 },184 }}},185 wantUp: 1,186 wantDown: false,187 wantVersion: 0,188 },189 {190 name: "run multiple test migration",191 fields: fields{migrationsTable: DefaultMigrationsTable},192 args: args{ctx: context.TODO(), migrations: migrations.Migrations{193 {194 Id: 0,195 Up: func(ctx context.Context, tx *sql.Tx) error {196 up++197 return nil198 },199 Down: func(ctx context.Context, tx *sql.Tx) error {200 down = true201 return nil202 },203 },204 {205 Id: 1,206 Up: func(ctx context.Context, tx *sql.Tx) error {207 up++208 return nil209 },210 Down: func(ctx context.Context, tx *sql.Tx) error {211 down = true212 return nil213 },214 },215 }},216 wantUp: 2,217 wantDown: false,218 wantVersion: 1,219 },220 {221 name: "run migrations out of order",222 fields: fields{migrationsTable: DefaultMigrationsTable},223 args: args{ctx: context.TODO(), migrations: migrations.Migrations{224 {225 Id: 1,226 Up: func(ctx context.Context, tx *sql.Tx) error {227 up++228 return nil229 },230 Down: func(ctx context.Context, tx *sql.Tx) error {231 down = true232 return nil233 },234 },235 {236 Id: 0,237 Up: func(ctx context.Context, tx *sql.Tx) error {238 up++239 return nil240 },241 Down: func(ctx context.Context, tx *sql.Tx) error {242 down = true243 return nil244 },245 },246 }},247 wantUp: 0,248 wantDown: false,249 wantErr: true,250 wantVersion: -1,251 },252 {253 name: "run error migration",254 fields: fields{migrationsTable: DefaultMigrationsTable},255 args: args{ctx: context.TODO(), migrations: migrations.Migrations{{256 Id: 0,257 Up: func(ctx context.Context, tx *sql.Tx) error {258 return fmt.Errorf("error")259 },260 Down: func(ctx context.Context, tx *sql.Tx) error {261 return fmt.Errorf("error")262 },263 }}},264 wantErr: true,265 wantUp: 0,266 wantDown: false,267 wantVersion: -1,268 },269 }270 for _, tt := range tests {271 t.Run(tt.name, func(t *testing.T) {272 up = 0273 down = false274 db, cleanup := CreateTestDb(t)275 defer cleanup()276 m := &SQLLiteMigrator{277 db: db,278 migrationsTable: tt.fields.migrationsTable,279 }280 {281 tx, err := db.Begin()282 require.NoError(t, err)283 require.NoError(t, m.setVersion(context.TODO(), tx, -1))284 require.NoError(t, err)285 require.NoError(t, tx.Commit())286 tx.Rollback()287 }288 if err := m.Up(tt.args.ctx, tt.args.migrations); (err != nil) != tt.wantErr {289 t.Errorf("Up() error = %v, wantErr %v", err, tt.wantErr)290 }291 require.Equal(t, tt.wantUp, up)292 require.Equal(t, tt.wantDown, down)293 // verify the version is correct294 var version int295 {296 tx, err := db.Begin()297 require.NoError(t, err)298 version, err = m.version(context.TODO(), tx)299 require.NoError(t, err)300 require.NoError(t, tx.Commit())301 tx.Rollback()302 }303 require.Equal(t, tt.wantVersion, version)304 if tt.wantErr {305 return306 }307 // walk backwards back to zero308 sort.Sort(sort.Reverse(tt.args.migrations))309 err := m.Down(tt.args.ctx, tt.args.migrations)310 require.NoError(t, err)311 {312 tx, err := db.Begin()313 require.NoError(t, err)314 version, err = m.version(context.TODO(), tx)315 require.NoError(t, err)316 require.NoError(t, tx.Commit())317 tx.Rollback()318 }319 require.Equal(t, NilVersion, version)320 })321 }322}323func TestSQLLiteMigrator_Migrate(t *testing.T) {324 var up int325 var down int326 migs := migrations.MigrationSet{327 0: {328 Id: 0,329 Up: func(ctx context.Context, tx *sql.Tx) error {330 up++331 return nil332 },333 Down: func(ctx context.Context, tx *sql.Tx) error {334 down++335 return nil336 },337 },338 1: {339 Id: 1,340 Up: func(ctx context.Context, tx *sql.Tx) error {341 up++342 return nil343 },344 Down: func(ctx context.Context, tx *sql.Tx) error {345 down++346 return nil347 },348 },349 }350 tests := []struct {351 name string352 startVersion int353 wantErr bool354 wantUp int355 wantDown int356 wantVersion int357 }{358 {359 name: "up from nothing",360 startVersion: -1,361 wantUp: 2,362 wantDown: 0,363 wantVersion: 1,364 },365 {366 name: "up from initial db",367 startVersion: 0,368 wantUp: 1,369 wantDown: 0,370 wantVersion: 1,371 },372 {373 name: "at latest",374 startVersion: 1,375 wantUp: 0,376 wantDown: 0,377 wantVersion: 1,378 },379 }380 for _, tt := range tests {381 t.Run(tt.name, func(t *testing.T) {382 up = 0383 down = 0384 db, cleanup := CreateTestDb(t)385 defer cleanup()386 m := &SQLLiteMigrator{387 db: db,388 migrationsTable: DefaultMigrationsTable,389 migrations: migs,390 }391 {392 tx, err := db.Begin()393 require.NoError(t, err)394 require.NoError(t, m.setVersion(context.TODO(), tx, tt.startVersion))395 require.NoError(t, err)396 require.NoError(t, tx.Commit())397 tx.Rollback()398 }399 if err := m.Migrate(context.TODO()); (err != nil) != tt.wantErr {400 t.Errorf("Migrate() error = %v, wantErr %v", err, tt.wantErr)401 }402 require.Equal(t, tt.wantUp, up)403 require.Equal(t, tt.wantDown, down)404 // verify the version is correct405 var version int406 {407 tx, err := db.Begin()408 require.NoError(t, err)409 version, err = m.version(context.TODO(), tx)410 require.NoError(t, err)411 require.NoError(t, tx.Commit())412 tx.Rollback()413 }414 require.Equal(t, tt.wantVersion, version)415 })416 }417}...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

...36 panic(fmt.Sprintf("Failed to connect to database: %s", err))37 }38 migrations.SetTableName("_migrations")39 cw.db.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`)40 oldVersion, newVersion, err := migrations.Run(cw.db, "init")41 if err != nil {42 return "", fmt.Errorf(err.Error())43 }44 switch args[0] {45 case "fresh":46 err = cw.disableForeignKeyConstraints()47 err = cw.dropAllTables()48 err = cw.enableForeignKeyConstraints()49 oldVersion, newVersion, err = migrations.Run(cw.db, "init")50 if err != nil {51 return "", fmt.Errorf(err.Error())52 }53 oldVersion, newVersion, err = migrations.Run(cw.db, "up")54 case "refresh":55 oldVersion, newVersion, err = migrations.Run(cw.db, "reset")56 if err != nil {57 return "", fmt.Errorf(err.Error())58 }59 oldVersion, newVersion, err = migrations.Run(cw.db, "up")60 case "rollback":61 oldVersion, newVersion, err = migrations.Run(cw.db, "down")62 case "status":63 //err = cw.status()64 case "migrate":65 oldVersion, newVersion, err = migrations.Run(cw.db, "up")66 default:67 oldVersion, newVersion, err = migrations.Run(cw.db, args...)68 }69 if err != nil {70 return "", fmt.Errorf(err.Error())71 }72 if newVersion != oldVersion {73 fmt.Printf("Migrated from version %d to %d\n", oldVersion, newVersion)74 } else {75 fmt.Println(colors.Green("Nothing to migrate."))76 }77 return78}79func Make(cmd string, name string) (msg string, err error) {80 switch cmd {81 case "migration":82 fmt.Println("Make Migration")83 timestamp := time.Now().UnixNano() / 100000084 filename := fmt.Sprintf("%s_%s.sql", strconv.FormatInt(timestamp, 10), strings.ToLower(name))85 emptyFile, err := os.Create("database/migrations/" + filename)86 if err != nil {87 panic(err.Error())...

Full Screen

Full Screen

errors.go

Source:errors.go Github

copy

Full Screen

...9// ErrorCommandRequired means that no command was specified.10const ErrorCommandRequired = Error("Command required")11// ErrorUnexpectedCommand means that command name is unknown.12const ErrorUnexpectedCommand = Error("Unexpected command")13// ErrorInvalidVersionArgumentFormat means that the format of an argument named `version` does not correspond to int64.14const ErrorInvalidVersionArgumentFormat = Error("Invalid version argument format")15// ErrorVersionNumberRequired means that no version number was specified via command line.16const ErrorVersionNumberRequired = Error("Version number required")17// ErrorMigrationsDatabaseAlreadyExists means that `migrations` database already exists.18const ErrorMigrationsDatabaseAlreadyExists = Error("Migrations database already exists")19// ErrorUnequalCountsOfMigrations means that the count of `up` migrations is not equal to the count of `down` migrations.20const ErrorUnequalCountsOfMigrations = Error("Unequal counts of `up` and `down` migrations")21// ErrorMigrationsAreNotInitialized means that migrations database is empty.22const ErrorMigrationsAreNotInitialized = Error("Migrations are not initialized, try to call `init` at first")23// ErrorTargetVersionNotFound means that the target migration version was not found in migrations list.24const ErrorTargetVersionNotFound = Error("Target migration version was not found")25// ErrorSomeMigrationsAreAbsent means that some migrations files are absent.26const ErrorSomeMigrationsAreAbsent = Error("Some migrations are absent")...

Full Screen

Full Screen

Version

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 m, err := migrate.New(4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(m.Version())8}

Full Screen

Full Screen

Version

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 version, dirty, err := m.Version()7 if err != nil {8 log.Fatal(err)9 }10 log.Printf("Version: %v, Dirty: %v", version, dirty)11}12import (13func main() {14 if err != nil {15 log.Fatal(err)16 }17 version, dirty, err := m.Version()18 if err != nil {19 log.Fatal(err)20 }21 log.Printf("Version: %v, Dirty: %v", version, dirty)22}23import (24func main() {25 if err != nil {26 log.Fatal(err)27 }28 if err := m.SetVersion(1); err != nil {29 log.Fatal(err)30 }31 version, dirty, err := m.Version()32 if err != nil {33 log.Fatal(err)34 }35 log.Printf("Version: %v, Dirty: %v", version, dirty)36}

Full Screen

Full Screen

Version

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 goose.SetDialect("postgres")4 goose.Run("status", goose.DBConf{5 }, "db/migrations")6}

Full Screen

Full Screen

Version

Using AI Code Generation

copy

Full Screen

1import "github.com/pressly/goose"2func main() {3 goose.SetDialect("postgres")4 goose.Run("status")5}6import "github.com/pressly/goose"7func main() {8 goose.SetDialect("postgres")9 goose.Run("up")10}11import "github.com/pressly/goose"12func main() {13 goose.SetDialect("postgres")14 goose.Run("down")15}16import "github.com/pressly/goose"17func main() {18 goose.SetDialect("postgres")19 goose.Run("create", "create_user_table")20}21import "github.com/pressly/goose"22func main() {23 goose.SetDialect("postgres")24 goose.Run("create", "create_user_table", "sql")25}26import "github.com/pressly/goose"27func main() {28 goose.SetDialect("postgres")29 goose.Run("create", "create_user_table", "sql")30}31import "github.com/pressly/goose"32func main() {33 goose.SetDialect("postgres")34 goose.Run("create", "create_user_table", "sql")35}36import "github.com/pressly/goose"37func main() {38 goose.SetDialect("postgres")39 goose.Run("create", "create_user_table", "sql")40}41import "github.com/pressly/goose"42func main() {43 goose.SetDialect("postgres")44 goose.Run("create", "create_user_table", "sql")45}46import "github.com/pressly/goose

Full Screen

Full Screen

Version

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "github.com/astaxie/beego/migration"3func main() {4m := new(migrations.Migration)5fmt.Println(m.Version())6}

Full Screen

Full Screen

Version

Using AI Code Generation

copy

Full Screen

1func main() {2 migrations.Version()3}4import "fmt"5type Migrations struct {6}7func (m Migrations) Version() {8 fmt.Println("version 1")9}10import "fmt"11type Migrations struct {12}13func (m Migrations) Version() {14 fmt.Println("version 2")15}

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