]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - db/db.go
Do not return internal error when SMS send fails.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / db / db.go
index bc4b8b3f4e5a16d345849b3972e563b7e629f5ba..2adc8c870caa159b34211401122cd743e536a84e 100644 (file)
--- a/db/db.go
+++ b/db/db.go
@@ -1,39 +1,99 @@
 package db
 
 import (
-       "fmt"
-       "strings"
-
+       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)
 
-       err = createSchema(DB)
+       err = migratedb()
        if err != nil {
-               log.Errorf("cannot create schemas %v\n", err)
+               log.Fatalf("cannot migratedb '%v'\n", err)
        }
 
-       err = createIndexes(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 {
-               log.Errorf("cannot create indexes %v\n", err)
+               log.Fatalf("redis init error %s", err)
        }
+
+}
+
+func migratedb() error {
+
+       mig := make([]migrate.Migration, 0)
+
+       for i := range migrations {
+               index := i
+               mig = append(mig, migrate.Migration{
+                       Version: migrations[index].Version,
+                       Up: func(db orm.DB) error {
+                               for _, query := range migrations[index].Up {
+                                       _, err := db.Exec(query)
+                                       if err != nil {
+                                               return err
+                                       }
+                               }
+
+                               return nil
+                       },
+                       Down: func(db orm.DB) error {
+                               for _, query := range migrations[index].Down {
+                                       _, err := db.Exec(query)
+                                       if err != nil {
+                                               return err
+                                       }
+                               }
+
+                               return nil
+                       },
+               })
+       }
+
+       oldVersion, newVersion, err := migrate.RunMigrations(DB, mig, "up")
+
+       if oldVersion != newVersion {
+               log.Infof("Migrate DB: %v -> %v", oldVersion, newVersion)
+       } else {
+               log.Infof("DB up-to-date: version '%v'", newVersion)
+       }
+       return err
 }
 
 func connect(config DBConfig) *pg.DB {
@@ -41,35 +101,7 @@ func connect(config DBConfig) *pg.DB {
                User:     config.User,
                Password: config.Password,
                Database: config.Database,
+               Network:  config.Type,
                Addr:     config.Address,
        })
 }
-
-func createSchema(db *pg.DB) error {
-       for _, model := range []interface{}{&User{}, &MarketConfig{}} {
-               err := db.CreateTable(model, &orm.CreateTableOptions{IfNotExists: true})
-               if err != nil {
-                       return err
-               }
-       }
-       return nil
-}
-
-func createIndexes(db *pg.DB) error {
-       indexes := []struct {
-               TableName string
-               Name      string
-               Columns   []string
-       }{
-               {"market_configs", "market_name_user_id_idx", []string{"user_id", "market_name"}},
-       }
-
-       for _, index := range indexes {
-               _, err := db.Exec(fmt.Sprintf("CREATE UNIQUE INDEX IF NOT EXISTS %s ON %s (%s)", index.Name, index.TableName, strings.Join(index.Columns, ",")))
-               if err != nil {
-                       return err
-               }
-       }
-
-       return nil
-}