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.

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