generated from d3ta-go/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interface.layer: add rdbms migration cmd apps (#1)
- Loading branch information
Showing
3 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |