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.

83 lines
1.7 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
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. _ "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. RepoRootPath = base.Cfg.MustValue("repository", "ROOT")
  59. }
  60. func init() {
  61. setEngine()
  62. err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Access))
  63. if err != nil {
  64. fmt.Printf("sync database struct error: %s\n", err)
  65. os.Exit(2)
  66. }
  67. }