]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - db/db.go
Fix migrations.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / db / db.go
index 7f87201a354abb1d7a64a0e1166972ea758fa83f..2adc8c870caa159b34211401122cd743e536a84e 100644 (file)
--- a/db/db.go
+++ b/db/db.go
@@ -4,21 +4,33 @@ import (
        migrate "github.com/go-pg/migrations"
        "github.com/go-pg/pg"
        "github.com/go-pg/pg/orm"
+       "github.com/go-redis/redis"
        "github.com/jloup/utils"
 )
 
-var DB *pg.DB
-
 var log = utils.StandardL().WithField("module", "db")
 
+var (
+       DB    *pg.DB
+       Redis *redis.Client
+)
+
 type DBConfig struct {
+       Type     string // tcp or unix
        Address  string
        Database string
        User     string
        Password string
 }
 
-func Init(config DBConfig) {
+type RedisConfig struct {
+       Type     string // tcp or unix
+       Address  string
+       Password string
+       Database int
+}
+
+func Init(config DBConfig, redisConfig RedisConfig) {
        var err error
 
        DB = connect(config)
@@ -27,27 +39,32 @@ func Init(config DBConfig) {
        if err != nil {
                log.Fatalf("cannot migratedb '%v'\n", err)
        }
-}
 
-func migratedb() error {
-       /* Remove after first MEP */
-       version, err := migrate.Version(DB)
+       Redis = redis.NewClient(&redis.Options{
+               Network:  redisConfig.Type,
+               Addr:     redisConfig.Address,
+               Password: redisConfig.Password,
+               DB:       redisConfig.Database,
+       })
+
+       _, err = Redis.Ping().Result()
+
        if err != nil {
-               return err
+               log.Fatalf("redis init error %s", err)
        }
 
-       if version == 0 {
-               return migrate.SetVersion(DB, 1)
-       }
-       /***/
+}
+
+func migratedb() error {
 
        mig := make([]migrate.Migration, 0)
 
-       for _, migration := range migrations {
+       for i := range migrations {
+               index := i
                mig = append(mig, migrate.Migration{
-                       Version: migration.Version,
+                       Version: migrations[index].Version,
                        Up: func(db orm.DB) error {
-                               for _, query := range migration.Up {
+                               for _, query := range migrations[index].Up {
                                        _, err := db.Exec(query)
                                        if err != nil {
                                                return err
@@ -57,7 +74,7 @@ func migratedb() error {
                                return nil
                        },
                        Down: func(db orm.DB) error {
-                               for _, query := range migration.Down {
+                               for _, query := range migrations[index].Down {
                                        _, err := db.Exec(query)
                                        if err != nil {
                                                return err
@@ -84,6 +101,7 @@ func connect(config DBConfig) *pg.DB {
                User:     config.User,
                Password: config.Password,
                Database: config.Database,
+               Network:  config.Type,
                Addr:     config.Address,
        })
 }