Commits

ArtemTitoulenko committed a136a28

allow migrating up or down to a target commit

migrate up and down towards targets

going down to a target does not run it's `down`
function. Going up to a target does run it's `up`
function.

  • Participants
  • Parent commits c7df92c

Comments (0)

Files changed (3)

File cmd/goose/cmd_down.go

 	"log"
 )
 
+var downTarget *int64
+
 var downCmd = &Command{
 	Name:    "down",
-	Usage:   "",
-	Summary: "Roll back the version by 1",
+	Usage:   "[--target=]",
+	Summary: "Roll back the version by 1, or to a target migration",
 	Help:    `down extended help here...`,
 	Run:     downRun,
 }
 
+func init() {
+	downTarget = downCmd.Flag.Int64("target", -1, "the target migration to roll back to")
+}
+
 func downRun(cmd *Command, args ...string) {
 
 	conf, err := dbConfFromFlags()
 		log.Fatal(err)
 	}
 
-	previous, err := goose.GetPreviousDBVersion(conf.MigrationsDir, current)
-	if err != nil {
-		log.Fatal(err)
+	if *downTarget < 0 || *downTarget >= current {
+		*downTarget, err = goose.GetPreviousDBVersion(conf.MigrationsDir, current)
+		if err != nil {
+			log.Fatal(err)
+		}
 	}
 
-	if err = goose.RunMigrations(conf, conf.MigrationsDir, previous); err != nil {
+	if err = goose.RunMigrations(conf, conf.MigrationsDir, *downTarget); err != nil {
 		log.Fatal(err)
 	}
 }

File cmd/goose/cmd_up.go

 	"log"
 )
 
+// the target version to migrate down to
+var upTarget *int64
+
 var upCmd = &Command{
 	Name:    "up",
-	Usage:   "",
-	Summary: "Migrate the DB to the most recent version available",
+	Usage:   "[--target=]",
+	Summary: "Migrate the DB to the most recent version available, or until a target version",
 	Help:    `up extended help here...`,
 	Run:     upRun,
 }
 
+func init() {
+	upTarget = upCmd.Flag.Int64("target", -1, "the target migration to migrate to, default: latest")
+}
+
 func upRun(cmd *Command, args ...string) {
 
 	conf, err := dbConfFromFlags()
 		log.Fatal(err)
 	}
 
-	target, err := goose.GetMostRecentDBVersion(conf.MigrationsDir)
+	recent, err := goose.GetMostRecentDBVersion(conf.MigrationsDir)
 	if err != nil {
 		log.Fatal(err)
 	}
 
-	if err := goose.RunMigrations(conf, conf.MigrationsDir, target); err != nil {
+	if *upTarget > recent || *upTarget < 0 {
+		*upTarget = recent
+	}
+
+	if err := goose.RunMigrations(conf, conf.MigrationsDir, *upTarget); err != nil {
 		log.Fatal(err)
 	}
 }

File db-sample/dbconf.yml

 
 test:
     driver: postgres
-    open: user=liam dbname=tester sslmode=disable
+    open: dbname=goose-tester sslmode=disable
 
 development:
     driver: postgres
-    open: user=liam dbname=tester sslmode=disable
-    
+    open: dbname=goose-tester sslmode=disable
+
 production:
     driver: postgres
-    open: user=liam dbname=tester sslmode=verify-full
+    open: dbname=goose-tester sslmode=verify-full
 
 customimport:
     driver: customdriver