Skip to content

Commit

Permalink
build(migrations): add a program to run migrations on the database th…
Browse files Browse the repository at this point in the history
…at extracts the data from env vars
  • Loading branch information
danvergara committed May 1, 2021
1 parent 3af82ba commit 5e38fcb
Show file tree
Hide file tree
Showing 3 changed files with 484 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/dbmigrate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"log"

"github.com/danvergara/dblab/pkg/config"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)

func main() {
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
}
}
}
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ module github.com/danvergara/dblab
go 1.16

require (
contrib.go.opencensus.io/exporter/stackdriver v0.6.0 // indirect
git.apache.org/thrift.git v0.0.0-20180924222215-a9235805469b // indirect
github.com/cznic/ql v1.2.0 // indirect
github.com/go-ini/ini v1.39.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang-migrate/migrate/v4 v4.14.1 // indirect
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7 // indirect
github.com/googleapis/gax-go v2.0.0+incompatible // indirect
github.com/gotestyourself/gotestyourself v2.1.0+incompatible // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgx v3.2.0+incompatible // indirect
github.com/jmoiron/sqlx v1.3.3 // indirect
github.com/jroimartin/gocui v0.4.0 // indirect
github.com/kshvakov/clickhouse v1.3.4 // indirect
github.com/lib/pq v1.10.0 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/nsf/termbox-go v1.1.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/openzipkin/zipkin-go v0.1.1 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
Expand Down

0 comments on commit 5e38fcb

Please sign in to comment.