db.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2020 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 db
  5. import (
  6. "fmt"
  7. "io"
  8. "net/url"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/jinzhu/gorm"
  13. _ "github.com/jinzhu/gorm/dialects/mssql"
  14. _ "github.com/jinzhu/gorm/dialects/mysql"
  15. _ "github.com/jinzhu/gorm/dialects/postgres"
  16. _ "github.com/jinzhu/gorm/dialects/sqlite"
  17. "github.com/pkg/errors"
  18. log "unknwon.dev/clog/v2"
  19. "gogs.io/gogs/internal/conf"
  20. "gogs.io/gogs/internal/dbutil"
  21. )
  22. // parsePostgreSQLHostPort parses given input in various forms defined in
  23. // https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
  24. // and returns proper host and port number.
  25. func parsePostgreSQLHostPort(info string) (string, string) {
  26. host, port := "127.0.0.1", "5432"
  27. if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") {
  28. idx := strings.LastIndex(info, ":")
  29. host = info[:idx]
  30. port = info[idx+1:]
  31. } else if len(info) > 0 {
  32. host = info
  33. }
  34. return host, port
  35. }
  36. func parseMSSQLHostPort(info string) (string, string) {
  37. host, port := "127.0.0.1", "1433"
  38. if strings.Contains(info, ":") {
  39. host = strings.Split(info, ":")[0]
  40. port = strings.Split(info, ":")[1]
  41. } else if strings.Contains(info, ",") {
  42. host = strings.Split(info, ",")[0]
  43. port = strings.TrimSpace(strings.Split(info, ",")[1])
  44. } else if len(info) > 0 {
  45. host = info
  46. }
  47. return host, port
  48. }
  49. // parseDSN takes given database options and returns parsed DSN.
  50. func parseDSN(opts conf.DatabaseOpts) (dsn string, err error) {
  51. // In case the database name contains "?" with some parameters
  52. concate := "?"
  53. if strings.Contains(opts.Name, concate) {
  54. concate = "&"
  55. }
  56. switch opts.Type {
  57. case "mysql":
  58. if opts.Host[0] == '/' { // Looks like a unix socket
  59. dsn = fmt.Sprintf("%s:%s@unix(%s)/%s%scharset=utf8mb4&parseTime=true",
  60. opts.User, opts.Password, opts.Host, opts.Name, concate)
  61. } else {
  62. dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s%scharset=utf8mb4&parseTime=true",
  63. opts.User, opts.Password, opts.Host, opts.Name, concate)
  64. }
  65. case "postgres":
  66. host, port := parsePostgreSQLHostPort(opts.Host)
  67. if host[0] == '/' { // looks like a unix socket
  68. dsn = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
  69. url.QueryEscape(opts.User), url.QueryEscape(opts.Password), port, opts.Name, concate, opts.SSLMode, host)
  70. } else {
  71. dsn = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
  72. url.QueryEscape(opts.User), url.QueryEscape(opts.Password), host, port, opts.Name, concate, opts.SSLMode)
  73. }
  74. case "mssql":
  75. host, port := parseMSSQLHostPort(opts.Host)
  76. dsn = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
  77. host, port, opts.Name, opts.User, opts.Password)
  78. case "sqlite3":
  79. dsn = "file:" + opts.Path + "?cache=shared&mode=rwc"
  80. default:
  81. return "", errors.Errorf("unrecognized dialect: %s", opts.Type)
  82. }
  83. return dsn, nil
  84. }
  85. func openDB(opts conf.DatabaseOpts) (*gorm.DB, error) {
  86. dsn, err := parseDSN(opts)
  87. if err != nil {
  88. return nil, errors.Wrap(err, "parse DSN")
  89. }
  90. return gorm.Open(opts.Type, dsn)
  91. }
  92. func getLogWriter() (io.Writer, error) {
  93. sec := conf.File.Section("log.gorm")
  94. w, err := log.NewFileWriter(
  95. filepath.Join(conf.Log.RootPath, "gorm.log"),
  96. log.FileRotationConfig{
  97. Rotate: sec.Key("ROTATE").MustBool(true),
  98. Daily: sec.Key("ROTATE_DAILY").MustBool(true),
  99. MaxSize: sec.Key("MAX_SIZE").MustInt64(100) * 1024 * 1024,
  100. MaxDays: sec.Key("MAX_DAYS").MustInt64(3),
  101. },
  102. )
  103. if err != nil {
  104. return nil, errors.Wrap(err, `create "gorm.log"`)
  105. }
  106. return w, nil
  107. }
  108. func Init() error {
  109. db, err := openDB(conf.Database)
  110. if err != nil {
  111. return errors.Wrap(err, "open database")
  112. }
  113. db.SingularTable(true)
  114. db.DB().SetMaxOpenConns(conf.Database.MaxOpenConns)
  115. db.DB().SetMaxIdleConns(conf.Database.MaxIdleConns)
  116. db.DB().SetConnMaxLifetime(time.Minute)
  117. w, err := getLogWriter()
  118. if err != nil {
  119. return errors.Wrap(err, "get log writer")
  120. }
  121. db.SetLogger(&dbutil.Writer{Writer: w})
  122. if !conf.IsProdMode() {
  123. db = db.LogMode(true)
  124. }
  125. switch conf.Database.Type {
  126. case "mysql":
  127. conf.UseMySQL = true
  128. db = db.Set("gorm:table_options", "ENGINE=InnoDB")
  129. case "postgres":
  130. conf.UsePostgreSQL = true
  131. case "mssql":
  132. conf.UseMSSQL = true
  133. case "sqlite3":
  134. conf.UseMySQL = true
  135. }
  136. err = db.AutoMigrate(new(LFSObject)).Error
  137. if err != nil {
  138. return errors.Wrap(err, "migrate schemes")
  139. }
  140. // Initialize stores, sorted in alphabetical order.
  141. AccessTokens = &accessTokens{DB: db}
  142. LoginSources = &loginSources{DB: db}
  143. LFS = &lfs{DB: db}
  144. Perms = &perms{DB: db}
  145. Repos = &repos{DB: db}
  146. TwoFactors = &twoFactors{DB: db}
  147. Users = &users{DB: db}
  148. return db.DB().Ping()
  149. }