60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"recipes/internal/config"
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
|
_ "github.com/golang-migrate/migrate/v4/source/file"
|
|
)
|
|
|
|
func main() {
|
|
var migrationsPath string
|
|
var down bool
|
|
|
|
flag.StringVar(&migrationsPath, "migrations-path", "", "path to migrations")
|
|
flag.BoolVar(&down, "down", false, "")
|
|
// flag.StringVar(&configFile, "config", "", "config file location")
|
|
|
|
flag.Parse()
|
|
|
|
if migrationsPath == "" {
|
|
panic("migrations-path is required")
|
|
}
|
|
|
|
// load config
|
|
cfg := config.MustLoad()
|
|
|
|
m, err := migrate.New("file://"+migrationsPath, fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", cfg.Postgresql.User, cfg.Postgresql.Password, cfg.Postgresql.Address, cfg.Postgresql.DBName))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if down {
|
|
if err := m.Down(); err != nil {
|
|
if errors.Is(err, migrate.ErrNoChange) {
|
|
fmt.Println("no migrations downed")
|
|
return
|
|
}
|
|
|
|
panic(err)
|
|
}
|
|
fmt.Println("migrations downed successfully")
|
|
return
|
|
}
|
|
|
|
if err := m.Up(); err != nil {
|
|
if errors.Is(err, migrate.ErrNoChange) {
|
|
fmt.Println("no migrations apply")
|
|
return
|
|
}
|
|
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println("migrations applied successfully")
|
|
}
|