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)
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
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
User: config.User,
Password: config.Password,
Database: config.Database,
+ Network: config.Type,
Addr: config.Address,
})
}