Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

models.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. _ "github.com/go-sql-driver/mysql"
  9. _ "github.com/lib/pq"
  10. "github.com/lunny/xorm"
  11. "github.com/gogits/gogs/modules/base"
  12. )
  13. var (
  14. orm *xorm.Engine
  15. RepoRootPath string
  16. )
  17. type Members struct {
  18. Id int64
  19. OrgId int64 `xorm:"unique(s) index"`
  20. UserId int64 `xorm:"unique(s)"`
  21. }
  22. type Issue struct {
  23. Id int64
  24. RepoId int64 `xorm:"index"`
  25. PosterId int64
  26. }
  27. type PullRequest struct {
  28. Id int64
  29. }
  30. type Comment struct {
  31. Id int64
  32. }
  33. func setEngine() {
  34. dbType := base.Cfg.MustValue("database", "DB_TYPE")
  35. dbHost := base.Cfg.MustValue("database", "HOST")
  36. dbName := base.Cfg.MustValue("database", "NAME")
  37. dbUser := base.Cfg.MustValue("database", "USER")
  38. dbPwd := base.Cfg.MustValue("database", "PASSWD")
  39. sslMode := base.Cfg.MustValue("database", "SSL_MODE")
  40. var err error
  41. switch dbType {
  42. case "mysql":
  43. orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@%s/%s?charset=utf8",
  44. dbUser, dbPwd, dbHost, dbName))
  45. case "postgres":
  46. orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
  47. dbUser, dbPwd, dbName, sslMode))
  48. default:
  49. fmt.Printf("Unknown database type: %s\n", dbType)
  50. os.Exit(2)
  51. }
  52. if err != nil {
  53. fmt.Printf("models.init(fail to conntect database): %v\n", err)
  54. os.Exit(2)
  55. }
  56. // TODO: for serv command, MUST remove the output to os.stdout, so
  57. // use log file to instead print to stdout
  58. //x.ShowDebug = true
  59. //orm.ShowErr = true
  60. f, err := os.Create("xorm.log")
  61. if err != nil {
  62. fmt.Printf("models.init(fail to create xorm.log): %v\n", err)
  63. os.Exit(2)
  64. }
  65. orm.Logger = f
  66. orm.ShowSQL = true
  67. // Determine and create root git reposiroty path.
  68. RepoRootPath = base.Cfg.MustValue("repository", "ROOT")
  69. if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil {
  70. fmt.Printf("models.init(fail to create RepoRootPath(%s)): %v\n", RepoRootPath, err)
  71. os.Exit(2)
  72. }
  73. }
  74. func init() {
  75. setEngine()
  76. if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Access), new(Action)); err != nil {
  77. fmt.Printf("sync database struct error: %v\n", err)
  78. os.Exit(2)
  79. }
  80. }