Example #1
0
func TestSetVersion(t *testing.T) {
	db := connectDB()

	migrations.Set([]*migrations.Migration{
		{Version: 1, Up: doPanic, Down: doPanic},
		{Version: 2, Up: doPanic, Down: doPanic},
		{Version: 3, Up: doPanic, Down: doPanic},
	})

	for i := 0; i < 5; i++ {
		wantOldVersion := int64(i)
		wantNewVersion := int64(i + 1)

		oldVersion, newVersion, err := migrations.Run(
			db, "set_version", fmt.Sprint(wantNewVersion))
		if err != nil {
			t.Fatal(err)
		}
		if oldVersion != wantOldVersion {
			t.Fatalf("got %d, wanted %d", oldVersion, wantOldVersion)
		}
		if newVersion != wantNewVersion {
			t.Fatalf("got %d, wanted %d", newVersion, wantNewVersion)
		}

		version, err := migrations.Version(db)
		if err != nil {
			t.Fatal(err)
		}
		if version != wantNewVersion {
			t.Fatalf("got version %d, wanted %d", version, wantNewVersion)
		}
	}
}
Example #2
0
func TestUpDown(t *testing.T) {
	db := connectDB()

	migrations.Set([]*migrations.Migration{
		{Version: 1, Up: doNothing, Down: doNothing},
		{Version: 2, Up: doNothing, Down: doNothing},
		{Version: 3, Up: doNothing, Down: doNothing},
	})
	oldVersion, newVersion, err := migrations.Run(db, "up")
	if err != nil {
		t.Fatal(err)
	}
	if oldVersion != 0 {
		t.Fatalf("got %d, wanted 0", oldVersion)
	}
	if newVersion != 3 {
		t.Fatalf("got %d, wanted 3", newVersion)
	}

	version, err := migrations.Version(db)
	if err != nil {
		t.Fatal(err)
	}
	if version != 3 {
		t.Fatalf("got version %d, wanted 3", version)
	}

	for i := 2; i >= -5; i-- {
		wantOldVersion := int64(i + 1)
		wantNewVersion := int64(i)
		if wantNewVersion < 0 {
			wantOldVersion = 0
			wantNewVersion = 0
		}

		oldVersion, newVersion, err = migrations.Run(db, "down")
		if err != nil {
			t.Fatal(err)
		}
		if oldVersion != wantOldVersion {
			t.Fatalf("got %d, wanted %d", oldVersion, wantOldVersion)
		}
		if newVersion != wantNewVersion {
			t.Fatalf("got %d, wanted %d", newVersion, wantNewVersion)
		}

		version, err = migrations.Version(db)
		if err != nil {
			t.Fatal(err)
		}
		if version != wantNewVersion {
			t.Fatalf("got version %d, wanted %d", version, wantNewVersion)
		}
	}
}