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.

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