Last active
February 15, 2019 12:48
-
-
Save lukaspj/eee69764cca2b58bb58fc368e4658a00 to your computer and use it in GitHub Desktop.
Can't acquire lock error
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
package main | |
import ( | |
"database/sql" | |
"fmt" | |
"github.com/golang-migrate/migrate/v4" | |
"github.com/golang-migrate/migrate/v4/database/postgres" | |
"github.com/pkg/errors" | |
"strings" | |
"time" | |
// File-based Migration Source | |
_ "github.com/golang-migrate/migrate/v4/source/file" | |
"github.com/spf13/viper" | |
// Postgres Driver | |
_ "github.com/lib/pq" | |
) | |
func GetConfig() *viper.Viper { | |
v := viper.New() | |
v.SetDefault("config_path", ".") | |
v.SetDefault("postgres.host", "ds-db.minikube") | |
v.SetDefault("postgres.port", "30432") | |
v.SetDefault("postgres.database", "postgres") | |
v.SetDefault("postgres.username", "postgres") | |
v.SetDefault("postgres.password", "fes10val") | |
v.SetDefault("migrations.location", "./migrations/ds-messages") | |
v.SetDefault("database.fresh", false) | |
configs := []string{ | |
"config_path", | |
"postgres.host", | |
"postgres.port", | |
"postgres.database", | |
"postgres.username", | |
"postgres.password", | |
"messages.database.fresh", | |
"messages.migrations.location", | |
} | |
for _, s := range configs { | |
err := v.BindEnv(s) | |
if err != nil { | |
panic(fmt.Errorf("Fatal error binding env %s: %s \n", s, err)) | |
} | |
} | |
r := strings.NewReplacer(".", "_") | |
v.SetEnvKeyReplacer(r) | |
p := v.GetString("config_path") | |
v.AddConfigPath(p) | |
v.SetConfigName("config") | |
err := v.ReadInConfig() | |
if err != nil { | |
println(fmt.Errorf("Fatal error config file: %s \n", err)) | |
} | |
return v | |
} | |
func main() { | |
v := GetConfig() | |
db, err := sql.Open("postgres", | |
fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", | |
v.GetString("postgres.username"), | |
v.GetString("postgres.password"), | |
v.GetString("postgres.host"), | |
v.GetString("postgres.port"), | |
v.GetString("postgres.database"))) | |
if err != nil { | |
println(fmt.Sprintf("%+v", errors.Wrap(err, "failed to connect to database"))) | |
return | |
} | |
driver, err := postgres.WithInstance(db, &postgres.Config{}) | |
if err != nil { | |
println(fmt.Sprintf("%+v", errors.Wrap(err, "failed to create database driver"))) | |
return | |
} | |
m, err := migrate.NewWithDatabaseInstance( | |
fmt.Sprintf("file://%s", v.GetString("messages.migrations.location")), | |
"postgres", driver) | |
if err != nil { | |
println(fmt.Sprintf("%+v", errors.Wrap(err, "failed to create migrations"))) | |
return | |
} | |
if v.GetBool("messages.database.fresh") { | |
println("tearing down existing database because messages.database.fresh is true") | |
err = m.Drop() | |
if err != nil { | |
println(fmt.Sprintf("%+v", errors.Wrap(err, "failed to tear down database"))) | |
return | |
} | |
} | |
migratedFromVersion, _, err := m.Version() | |
println("performing migrations...") | |
fromTime := time.Now() | |
err = m.Up() | |
if err == migrate.ErrNoChange { | |
println("no migrations performed") | |
} else if err != nil { | |
println(fmt.Sprintf("%+v", errors.Wrap(err, "failed to migrate database"))) | |
return | |
} else { | |
migratedToVersion, _, _ := m.Version() | |
println(fmt.Sprintf("migrated database from v%v to v%v in %v seconds.", migratedFromVersion, migratedToVersion, time.Since(fromTime).Seconds())) | |
} | |
srcErr, dbErr := m.Close() | |
if srcErr != nil { | |
println(fmt.Sprintf("%+v", errors.Wrap(srcErr, "failed to disconnect from migration source"))) | |
} | |
if dbErr != nil { | |
println(fmt.Sprintf("%+v", errors.Wrap(srcErr, "failed to disconnect from database"))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment