SHARE
TWEET

Untitled

a guest Dec 2nd, 2019 141 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package db
  2.  
  3. import (
  4.     "fmt"
  5.  
  6.     "isi.nc/inm/dpc/pkg/env"
  7.     "isi.nc/inm/dpc/pkg/logger"
  8.  
  9.     "github.com/jinzhu/gorm"
  10.     //need side-effects
  11.     _ "github.com/jinzhu/gorm/dialects/postgres"
  12. )
  13.  
  14. const (
  15.     hostOverride     = "OVERRIDE_SQL_HOST"
  16.     portOverride     = "OVERRIDE_SQL_PORT"
  17.     userOverride     = "OVERRIDE_SQL_USER"
  18.     dbNameOverride   = "OVERRIDE_SQL_DB_NAME"
  19.     passwordOverride = "OVERRIDE_SQL_PASSWORD"
  20.     configOverride   = "OVERRIDE_SQL_CONFIG"
  21.     logModeOverride  = "OVERRIDE_SQL_LOG_MODE"
  22.  
  23.     defaultHost     = "dpcv2-postgresql"
  24.     defaultPort     = "5432"
  25.     defaultUser     = "postgres"
  26.     defaultDbName   = "inm"
  27.     defaultPassword = "postgres"
  28.     defaultConfig   = "sslmode=disable"
  29.     defaultLogMode  = "false"
  30. )
  31.  
  32. type PostgresDB struct {
  33.     db *gorm.DB
  34. }
  35.  
  36. type psqlLogger struct {
  37. }
  38.  
  39. func (p psqlLogger) Print(v ...interface{}) {
  40.     logger.Debug.Print(v...)
  41. }
  42.  
  43. func NewPostgresDB(optionalDbName *string) (*PostgresDB, error) {
  44.     var dbName string
  45.     if optionalDbName != nil {
  46.         dbName = *optionalDbName
  47.     } else {
  48.         dbName = env.GetOrDefault(dbNameOverride, defaultDbName)
  49.     }
  50.     dsn := fmt.Sprintf("user=%v password=%v host=%v port=%v dbname=%v %v",
  51.         env.GetOrDefault(userOverride, defaultUser),
  52.         env.GetOrDefault(passwordOverride, defaultPassword),
  53.         env.GetOrDefault(hostOverride, defaultHost),
  54.         env.GetOrDefault(portOverride, defaultPort),
  55.         dbName,
  56.         env.GetOrDefault(configOverride, defaultConfig))
  57.  
  58.     _db, err := gorm.Open("postgres", dsn)
  59.     if err != nil {
  60.         logger.Error.Printf("Impossible to connect w/ BDD: %s", err.Error())
  61.         return nil, err
  62.     }
  63.  
  64.     if env.GetOrDefault(logModeOverride, defaultLogMode) == "true" {
  65.         _db.LogMode(true)
  66.     } else {
  67.         _db.SetLogger(psqlLogger{})
  68.     }
  69.  
  70.     if err := _db.Exec("CREATE EXTENSION IF NOT EXISTS unaccent").Error; err != nil {
  71.         logger.Error.Printf("Impossible to create extension unaccent: %s", err.Error())
  72.         return nil, err
  73.     }
  74.     if err := _db.Exec("CREATE EXTENSION IF NOT EXISTS fuzzystrmatch").Error; err != nil {
  75.         logger.Error.Printf("Impossible to create extension fuzzystrmatch: %s", err.Error())
  76.         return nil, err
  77.     }
  78.  
  79.     return &PostgresDB{
  80.         db: _db.Set("gorm:auto_preload", true),
  81.     }, nil
  82. }
  83.  
  84. func (p *PostgresDB) DB() *gorm.DB {
  85.     return p.db
  86. }
  87.  
  88. func (p *PostgresDB) PrefixTables(prefix string) {
  89.     gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
  90.         return prefix + "_" + defaultTableName
  91.     }
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top