Example #1
0
func TestMigrate_Rollback(t *testing.T) {
	db := newDB(t)
	defer db.Close()

	migration := migrate.Migration{
		ID: 1,
		Up: func(tx *sql.Tx) error {
			// This should completely ok
			if _, err := tx.Exec("CREATE TABLE people (id int)"); err != nil {
				return err
			}
			// This should throw an error
			if _, err := tx.Exec("ALTER TABLE foo ADD COLUMN first_name text"); err != nil {
				return err
			}
			return nil
		},
	}

	err := migrate.Exec(db, migrate.Up, migration)
	assert.Error(t, err)
	assert.Equal(t, []int{}, appliedMigrations(t, db))
	// If the transaction wasn't rolled back, we'd see a people table.
	assertSchema(t, ``, db)
	assert.IsType(t, &migrate.MigrationError{}, err)
}
Example #2
0
func Example() {
	migrations := []migrate.Migration{
		{
			ID: 1,
			Up: func(tx *sql.Tx) error {
				_, err := tx.Exec("CREATE TABLE people (id int)")
				return err
			},
			Down: func(tx *sql.Tx) error {
				_, err := tx.Exec("DROP TABLE people")
				return err
			},
		},
		{
			ID: 2,
			// For simple sql migrations, you can use the migrate.Queries
			// helper.
			Up: migrate.Queries([]string{
				"ALTER TABLE people ADD COLUMN first_name text",
			}),
			Down: func(tx *sql.Tx) error {
				// It's not possible to remove a column with
				// sqlite.
				_, err := tx.Exec("SELECT 1 FROM people")
				return err
			},
		},
	}

	db, _ := sql.Open("sqlite3", ":memory:")
	_ = migrate.Exec(db, migrate.Up, migrations...)
}
Example #3
0
func TestMigrate(t *testing.T) {
	db := newDB(t)
	defer db.Close()

	migrations := testMigrations[:]

	err := migrate.Exec(db, migrate.Up, migrations...)
	assert.NoError(t, err)
	assert.Equal(t, []int{1, 2}, appliedMigrations(t, db))
	assertSchema(t, `
people
CREATE TABLE people (id int, first_name text)
`, db)

	err = migrate.Exec(db, migrate.Down, migrations...)
	assert.NoError(t, err)
	assert.Equal(t, []int{}, appliedMigrations(t, db))
	assertSchema(t, ``, db)
}
Example #4
0
func TestMigrate_Individual(t *testing.T) {
	db := newDB(t)
	defer db.Close()

	err := migrate.Exec(db, migrate.Up, testMigrations[0])
	assert.NoError(t, err)
	assert.Equal(t, []int{1}, appliedMigrations(t, db))
	assertSchema(t, `
people
CREATE TABLE people (id int)
`, db)

	err = migrate.Exec(db, migrate.Up, testMigrations[1])
	assert.NoError(t, err)
	assert.Equal(t, []int{1, 2}, appliedMigrations(t, db))
	assertSchema(t, `
people
CREATE TABLE people (id int, first_name text)
`, db)
}
Example #5
0
func TestMigrate_AlreadyRan(t *testing.T) {
	db := newDB(t)
	defer db.Close()

	migration := testMigrations[0]

	err := migrate.Exec(db, migrate.Up, migration)
	assert.NoError(t, err)
	assert.Equal(t, []int{1}, appliedMigrations(t, db))
	assertSchema(t, `
people
CREATE TABLE people (id int)
`, db)

	err = migrate.Exec(db, migrate.Up, migration)
	assert.NoError(t, err)
	assert.Equal(t, []int{1}, appliedMigrations(t, db))
	assertSchema(t, `
people
CREATE TABLE people (id int)
`, db)
}
Example #6
0
func TestMigrate_Order(t *testing.T) {
	db := newDB(t)
	defer db.Close()

	migrations := []migrate.Migration{
		testMigrations[1],
		testMigrations[0],
	}

	err := migrate.Exec(db, migrate.Up, migrations...)
	assert.NoError(t, err)
	assert.Equal(t, []int{1, 2}, appliedMigrations(t, db))
	assertSchema(t, `
people
CREATE TABLE people (id int, first_name text)
`, db)
}