models.go 1.7 KB

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