/models/gorm.go

https://github.com/NyaaPantsu/nyaa · Go · 114 lines · 85 code · 16 blank · 13 comment · 26 complexity · 8bf8782cd6e012a43b107102a48cfedd MD5 · raw file

  1. package models
  2. import (
  3. "github.com/NyaaPantsu/nyaa/config"
  4. "github.com/NyaaPantsu/nyaa/utils/log"
  5. "github.com/jinzhu/gorm"
  6. _ "github.com/jinzhu/gorm/dialects/postgres" // Need for postgres support
  7. _ "github.com/jinzhu/gorm/dialects/sqlite" // Need for sqlite
  8. elastic "gopkg.in/olivere/elastic.v5"
  9. )
  10. const (
  11. // SqliteType : name of the sqlite type in gorm
  12. SqliteType = "sqlite3"
  13. )
  14. // Logger interface
  15. type Logger interface {
  16. Print(v ...interface{})
  17. }
  18. // DefaultLogger : use the default gorm logger that prints to stdout
  19. var DefaultLogger Logger
  20. // ORM : Variable for interacting with database
  21. var ORM *gorm.DB
  22. // ElasticSearchClient : Client for Elastic search
  23. var ElasticSearchClient *elastic.Client
  24. // IsSqlite : Variable to know if we are in sqlite or postgres
  25. var IsSqlite bool
  26. // ElasticSearchInit : Initialization of ES client
  27. func ElasticSearchInit() (*elastic.Client, error) {
  28. client, err := elastic.NewClient()
  29. if err != nil {
  30. log.Errorf("Unable to create elasticsearch client: %s", err)
  31. return nil, err
  32. }
  33. log.Infof("Using elasticsearch client")
  34. return client, nil
  35. }
  36. // GormInit init gorm ORM.
  37. func GormInit(logger Logger) (*gorm.DB, error) {
  38. conf := config.Get()
  39. db, openErr := gorm.Open(conf.DBType, conf.DBParams)
  40. if openErr != nil {
  41. log.CheckError(openErr)
  42. return nil, openErr
  43. }
  44. IsSqlite = conf.DBType == SqliteType
  45. connectionErr := db.DB().Ping()
  46. if connectionErr != nil {
  47. log.CheckError(connectionErr)
  48. return nil, connectionErr
  49. }
  50. db.DB().SetMaxIdleConns(1)
  51. // This should be about the number of cores the machine has (and should
  52. // be lower than the max_connection specified by postgresql.conf)
  53. // Since we have two applications running, this should really be
  54. // number of cores / 2
  55. // TODO Make configurable
  56. db.DB().SetMaxOpenConns(4)
  57. if config.Get().Environment == "DEVELOPMENT" {
  58. db.LogMode(true)
  59. }
  60. switch conf.DBLogMode {
  61. case "detailed":
  62. db.LogMode(true)
  63. case "silent":
  64. db.LogMode(false)
  65. }
  66. if logger != nil {
  67. db.SetLogger(logger)
  68. }
  69. db.AutoMigrate(&User{}, &UserFollows{}, &UserUploadsOld{}, &Notification{}, &Activity{})
  70. if db.Error != nil {
  71. return db, db.Error
  72. }
  73. db.AutoMigrate(&Torrent{}, &TorrentReport{}, &Scrape{})
  74. if db.Error != nil {
  75. return db, db.Error
  76. }
  77. db.AutoMigrate(&File{})
  78. if db.Error != nil {
  79. return db, db.Error
  80. }
  81. db.AutoMigrate(&Comment{}, &OldComment{})
  82. if db.Error != nil {
  83. return db, db.Error
  84. }
  85. db.AutoMigrate(&OpenID{}, &Access{}, &Refresh{}, &Code{})
  86. if db.Error != nil {
  87. return db, db.Error
  88. }
  89. db.AutoMigrate(&OauthClient{})
  90. if db.Error != nil {
  91. return db, db.Error
  92. }
  93. db.AutoMigrate(&Tag{})
  94. if db.Error != nil {
  95. return db, db.Error
  96. }
  97. return db, nil
  98. }