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.

188 lines
5.5 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
  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. x.ShowWarn = true
  116. return nil
  117. }
  118. func NewEngine() (err error) {
  119. if err = SetEngine(); err != nil {
  120. return err
  121. }
  122. if err = x.Sync2(tables...); err != nil {
  123. return fmt.Errorf("sync database struct error: %v\n", err)
  124. }
  125. return nil
  126. }
  127. type Statistic struct {
  128. Counter struct {
  129. User, Org, PublicKey,
  130. Repo, Watch, Star, Action, Access,
  131. Issue, Comment, Oauth, Follow,
  132. Mirror, Release, LoginSource, Webhook,
  133. Milestone, Label, HookTask,
  134. Team, UpdateTask, Attachment int64
  135. }
  136. }
  137. func GetStatistic() (stats Statistic) {
  138. stats.Counter.User = CountUsers()
  139. stats.Counter.Org = CountOrganizations()
  140. stats.Counter.PublicKey, _ = x.Count(new(PublicKey))
  141. stats.Counter.Repo = CountRepositories()
  142. stats.Counter.Watch, _ = x.Count(new(Watch))
  143. stats.Counter.Star, _ = x.Count(new(Star))
  144. stats.Counter.Action, _ = x.Count(new(Action))
  145. stats.Counter.Access, _ = x.Count(new(Access))
  146. stats.Counter.Issue, _ = x.Count(new(Issue))
  147. stats.Counter.Comment, _ = x.Count(new(Comment))
  148. stats.Counter.Oauth, _ = x.Count(new(Oauth2))
  149. stats.Counter.Follow, _ = x.Count(new(Follow))
  150. stats.Counter.Mirror, _ = x.Count(new(Mirror))
  151. stats.Counter.Release, _ = x.Count(new(Release))
  152. stats.Counter.LoginSource, _ = x.Count(new(LoginSource))
  153. stats.Counter.Webhook, _ = x.Count(new(Webhook))
  154. stats.Counter.Milestone, _ = x.Count(new(Milestone))
  155. stats.Counter.Label, _ = x.Count(new(Label))
  156. stats.Counter.HookTask, _ = x.Count(new(HookTask))
  157. stats.Counter.Team, _ = x.Count(new(Team))
  158. stats.Counter.UpdateTask, _ = x.Count(new(UpdateTask))
  159. stats.Counter.Attachment, _ = x.Count(new(Attachment))
  160. return
  161. }
  162. func Ping() error {
  163. return x.Ping()
  164. }
  165. // DumpDatabase dumps all data from database to file system.
  166. func DumpDatabase(filePath string) error {
  167. return x.DumpAllToFile(filePath)
  168. }