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.

98 lines
2.0 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
  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. "os/user"
  9. _ "github.com/go-sql-driver/mysql"
  10. "github.com/lunny/xorm"
  11. "github.com/gogits/gogs/utils"
  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 := utils.Cfg.MustValue("database", "DB_TYPE")
  35. dbHost := utils.Cfg.MustValue("database", "HOST")
  36. dbName := utils.Cfg.MustValue("database", "NAME")
  37. dbUser := utils.Cfg.MustValue("database", "USER")
  38. dbPwd := utils.Cfg.MustValue("database", "PASSWD")
  39. uname, err := user.Current()
  40. if err != nil {
  41. fmt.Printf("models.init -> fail to get user: %s\n", err)
  42. os.Exit(2)
  43. }
  44. if uname.Username == "jiahuachen" {
  45. dbPwd = utils.Cfg.MustValue("database", "PASSWD_jiahua")
  46. }
  47. switch dbType {
  48. case "mysql":
  49. orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%v@%v/%v?charset=utf8",
  50. dbUser, dbPwd, dbHost, dbName))
  51. default:
  52. fmt.Printf("Unknown database type: %s\n", dbType)
  53. os.Exit(2)
  54. }
  55. if err != nil {
  56. fmt.Printf("models.init -> fail to conntect database: %s\n", dbType)
  57. os.Exit(2)
  58. }
  59. //TODO: for serv command, MUST remove the output to os.stdout, so
  60. // use log file to instead print to stdout
  61. //x.ShowDebug = true
  62. //orm.ShowErr = true
  63. f, _ := os.Create("xorm.log")
  64. orm.Logger = f
  65. orm.ShowSQL = true
  66. //log.Trace("Initialized database -> %s", dbName)
  67. RepoRootPath = utils.Cfg.MustValue("repository", "ROOT")
  68. if uname.Username == "jiahuachen" {
  69. RepoRootPath = utils.Cfg.MustValue("repository", "ROOT_jiahuachen")
  70. }
  71. }
  72. func init() {
  73. setEngine()
  74. err := orm.Sync(new(User), new(PublicKey), new(Repo), new(Access))
  75. if err != nil {
  76. fmt.Printf("sync database struct error: %s\n", err)
  77. os.Exit(2)
  78. }
  79. }