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.

models.go 2.2KB

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