Skip to content

Instantly share code, notes, and snippets.

@Daniel1984
Last active January 10, 2021 15:31
Show Gist options
  • Save Daniel1984/4616113d1fcac0d321d0ab6834443266 to your computer and use it in GitHub Desktop.
Save Daniel1984/4616113d1fcac0d321d0ab6834443266 to your computer and use it in GitHub Desktop.
// cmd/dbmigrate/main.go
package main
import (
"log"
"github.com/boilerplate/pkg/config"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
cfg := config.Get()
direction := cfg.GetMigration()
if direction != "down" && direction != "up" {
log.Println("-migrate accepts [up, down] values only")
return
}
m, err := migrate.New("file://db/migrations", cfg.GetDBConnStr())
if err != nil {
log.Printf("%s", err)
return
}
if direction == "up" {
if err := m.Up(); err != nil {
log.Printf("failed migrate up: %s", err)
return
}
}
if direction == "down" {
if err := m.Down(); err != nil {
log.Printf("failed migrate down: %s", err)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment