func Exec(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection) (int, error) {
	return ExecMax(db, dialect, m, dir, 0)
}
Execute a set of migrations

Returns the number of applied migrations.
package migrate
import (
	"bytes"
	"database/sql"
	"errors"
	"fmt"
	"io"
	"os"
	"path"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"
	"github.com/rubenv/sql-migrate/sqlparse"
	"gopkg.in/gorp.v1"
)
type MigrationDirection int
const (
	Up MigrationDirection = iota
	Down
)
var tableName = "gorp_migrations"
var schemaName = ""
var numberPrefixRegex = regexp.MustCompile(`^(\d+).*$`)
func SetTable(name string) {
	if name != "" {
		tableName = name
	}
}
func SetSchema(name string) {
	if name != "" {
		schemaName = name
	}
}
func getTableName() string {
	t := tableName
	if schemaName != "" {
		t = fmt.Sprintf("%s.%s", schemaName, t)
	}
	return t
}
type Migration struct {
	Id   string
	Up   []string
	Down []string
}
func (m Migration) Less(other *Migration) bool {
	switch {
	case m.isNumeric() && other.isNumeric():
		return m.VersionInt() < other.VersionInt()
	case m.isNumeric() && !other.isNumeric():
		return true
	case !m.isNumeric() && other.isNumeric():
		return false
	default:
		return m.Id < other.Id
	}
}
func (m Migration) isNumeric() bool {
	return len(m.NumberPrefixMatches()) > 0
}
func (m Migration) NumberPrefixMatches() []string {
	return numberPrefixRegex.FindStringSubmatch(m.Id)
}
func (m Migration) VersionInt() int64 {
	v := m.NumberPrefixMatches()[1]
	value, err := strconv.ParseInt(v, 10, 64)
	if err != nil {
		panic(fmt.Sprintf("Could not parse %q into int64: %s", v, err))
	}
	return value
}
type PlannedMigration struct {
	*Migration
	Queries []string
}
type byId []*Migration
func (b byId) Len() int           { return len(b) }
func (b byId) Swap(i, j int)      { b[i], b[j] = b[j], b[i] }
func (b byId) Less(i, j int) bool { return b[i].Less(b[j]) }
type MigrationRecord struct {
	Id        string    `db:"id"`
	AppliedAt time.Time `db:"applied_at"`
}
var MigrationDialects = map[string]gorp.Dialect{
	"sqlite3":  gorp.SqliteDialect{},
	"postgres": gorp.PostgresDialect{},
	"mysql":    gorp.MySQLDialect{"InnoDB", "UTF8"},
	"mssql":    gorp.SqlServerDialect{},
	"oci8":     gorp.OracleDialect{},
}
type MigrationSource interface {
	FindMigrations() ([]*Migration, error)
}
type MemoryMigrationSource struct {
	Migrations []*Migration
}
var _ MigrationSource = (*MemoryMigrationSource)(nil)
func (m MemoryMigrationSource) FindMigrations() ([]*Migration, error) {
	sort.Sort(byId(m.Migrations))
	return m.Migrations, nil
}
type FileMigrationSource struct {
	Dir string
}
var _ MigrationSource = (*FileMigrationSource)(nil)
func (f FileMigrationSource) FindMigrations() ([]*Migration, error) {
	migrations := make([]*Migration, 0)
	file, err := os.Open(f.Dir)
	if err != nil {
		return nil, err
	}
	files, err := file.Readdir(0)
	if err != nil {
		return nil, err
	}
	for _, info := range files {
		if strings.HasSuffix(info.Name(), ".sql") {
			file, err := os.Open(path.Join(f.Dir, info.Name()))
			if err != nil {
				return nil, err
			}
			migration, err := ParseMigration(info.Name(), file)
			if err != nil {
				return nil, err
			}
			migrations = append(migrations, migration)
		}
	}
	sort.Sort(byId(migrations))
	return migrations, nil
}
type AssetMigrationSource struct {
	Asset func(path string) ([]byte, error)
	AssetDir func(path string) ([]string, error)
	Dir string
}
var _ MigrationSource = (*AssetMigrationSource)(nil)
func (a AssetMigrationSource) FindMigrations() ([]*Migration, error) {
	migrations := make([]*Migration, 0)
	files, err := a.AssetDir(a.Dir)
	if err != nil {
		return nil, err
	}
	for _, name := range files {
		if strings.HasSuffix(name, ".sql") {
			file, err := a.Asset(path.Join(a.Dir, name))
			if err != nil {
				return nil, err
			}
			migration, err := ParseMigration(name, bytes.NewReader(file))
			if err != nil {
				return nil, err
			}
			migrations = append(migrations, migration)
		}
	}
	sort.Sort(byId(migrations))
	return migrations, nil
}
func ParseMigration(id string, r io.ReadSeeker) (*Migration, error) {
	m := &Migration{
		Id: id,
	}
	up, err := sqlparse.SplitSQLStatements(r, true)
	if err != nil {
		return nil, err
	}
	down, err := sqlparse.SplitSQLStatements(r, false)
	if err != nil {
		return nil, err
	}
	m.Up = up
	m.Down = down
	return m, nil
}