From a6e12aaef6344fff43745baa755c6afa11935550 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 30 Mar 2014 10:47:08 -0400 Subject: [PATCH] Fixing bug --- models/models.go | 25 ++++++++++++++++++++++++- modules/auth/user.go | 4 ++++ routers/install.go | 20 +++++++++++++++----- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/models/models.go b/models/models.go index 825730c5a8..06533e45f9 100644 --- a/models/models.go +++ b/models/models.go @@ -17,7 +17,8 @@ import ( ) var ( - orm *xorm.Engine + orm *xorm.Engine + HasEngine bool DbCfg struct { Type, Host, Name, User, Pwd, Path, SslMode string @@ -34,6 +35,28 @@ func LoadModelsConfig() { DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db") } +func NewTestEngine(x *xorm.Engine) (err error) { + switch DbCfg.Type { + case "mysql": + x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", + DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name)) + case "postgres": + x, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s", + DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode)) + case "sqlite3": + os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm) + x, err = xorm.NewEngine("sqlite3", DbCfg.Path) + default: + return fmt.Errorf("Unknown database type: %s\n", DbCfg.Type) + } + if err != nil { + return fmt.Errorf("models.init(fail to conntect database): %v\n", err) + } + + return x.Sync(new(User), new(PublicKey), new(Repository), new(Watch), + new(Action), new(Access), new(Issue), new(Comment)) +} + func SetEngine() (err error) { switch DbCfg.Type { case "mysql": diff --git a/modules/auth/user.go b/modules/auth/user.go index cb8db1b29a..6242a11c18 100644 --- a/modules/auth/user.go +++ b/modules/auth/user.go @@ -21,6 +21,10 @@ import ( // SignedInId returns the id of signed in user. func SignedInId(session session.SessionStore) int64 { + if !models.HasEngine { + return 0 + } + userId := session.Get("userId") if userId == nil { return 0 diff --git a/routers/install.go b/routers/install.go index 36bba3d41a..20e4241954 100644 --- a/routers/install.go +++ b/routers/install.go @@ -11,6 +11,7 @@ import ( "github.com/Unknwon/goconfig" "github.com/codegangsta/martini" + // "github.com/lunny/xorm" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -38,9 +39,14 @@ func GlobalInit() { models.LoadModelsConfig() models.LoadRepoConfig() models.NewRepoContext() - if err := models.NewEngine(); err != nil && base.InstallLock { - log.Error("%v", err) - os.Exit(2) + + if base.InstallLock { + if err := models.NewEngine(); err != nil { + log.Error("%v", err) + os.Exit(2) + } + + models.HasEngine = true } base.NewServices() checkRunMode() @@ -107,7 +113,11 @@ func Install(ctx *middleware.Context, form auth.InstallForm) { models.DbCfg.SslMode = form.SslMode models.DbCfg.Path = form.DatabasePath - if err := models.NewEngine(); err != nil { + // ctx.RenderWithErr("Database setting is not correct: ", "install", &form) + // return + log.Trace("00000000000000000000000000000000000000000000") + var x *xorm.Engine + if err := models.NewTestEngine(x); err != nil { if strings.Contains(err.Error(), `unknown driver "sqlite3"`) { ctx.RenderWithErr("Your release version does not support SQLite3, please download the official binary version "+ "from https://github.com/gogits/gogs/wiki/Install-from-binary, NOT the gobuild version.", "install", &form) @@ -158,7 +168,7 @@ func Install(ctx *middleware.Context, form auth.InstallForm) { base.Cfg.SetValue("security", "INSTALL_LOCK", "true") - if err := goconfig.SaveConfigFile(base.Cfg, "custom/conf/app.ini"); err != nil { + if err := goconfig.SaveConfigFile(base.Cfg, "custom/conf/app1.ini"); err != nil { ctx.RenderWithErr("Fail to save configuration: "+err.Error(), "install", &form) return }