Commits

Liam Staskawicz committed 9a39a80

re-org: move more SQL specific code into migration_sql.go. also, acknowledge that we're only going to run one go migration at a time for now

Comments (0)

Files changed (3)

 
 	for _, v := range migrations.Versions {
 
-		txn, err := db.Begin()
-		if err != nil {
-			log.Fatal("db.Begin:", err)
-		}
-
 		var numStatements int
 		var e error
 
 
 		switch path.Ext(filepath) {
 		case ".go":
-			numStatements, e = runGoMigration(txn, conf, filepath, v, migrations.Direction)
+			numStatements, e = runGoMigration(conf, filepath, v, migrations.Direction)
 		case ".sql":
-			numStatements, e = runSQLMigration(txn, filepath, v, migrations.Direction)
+			numStatements, e = runSQLMigration(db, filepath, v, migrations.Direction)
 		}
 
 		if e != nil {
-			txn.Rollback()
-			fmt.Printf("FAIL %s (%v), quitting migration.", path.Base(filepath), e)
-			os.Exit(1)
-		}
-
-		if e = finalizeMigration(txn, migrations, v); e != nil {
-			fmt.Printf("error finalizing migration %s, quitting. (%v)", path.Base(filepath), e)
-			os.Exit(1)
+			log.Fatalf("FAIL %v, quitting migration", e)
 		}
 
 		fmt.Printf("OK   %s (%d statements)\n", path.Base(filepath), numStatements)
 	}
 }
 
-// Update the version table for the given migration,
-// and finalize the transaction.
-func finalizeMigration(txn *sql.Tx, mm *MigrationMap, v int) error {
-
-	if mm.Direction == false {
-		v--
-	}
-
-	// XXX: drop goose_db_version table on some minimum version number?
-	versionStmt := fmt.Sprintf("INSERT INTO goose_db_version (version_id) VALUES (%d);", v)
-	if _, err := txn.Exec(versionStmt); err != nil {
-		txn.Rollback()
-		return err
-	}
-
-	return txn.Commit()
-}
-
 // collect all the valid looking migration scripts in the 
 // migrations folder, and key them by version
 func collectMigrations(dirpath string, currentVersion int) (mm *MigrationMap, err error) {

cmd/migration_go.go

 
 import (
 	"bufio"
-	"database/sql"
 	"fmt"
 	"io"
 	"io/ioutil"
 )
 
 type TemplateData struct {
-	Versions  []string
+	Version   string
 	DBDriver  string
 	DBOpen    string
 	Direction string
 // original .go migration, and execute it via `go run` along
 // with a main() of our own creation.
 //
-func runGoMigration(txn *sql.Tx, conf *DBConf, path string, version int, direction bool) (int, error) {
+func runGoMigration(conf *DBConf, path string, version int, direction bool) (int, error) {
 
 	// everything gets written to a temp file, and zapped afterwards
 	d, e := ioutil.TempDir("", "goose")
 	defer os.RemoveAll(d)
 
 	td := &TemplateData{
-		Versions:  []string{fmt.Sprintf("%d", version)},
+		Version:   fmt.Sprintf("%d", version),
 		DBDriver:  conf.Driver,
 		DBOpen:    conf.OpenStr,
 		Direction: directionStr(direction),
 	if err != nil {
 		log.Fatal("failed to open DB:", err)
 	}
-{{range .Versions}}
+	defer db.Close()
 
-	// ----- migration {{ . }} -----
 	txn, err := db.Begin()
 	if err != nil {
 		log.Fatal("db.Begin:", err)
 	}
 
-	migration_{{ . }}_{{$.Direction}}(txn)
+	migration_{{ .Version }}_{{ .Direction }}(txn)
 
 	e := txn.Commit()
 	if e != nil {
 		log.Fatal("Commit() failed:", e)
-	}{{end}}
+	}
 }
 `))

cmd/migration_sql.go

 
 import (
 	"database/sql"
+	"fmt"
 	"io/ioutil"
 	"log"
+	"path"
 	"strings"
 )
 
 //
 // All statements following an Up or Down directive are grouped together
 // until another direction directive is found.
-func runSQLMigration(txn *sql.Tx, path string, v int, direction bool) (count int, err error) {
+func runSQLMigration(db *sql.DB, script string, v int, direction bool) (count int, err error) {
 
-	f, err := ioutil.ReadFile(path)
+	txn, err := db.Begin()
+	if err != nil {
+		log.Fatal("db.Begin:", err)
+	}
+	defer db.Close()
+
+	f, err := ioutil.ReadFile(script)
 	if err != nil {
 		log.Fatal(err)
 	}
 		}
 
 		if _, err = txn.Exec(query); err != nil {
-			log.Println("error executing query:\n", query)
+			txn.Rollback()
+			log.Fatalf("FAIL %s (%v), quitting migration.", path.Base(script), err)
 			return count, err
 		}
 
 		count++
 	}
 
+	if err = finalizeMigration(txn, direction, v); err != nil {
+		log.Fatalf("error finalizing migration %s, quitting. (%v)", path.Base(script), err)
+	}
+
 	return count, nil
 }
+
+// Update the version table for the given migration,
+// and finalize the transaction.
+func finalizeMigration(txn *sql.Tx, direction bool, v int) error {
+
+	if direction == false {
+		v--
+	}
+
+	// XXX: drop goose_db_version table on some minimum version number?
+	versionStmt := fmt.Sprintf("INSERT INTO goose_db_version (version_id) VALUES (%d);", v)
+	if _, err := txn.Exec(versionStmt); err != nil {
+		txn.Rollback()
+		return err
+	}
+
+	return txn.Commit()
+}