models.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "os"
  8. "path"
  9. "strings"
  10. _ "github.com/go-sql-driver/mysql"
  11. "github.com/go-xorm/xorm"
  12. _ "github.com/lib/pq"
  13. "github.com/gogits/gogs/modules/setting"
  14. )
  15. var (
  16. x *xorm.Engine
  17. tables []interface{}
  18. HasEngine bool
  19. DbCfg struct {
  20. Type, Host, Name, User, Pwd, Path, SslMode string
  21. }
  22. EnableSQLite3 bool
  23. UseSQLite3 bool
  24. )
  25. func init() {
  26. tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch),
  27. new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow),
  28. new(Mirror), new(Release), new(LoginSource), new(Webhook), new(IssueUser),
  29. new(Milestone), new(Label), new(HookTask))
  30. }
  31. func LoadModelsConfig() {
  32. DbCfg.Type = setting.Cfg.MustValue("database", "DB_TYPE")
  33. if DbCfg.Type == "sqlite3" {
  34. UseSQLite3 = true
  35. }
  36. DbCfg.Host = setting.Cfg.MustValue("database", "HOST")
  37. DbCfg.Name = setting.Cfg.MustValue("database", "NAME")
  38. DbCfg.User = setting.Cfg.MustValue("database", "USER")
  39. if len(DbCfg.Pwd) == 0 {
  40. DbCfg.Pwd = setting.Cfg.MustValue("database", "PASSWD")
  41. }
  42. DbCfg.SslMode = setting.Cfg.MustValue("database", "SSL_MODE")
  43. DbCfg.Path = setting.Cfg.MustValue("database", "PATH", "data/gogs.db")
  44. }
  45. func NewTestEngine(x *xorm.Engine) (err error) {
  46. switch DbCfg.Type {
  47. case "mysql":
  48. x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  49. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  50. case "postgres":
  51. var host, port = "127.0.0.1", "5432"
  52. fields := strings.Split(DbCfg.Host, ":")
  53. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  54. host = fields[0]
  55. }
  56. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  57. port = fields[1]
  58. }
  59. cnnstr := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
  60. DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
  61. x, err = xorm.NewEngine("postgres", cnnstr)
  62. case "sqlite3":
  63. if !EnableSQLite3 {
  64. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  65. }
  66. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  67. x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  68. default:
  69. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  70. }
  71. if err != nil {
  72. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  73. }
  74. return x.Sync(tables...)
  75. }
  76. func SetEngine() (err error) {
  77. switch DbCfg.Type {
  78. case "mysql":
  79. x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  80. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  81. case "postgres":
  82. var host, port = "127.0.0.1", "5432"
  83. fields := strings.Split(DbCfg.Host, ":")
  84. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  85. host = fields[0]
  86. }
  87. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  88. port = fields[1]
  89. }
  90. x, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
  91. DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode))
  92. case "sqlite3":
  93. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  94. x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  95. default:
  96. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  97. }
  98. if err != nil {
  99. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  100. }
  101. // WARNNING: for serv command, MUST remove the output to os.stdout,
  102. // so use log file to instead print to stdout.
  103. logPath := path.Join(setting.LogRootPath, "xorm.log")
  104. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  105. f, err := os.Create(logPath)
  106. if err != nil {
  107. return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
  108. }
  109. x.Logger = xorm.NewSimpleLogger(f)
  110. x.ShowSQL = true
  111. x.ShowDebug = true
  112. x.ShowErr = true
  113. return nil
  114. }
  115. func NewEngine() (err error) {
  116. if err = SetEngine(); err != nil {
  117. return err
  118. }
  119. if err = x.Sync2(tables...); err != nil {
  120. return fmt.Errorf("sync database struct error: %v\n", err)
  121. }
  122. return nil
  123. }
  124. type Statistic struct {
  125. Counter struct {
  126. User, PublicKey, Repo, Watch, Action, Access,
  127. Issue, Comment, Mirror, Oauth, Release,
  128. LoginSource, Webhook, Milestone int64
  129. }
  130. }
  131. func GetStatistic() (stats Statistic) {
  132. stats.Counter.User, _ = x.Count(new(User))
  133. stats.Counter.PublicKey, _ = x.Count(new(PublicKey))
  134. stats.Counter.Repo, _ = x.Count(new(Repository))
  135. stats.Counter.Watch, _ = x.Count(new(Watch))
  136. stats.Counter.Action, _ = x.Count(new(Action))
  137. stats.Counter.Access, _ = x.Count(new(Access))
  138. stats.Counter.Issue, _ = x.Count(new(Issue))
  139. stats.Counter.Comment, _ = x.Count(new(Comment))
  140. stats.Counter.Mirror, _ = x.Count(new(Mirror))
  141. stats.Counter.Oauth, _ = x.Count(new(Oauth2))
  142. stats.Counter.Release, _ = x.Count(new(Release))
  143. stats.Counter.LoginSource, _ = x.Count(new(LoginSource))
  144. stats.Counter.Webhook, _ = x.Count(new(Webhook))
  145. stats.Counter.Milestone, _ = x.Count(new(Milestone))
  146. return
  147. }
  148. // DumpDatabase dumps all data from database to file system.
  149. func DumpDatabase(filePath string) error {
  150. return x.DumpAllToFile(filePath)
  151. }