You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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(Follow), new(Oauth2),
  27. new(Repository), new(Watch), new(Star), new(Action), new(Access),
  28. new(Issue), new(Comment), new(Attachment), new(IssueUser), new(Label), new(Milestone),
  29. new(Mirror), new(Release), new(LoginSource), new(Webhook),
  30. new(UpdateTask), new(HookTask), new(Team), new(OrgUser), new(TeamUser),
  31. new(Notice))
  32. }
  33. func LoadModelsConfig() {
  34. DbCfg.Type = setting.Cfg.MustValue("database", "DB_TYPE")
  35. if DbCfg.Type == "sqlite3" {
  36. UseSQLite3 = true
  37. }
  38. DbCfg.Host = setting.Cfg.MustValue("database", "HOST")
  39. DbCfg.Name = setting.Cfg.MustValue("database", "NAME")
  40. DbCfg.User = setting.Cfg.MustValue("database", "USER")
  41. if len(DbCfg.Pwd) == 0 {
  42. DbCfg.Pwd = setting.Cfg.MustValue("database", "PASSWD")
  43. }
  44. DbCfg.SslMode = setting.Cfg.MustValue("database", "SSL_MODE")
  45. DbCfg.Path = setting.Cfg.MustValue("database", "PATH", "data/gogs.db")
  46. }
  47. func getEngine() (*xorm.Engine, error) {
  48. cnnstr := ""
  49. switch DbCfg.Type {
  50. case "mysql":
  51. cnnstr = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  52. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name)
  53. case "postgres":
  54. var host, port = "127.0.0.1", "5432"
  55. fields := strings.Split(DbCfg.Host, ":")
  56. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  57. host = fields[0]
  58. }
  59. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  60. port = fields[1]
  61. }
  62. cnnstr = fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
  63. DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
  64. case "sqlite3":
  65. if !EnableSQLite3 {
  66. return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  67. }
  68. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  69. cnnstr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc"
  70. default:
  71. return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  72. }
  73. return xorm.NewEngine(DbCfg.Type, cnnstr)
  74. }
  75. func NewTestEngine(x *xorm.Engine) (err error) {
  76. x, err = getEngine()
  77. if err != nil {
  78. return fmt.Errorf("models.init(fail to connect to database): %v", err)
  79. }
  80. return x.Sync(tables...)
  81. }
  82. func SetEngine() (err error) {
  83. x, err = getEngine()
  84. if err != nil {
  85. return fmt.Errorf("models.init(fail to connect to database): %v", err)
  86. }
  87. // WARNNING: for serv command, MUST remove the output to os.stdout,
  88. // so use log file to instead print to stdout.
  89. logPath := path.Join(setting.LogRootPath, "xorm.log")
  90. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  91. f, err := os.Create(logPath)
  92. if err != nil {
  93. return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
  94. }
  95. x.Logger = xorm.NewSimpleLogger(f)
  96. x.ShowSQL = true
  97. x.ShowInfo = true
  98. x.ShowDebug = true
  99. x.ShowErr = true
  100. x.ShowWarn = true
  101. return nil
  102. }
  103. func NewEngine() (err error) {
  104. if err = SetEngine(); err != nil {
  105. return err
  106. }
  107. if err = x.Sync2(tables...); err != nil {
  108. return fmt.Errorf("sync database struct error: %v\n", err)
  109. }
  110. return nil
  111. }
  112. type Statistic struct {
  113. Counter struct {
  114. User, Org, PublicKey,
  115. Repo, Watch, Star, Action, Access,
  116. Issue, Comment, Oauth, Follow,
  117. Mirror, Release, LoginSource, Webhook,
  118. Milestone, Label, HookTask,
  119. Team, UpdateTask, Attachment int64
  120. }
  121. }
  122. func GetStatistic() (stats Statistic) {
  123. stats.Counter.User = CountUsers()
  124. stats.Counter.Org = CountOrganizations()
  125. stats.Counter.PublicKey, _ = x.Count(new(PublicKey))
  126. stats.Counter.Repo = CountRepositories()
  127. stats.Counter.Watch, _ = x.Count(new(Watch))
  128. stats.Counter.Star, _ = x.Count(new(Star))
  129. stats.Counter.Action, _ = x.Count(new(Action))
  130. stats.Counter.Access, _ = x.Count(new(Access))
  131. stats.Counter.Issue, _ = x.Count(new(Issue))
  132. stats.Counter.Comment, _ = x.Count(new(Comment))
  133. stats.Counter.Oauth, _ = x.Count(new(Oauth2))
  134. stats.Counter.Follow, _ = x.Count(new(Follow))
  135. stats.Counter.Mirror, _ = x.Count(new(Mirror))
  136. stats.Counter.Release, _ = x.Count(new(Release))
  137. stats.Counter.LoginSource, _ = x.Count(new(LoginSource))
  138. stats.Counter.Webhook, _ = x.Count(new(Webhook))
  139. stats.Counter.Milestone, _ = x.Count(new(Milestone))
  140. stats.Counter.Label, _ = x.Count(new(Label))
  141. stats.Counter.HookTask, _ = x.Count(new(HookTask))
  142. stats.Counter.Team, _ = x.Count(new(Team))
  143. stats.Counter.UpdateTask, _ = x.Count(new(UpdateTask))
  144. stats.Counter.Attachment, _ = x.Count(new(Attachment))
  145. return
  146. }
  147. func Ping() error {
  148. return x.Ping()
  149. }
  150. // DumpDatabase dumps all data from database to file system.
  151. func DumpDatabase(filePath string) error {
  152. return x.DumpAllToFile(filePath)
  153. }