diff --git a/go.mod b/go.mod index a538d57..24a3b32 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/d3ta-go/ms-geolocation-restapi go 1.15 require ( - github.com/d3ta-go/ddd-mod-geolocation v0.0.2 - github.com/d3ta-go/system v0.0.9 + github.com/d3ta-go/ddd-mod-geolocation v0.0.3 + github.com/d3ta-go/system v0.0.12 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/fsnotify/fsnotify v1.4.9 github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b diff --git a/go.sum b/go.sum index a2eb3c0..d4810bc 100644 --- a/go.sum +++ b/go.sum @@ -56,11 +56,15 @@ github.com/d3ta-go/ddd-mod-geolocation v0.0.1 h1:f2/XmLuTVCVlpv1RPRKB9bBFIbik0Ej github.com/d3ta-go/ddd-mod-geolocation v0.0.1/go.mod h1:x+JZYrdr3mfj8VSyGKzQoZjk9CriRHHj2fEkJ0Ke2Sg= github.com/d3ta-go/ddd-mod-geolocation v0.0.2 h1:ZmjwAAWTzr2he0Etyp0Va0wYh6scPOEYTbJvc2HBqnw= github.com/d3ta-go/ddd-mod-geolocation v0.0.2/go.mod h1:NlozGwqGUNcWCCcq/80iMSmPy9r9cFSlgdISoHKSFLg= +github.com/d3ta-go/ddd-mod-geolocation v0.0.3 h1:dOEOPGFH4S/oKHi0TTovDmq7F4LyHkhW2nL9EuEh+Uk= +github.com/d3ta-go/ddd-mod-geolocation v0.0.3/go.mod h1:aEny9WRNq3lkfRHa3rJwF0g3jeXSn0war4yNYA7WQJQ= github.com/d3ta-go/system v0.0.3/go.mod h1:3JSsQSZ6h0+DSsnxa/3Va+IpCvmNWsw/AfLNpNGoAHc= github.com/d3ta-go/system v0.0.7 h1:8b+1g5uSudXasbr4hnk/WIgdMsw7TM3LzAI1QZDC3z4= github.com/d3ta-go/system v0.0.7/go.mod h1:Ps2kaVLXfy4qexJds0+jZOzGIuF/rw4teFJmkFnDrGI= github.com/d3ta-go/system v0.0.9 h1:Ir64Z2EGql68a6kolsBfkQMPSsrxiNFmQYhnkxFq+PA= github.com/d3ta-go/system v0.0.9/go.mod h1:Ps2kaVLXfy4qexJds0+jZOzGIuF/rw4teFJmkFnDrGI= +github.com/d3ta-go/system v0.0.12 h1:LZrrZuyIyGpGsI19eJIxufeH3J5nmToOzXK3eIaJkuc= +github.com/d3ta-go/system v0.0.12/go.mod h1:Ps2kaVLXfy4qexJds0+jZOzGIuF/rw4teFJmkFnDrGI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/interface/cmd-apps/database/migrate.go b/interface/cmd-apps/database/migrate.go index 8c9114d..4899fbf 100644 --- a/interface/cmd-apps/database/migrate.go +++ b/interface/cmd-apps/database/migrate.go @@ -1,10 +1,82 @@ package database -import "fmt" +import ( + "fmt" + + "github.com/d3ta-go/ddd-mod-geolocation/modules/geolocation/infrastructure/migration" + "github.com/d3ta-go/system/system/config" + "github.com/d3ta-go/system/system/handler" + "github.com/d3ta-go/system/system/initialize" + "github.com/fsnotify/fsnotify" +) + +func initConfig(h *handler.Handler) (*config.Config, error) { + //init config + cfg, viper, err := config.NewConfig("./") + if err != nil { + panic(err) + } + h.SetDefaultConfig(cfg) + h.SetViper("config", viper) + + viper.OnConfigChange(func(e fsnotify.Event) { + // fmt.Println("config file changed:", e.Name) + c := new(config.Config) + if err := viper.Unmarshal(&c); err != nil { + fmt.Println(err) + } + + h.SetDefaultConfig(c) + initializeSystems(h) + }) + + return cfg, nil +} + +func initializeSystems(h *handler.Handler) error { + + // initialize database + if err := initialize.LoadAllDatabaseConnection(h); err != nil { + panic(err) + } + + // initialize cacher + if err := initialize.OpenAllCacheConnection(h); err != nil { + panic(err) + } + + return nil +} // RunDBMigrate run DB Migration func RunDBMigrate() error { // Add your custom code - fmt.Println("RunDBMigrate: not implemented yet!") + // init super handler + superHandler := new(handler.Handler) + + // init configuration + _, err := initConfig(superHandler) + if err != nil { + return err + } + + // initialize Systems + err = initializeSystems(superHandler) + if err != nil { + return err + } + defer initialize.CloseDBConnections(superHandler) + + mig, err := migration.NewRDBMSMigration(superHandler) + if err != nil { + return err + } + + if err := mig.Run(); err != nil { + if err := mig.RollBack(); err != nil { + return err + } + } + return nil }