Last active
January 10, 2021 15:31
-
-
Save Daniel1984/4616113d1fcac0d321d0ab6834443266 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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