Example #1
0
func TestMigration_Up(t *testing.T) {
	func() {
		tempDir, err := ioutil.TempDir("", "TestMigration_Up")
		if err != nil {
			t.Fatal(err)
		}
		dbpath := filepath.Join(tempDir, "TestMigration_Up.sqlite3")
		defer os.RemoveAll(tempDir)
		origStdout, origStderr := os.Stdout, os.Stderr
		defer func() {
			os.Stdout, os.Stderr = origStdout, origStderr
		}()
		devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0755)
		if err != nil {
			t.Fatal(err)
		}
		os.Stdout, os.Stderr = devnull, devnull
		kocha.TxTypeMap["genmai"] = &kocha.GenmaiTransaction{}
		config := kocha.DatabaseConfig{Driver: "sqlite3", DSN: dbpath}
		m := &testMigration{}
		if err := kocha.Migrate(config, m).Up(-1); err != nil {
			t.Fatal(err)
		}
		actual := m.called
		expected := []string{"Up_20140303012000_TestMig1", "Up_20140309121357_TestMig2"}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Up(-1) => %#v, want %#v", config, m, actual, expected)
		}

		m.called = nil
		if err := kocha.Migrate(config, m).Up(-1); err != nil {
			t.Fatal(err)
		}
		actual = m.called
		expected = nil
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Up(-1) => %#v, want %#v", config, m, actual, expected)
		}
	}()

	func() {
		tempDir, err := ioutil.TempDir("", "TestMigration_Up")
		if err != nil {
			t.Fatal(err)
		}
		dbpath := filepath.Join(tempDir, "TestMigration_Up.sqlite3")
		defer os.RemoveAll(tempDir)
		origStdout, origStderr := os.Stdout, os.Stderr
		defer func() {
			os.Stdout, os.Stderr = origStdout, origStderr
		}()
		devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0755)
		if err != nil {
			t.Fatal(err)
		}
		os.Stdout, os.Stderr = devnull, devnull
		kocha.TxTypeMap["genmai"] = &kocha.GenmaiTransaction{}
		config := kocha.DatabaseConfig{Driver: "sqlite3", DSN: dbpath}
		m := &testMigration{}
		if err := kocha.Migrate(config, m).Up(1); err != nil {
			t.Fatal(err)
		}
		actual := m.called
		expected := []string{"Up_20140303012000_TestMig1"}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Up(1) => %#v, want %#v", config, m, actual, expected)
		}

		m.called = nil
		if err := kocha.Migrate(config, m).Up(1); err != nil {
			t.Fatal(err)
		}
		actual = m.called
		expected = []string{"Up_20140309121357_TestMig2"}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Up(1) => %#v, want %#v", config, m, actual, expected)
		}

		m.called = nil
		if err := kocha.Migrate(config, m).Up(1); err != nil {
			t.Fatal(err)
		}
		actual = m.called
		expected = nil
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Up(1) => %#v, want %#v", config, m, actual, expected)
		}
	}()
}
Example #2
0
func TestMigration_Down(t *testing.T) {
	init := func(dbpath string) {
		db, err := sql.Open("sqlite3", dbpath)
		if err != nil {
			t.Fatal(err)
		}
		for _, query := range []string{
			`DROP TABLE IF EXISTS schema_migration`,
			`CREATE TABLE IF NOT EXISTS schema_migration (version varchar(255) PRIMARY KEY)`,
			`INSERT INTO schema_migration VALUES ('20140303012000'), ('20140309121357')`,
		} {
			if _, err := db.Exec(query); err != nil {
				t.Fatal(err)
			}
		}
	}

	func() {
		tempDir, err := ioutil.TempDir("", "TestMigration_Down")
		if err != nil {
			t.Fatal(err)
		}
		dbpath := filepath.Join(tempDir, "TestMigration_Down.sqlite3")
		defer os.RemoveAll(tempDir)
		init(dbpath)
		origStdout, origStderr := os.Stdout, os.Stderr
		defer func() {
			os.Stdout, os.Stderr = origStdout, origStderr
		}()
		devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0755)
		if err != nil {
			t.Fatal(err)
		}
		os.Stdout, os.Stderr = devnull, devnull
		kocha.TxTypeMap["genmai"] = &kocha.GenmaiTransaction{}
		config := kocha.DatabaseConfig{Driver: "sqlite3", DSN: dbpath}
		m := &testMigration{}
		if err := kocha.Migrate(config, m).Down(-1); err != nil {
			t.Fatal(err)
		}
		actual := m.called
		expected := []string{"Down_20140309121357_TestMig2"}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Down(-1) => %#v, want %#v", config, m, actual, expected)
		}

		m.called = nil
		if err := kocha.Migrate(config, m).Down(-1); err != nil {
			t.Fatal(err)
		}
		actual = m.called
		expected = []string{"Down_20140303012000_TestMig1"}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Down(-1) => %#v, want %#v", config, m, actual, expected)
		}
	}()

	func() {
		tempDir, err := ioutil.TempDir("", "TestMigration_Down")
		if err != nil {
			t.Fatal(err)
		}
		dbpath := filepath.Join(tempDir, "TestMigration_Down.sqlite3")
		defer os.RemoveAll(tempDir)
		init(dbpath)
		origStdout, origStderr := os.Stdout, os.Stderr
		defer func() {
			os.Stdout, os.Stderr = origStdout, origStderr
		}()
		devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0755)
		if err != nil {
			t.Fatal(err)
		}
		os.Stdout, os.Stderr = devnull, devnull
		kocha.TxTypeMap["genmai"] = &kocha.GenmaiTransaction{}
		config := kocha.DatabaseConfig{Driver: "sqlite3", DSN: dbpath}
		m := &testMigration{}
		if err := kocha.Migrate(config, m).Down(3); err != nil {
			t.Fatal(err)
		}
		actual := m.called
		expected := []string{"Down_20140309121357_TestMig2", "Down_20140303012000_TestMig1"}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Down(3) => %#v, want %#v", config, m, actual, expected)
		}

		m.called = nil
		if err := kocha.Migrate(config, m).Down(2); err != nil {
			t.Fatal(err)
		}
		actual = m.called
		expected = nil
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Down(2) => %#v, want %#v", config, m, actual, expected)
		}

		m.called = nil
		if err := kocha.Migrate(config, m).Down(1); err != nil {
			t.Fatal(err)
		}
		actual = m.called
		expected = nil
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Migrate(%#v, %#v).Down(1) => %#v, want %#v", config, m, actual, expected)
		}
	}()
}
Example #3
0
// AUTO-GENERATED by Kocha.
// DO NOT CHANGE this method signature.
func Down(config kocha.DatabaseConfig, n int) error {
	return kocha.Migrate(config, &Migration{}).Down(n)
}