models.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "database/sql"
  7. "fmt"
  8. "net/url"
  9. "os"
  10. "path"
  11. "strings"
  12. _ "github.com/go-sql-driver/mysql"
  13. "github.com/go-xorm/core"
  14. "github.com/go-xorm/xorm"
  15. _ "github.com/lib/pq"
  16. "github.com/gogits/gogs/models/migrations"
  17. "github.com/gogits/gogs/modules/setting"
  18. )
  19. // Engine represents a xorm engine or session.
  20. type Engine interface {
  21. Delete(interface{}) (int64, error)
  22. Exec(string, ...interface{}) (sql.Result, error)
  23. Find(interface{}, ...interface{}) error
  24. Get(interface{}) (bool, error)
  25. Id(interface{}) *xorm.Session
  26. Insert(...interface{}) (int64, error)
  27. InsertOne(interface{}) (int64, error)
  28. Iterate(interface{}, xorm.IterFunc) error
  29. Sql(string, ...interface{}) *xorm.Session
  30. Where(string, ...interface{}) *xorm.Session
  31. }
  32. func sessionRelease(sess *xorm.Session) {
  33. if !sess.IsCommitedOrRollbacked {
  34. sess.Rollback()
  35. }
  36. sess.Close()
  37. }
  38. var (
  39. x *xorm.Engine
  40. tables []interface{}
  41. HasEngine bool
  42. DbCfg struct {
  43. Type, Host, Name, User, Passwd, Path, SSLMode string
  44. }
  45. EnableSQLite3 bool
  46. EnableTidb bool
  47. )
  48. func init() {
  49. tables = append(tables,
  50. new(User), new(PublicKey), new(AccessToken),
  51. new(Repository), new(DeployKey), new(Collaboration), new(Access),
  52. new(Watch), new(Star), new(Follow), new(Action),
  53. new(Issue), new(PullRequest), new(Comment), new(Attachment), new(IssueUser),
  54. new(Label), new(IssueLabel), new(Milestone),
  55. new(Mirror), new(Release), new(LoginSource), new(Webhook),
  56. new(UpdateTask), new(HookTask),
  57. new(Team), new(OrgUser), new(TeamUser), new(TeamRepo),
  58. new(Notice), new(EmailAddress))
  59. gonicNames := []string{"SSL"}
  60. for _, name := range gonicNames {
  61. core.LintGonicMapper[name] = true
  62. }
  63. }
  64. func LoadConfigs() {
  65. sec := setting.Cfg.Section("database")
  66. DbCfg.Type = sec.Key("DB_TYPE").String()
  67. switch DbCfg.Type {
  68. case "sqlite3":
  69. setting.UseSQLite3 = true
  70. case "mysql":
  71. setting.UseMySQL = true
  72. case "postgres":
  73. setting.UsePostgreSQL = true
  74. case "tidb":
  75. setting.UseTiDB = true
  76. }
  77. DbCfg.Host = sec.Key("HOST").String()
  78. DbCfg.Name = sec.Key("NAME").String()
  79. DbCfg.User = sec.Key("USER").String()
  80. if len(DbCfg.Passwd) == 0 {
  81. DbCfg.Passwd = sec.Key("PASSWD").String()
  82. }
  83. DbCfg.SSLMode = sec.Key("SSL_MODE").String()
  84. DbCfg.Path = sec.Key("PATH").MustString("data/gogs.db")
  85. }
  86. func getEngine() (*xorm.Engine, error) {
  87. cnnstr := ""
  88. var Param string = "?"
  89. if strings.Contains(DbCfg.Name, Param) {
  90. Param = "&"
  91. }
  92. switch DbCfg.Type {
  93. case "mysql":
  94. if DbCfg.Host[0] == '/' { // looks like a unix socket
  95. cnnstr = fmt.Sprintf("%s:%s@unix(%s)/%s%scharset=utf8&parseTime=true",
  96. DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
  97. } else {
  98. cnnstr = fmt.Sprintf("%s:%s@tcp(%s)/%s%scharset=utf8&parseTime=true",
  99. DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
  100. }
  101. case "postgres":
  102. var host, port = "127.0.0.1", "5432"
  103. fields := strings.Split(DbCfg.Host, ":")
  104. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  105. host = fields[0]
  106. }
  107. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  108. port = fields[1]
  109. }
  110. cnnstr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
  111. url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), host, port, DbCfg.Name, Param, DbCfg.SSLMode)
  112. case "sqlite3":
  113. if !EnableSQLite3 {
  114. return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  115. }
  116. if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
  117. return nil, fmt.Errorf("Fail to create directories: %v", err)
  118. }
  119. cnnstr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc"
  120. case "tidb":
  121. if !EnableTidb {
  122. return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  123. }
  124. if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
  125. return nil, fmt.Errorf("Fail to create directories: %v", err)
  126. }
  127. cnnstr = "goleveldb://" + DbCfg.Path
  128. default:
  129. return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  130. }
  131. return xorm.NewEngine(DbCfg.Type, cnnstr)
  132. }
  133. func NewTestEngine(x *xorm.Engine) (err error) {
  134. x, err = getEngine()
  135. if err != nil {
  136. return fmt.Errorf("Connect to database: %v", err)
  137. }
  138. x.SetMapper(core.GonicMapper{})
  139. return x.StoreEngine("InnoDB").Sync2(tables...)
  140. }
  141. func SetEngine() (err error) {
  142. x, err = getEngine()
  143. if err != nil {
  144. return fmt.Errorf("Fail to connect to database: %v", err)
  145. }
  146. x.SetMapper(core.GonicMapper{})
  147. // WARNING: for serv command, MUST remove the output to os.stdout,
  148. // so use log file to instead print to stdout.
  149. logPath := path.Join(setting.LogRootPath, "xorm.log")
  150. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  151. f, err := os.Create(logPath)
  152. if err != nil {
  153. return fmt.Errorf("Fail to create xorm.log: %v", err)
  154. }
  155. x.SetLogger(xorm.NewSimpleLogger(f))
  156. x.ShowSQL(true)
  157. return nil
  158. }
  159. func NewEngine() (err error) {
  160. if err = SetEngine(); err != nil {
  161. return err
  162. }
  163. if err = migrations.Migrate(x); err != nil {
  164. return fmt.Errorf("migrate: %v", err)
  165. }
  166. if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
  167. return fmt.Errorf("sync database struct error: %v\n", err)
  168. }
  169. return nil
  170. }
  171. type Statistic struct {
  172. Counter struct {
  173. User, Org, PublicKey,
  174. Repo, Watch, Star, Action, Access,
  175. Issue, Comment, Oauth, Follow,
  176. Mirror, Release, LoginSource, Webhook,
  177. Milestone, Label, HookTask,
  178. Team, UpdateTask, Attachment int64
  179. }
  180. }
  181. func GetStatistic() (stats Statistic) {
  182. stats.Counter.User = CountUsers()
  183. stats.Counter.Org = CountOrganizations()
  184. stats.Counter.PublicKey, _ = x.Count(new(PublicKey))
  185. stats.Counter.Repo = CountRepositories(true)
  186. stats.Counter.Watch, _ = x.Count(new(Watch))
  187. stats.Counter.Star, _ = x.Count(new(Star))
  188. stats.Counter.Action, _ = x.Count(new(Action))
  189. stats.Counter.Access, _ = x.Count(new(Access))
  190. stats.Counter.Issue, _ = x.Count(new(Issue))
  191. stats.Counter.Comment, _ = x.Count(new(Comment))
  192. stats.Counter.Oauth = 0
  193. stats.Counter.Follow, _ = x.Count(new(Follow))
  194. stats.Counter.Mirror, _ = x.Count(new(Mirror))
  195. stats.Counter.Release, _ = x.Count(new(Release))
  196. stats.Counter.LoginSource = CountLoginSources()
  197. stats.Counter.Webhook, _ = x.Count(new(Webhook))
  198. stats.Counter.Milestone, _ = x.Count(new(Milestone))
  199. stats.Counter.Label, _ = x.Count(new(Label))
  200. stats.Counter.HookTask, _ = x.Count(new(HookTask))
  201. stats.Counter.Team, _ = x.Count(new(Team))
  202. stats.Counter.UpdateTask, _ = x.Count(new(UpdateTask))
  203. stats.Counter.Attachment, _ = x.Count(new(Attachment))
  204. return
  205. }
  206. func Ping() error {
  207. return x.Ping()
  208. }
  209. // DumpDatabase dumps all data from database to file system.
  210. func DumpDatabase(filePath string) error {
  211. return x.DumpAllToFile(filePath)
  212. }