Merge pull request #55 from pressly/create

Create command reads .go files from FS
pull/41/head^2
Vojtech Vitek 2017-06-20 15:03:04 -04:00 committed by GitHub
commit 226da2c1b9
2 changed files with 41 additions and 19 deletions

View File

@ -18,7 +18,7 @@ var (
// MaxVersion is the maximum allowed version.
MaxVersion int64 = 9223372036854775807 // max(int64)
goMigrations []*Migration
registeredGoMigrations = map[int64]*Migration{}
)
// Migrations slice.
@ -93,9 +93,12 @@ func AddMigration(up func(*sql.Tx) error, down func(*sql.Tx) error) {
// AddNamedMigration : Add a named migration.
func AddNamedMigration(filename string, up func(*sql.Tx) error, down func(*sql.Tx) error) {
v, _ := NumericComponent(filename)
migration := &Migration{Version: v, Next: -1, Previous: -1, UpFn: up, DownFn: down, Source: filename}
migration := &Migration{Version: v, Next: -1, Previous: -1, Registered: true, UpFn: up, DownFn: down, Source: filename}
goMigrations = append(goMigrations, migration)
if existing, ok := registeredGoMigrations[v]; ok {
panic(fmt.Sprintf("failed to add migration %q: version conflicts with %q", filename, existing.Source))
}
registeredGoMigrations[v] = migration
}
// CollectMigrations returns all the valid looking migration scripts in the
@ -103,18 +106,12 @@ func AddNamedMigration(filename string, up func(*sql.Tx) error, down func(*sql.T
func CollectMigrations(dirpath string, current, target int64) (Migrations, error) {
var migrations Migrations
// extract the numeric component of each migration,
// filter out any uninteresting files,
// and ensure we only have one file per migration version.
sqlMigrations, err := filepath.Glob(dirpath + "/*.sql")
sqlMigrationsSubDirectories, err := filepath.Glob(dirpath + "/**/*.sql")
sqlMigrations = append(sqlMigrations, sqlMigrationsSubDirectories...)
// SQL migration files.
sqlMigrationFiles, err := filepath.Glob(dirpath + "/**.sql")
if err != nil {
return nil, err
}
for _, file := range sqlMigrations {
for _, file := range sqlMigrationFiles {
v, err := NumericComponent(file)
if err != nil {
return nil, err
@ -125,7 +122,8 @@ func CollectMigrations(dirpath string, current, target int64) (Migrations, error
}
}
for _, migration := range goMigrations {
// Go migrations registered via goose.AddMigration().
for _, migration := range registeredGoMigrations {
v, err := NumericComponent(migration.Source)
if err != nil {
return nil, err
@ -135,6 +133,26 @@ func CollectMigrations(dirpath string, current, target int64) (Migrations, error
}
}
// Go migration files
goMigrationFiles, err := filepath.Glob(dirpath + "/**.go")
if err != nil {
return nil, err
}
for _, file := range goMigrationFiles {
v, err := NumericComponent(file)
if err != nil {
continue // Skip any files that don't have start with version.
}
// Skip migrations already registered via goose.AddMigration().
if _, ok := registeredGoMigrations[v]; ok {
continue
}
if versionFilter(v, current, target) {
migration := &Migration{Version: v, Next: -1, Previous: -1, Source: file, Registered: false}
migrations = append(migrations, migration)
}
}
migrations = sortAndConnectMigrations(migrations)
return migrations, nil

View File

@ -20,12 +20,13 @@ type MigrationRecord struct {
// Migration struct.
type Migration struct {
Version int64
Next int64 // next version, or -1 if none
Previous int64 // previous version, -1 if none
Source string // path to .sql script
UpFn func(*sql.Tx) error // Up go migration function
DownFn func(*sql.Tx) error // Down go migration function
Version int64
Next int64 // next version, or -1 if none
Previous int64 // previous version, -1 if none
Source string // path to .sql script
Registered bool
UpFn func(*sql.Tx) error // Up go migration function
DownFn func(*sql.Tx) error // Down go migration function
}
func (m *Migration) String() string {
@ -50,6 +51,9 @@ func (m *Migration) run(db *sql.DB, direction bool) error {
}
case ".go":
if !m.Registered {
log.Fatalf("failed to apply Go migration %q: Go functions must be registered and built into a custom binary (see https://github.com/pressly/goose/tree/master/examples/go-migrations)", m.Source)
}
tx, err := db.Begin()
if err != nil {
log.Fatal("db.Begin: ", err)