summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
author无闻 <joe2010xtmf@163.com>2014-09-04 11:38:35 -0400
committer无闻 <joe2010xtmf@163.com>2014-09-04 11:38:35 -0400
commitd8290c9bac5d991674a5d9cc6d56bf102a52245d (patch)
tree9a75589d99851f660cf1d37805b828001c3e34ca /models
parent8db8771be7d20e0e4620fad6d4b29232a80f484e (diff)
parentbdfdf3cacba75fe1c36f7a05096748c26f25a2f9 (diff)
downloadgitea-d8290c9bac5d991674a5d9cc6d56bf102a52245d.tar.gz
gitea-d8290c9bac5d991674a5d9cc6d56bf102a52245d.zip
Merge pull request #444 from tstorch/database-cleanup
Code dedoublication in models/models.go
Diffstat (limited to 'models')
-rw-r--r--models/models.go44
1 files changed, 15 insertions, 29 deletions
diff --git a/models/models.go b/models/models.go
index 4e2e08cf83..5558ec0062 100644
--- a/models/models.go
+++ b/models/models.go
@@ -55,11 +55,12 @@ func LoadModelsConfig() {
DbCfg.Path = setting.Cfg.MustValue("database", "PATH", "data/gogs.db")
}
-func NewTestEngine(x *xorm.Engine) (err error) {
+func getEngine() (*xorm.Engine, error) {
+ cnnstr := ""
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))
+ cnnstr = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
+ DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name)
case "postgres":
var host, port = "127.0.0.1", "5432"
fields := strings.Split(DbCfg.Host, ":")
@@ -69,46 +70,31 @@ func NewTestEngine(x *xorm.Engine) (err error) {
if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
port = fields[1]
}
- cnnstr := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
+ cnnstr = fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
- x, err = xorm.NewEngine("postgres", cnnstr)
case "sqlite3":
if !EnableSQLite3 {
- return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
+ return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
}
os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
- x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
+ cnnstr = DbCfg.Path
default:
- return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
+ return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
}
+ return xorm.NewEngine(DbCfg.Type, cnnstr)
+}
+
+func NewTestEngine(x *xorm.Engine) (err error) {
+ x, err = getEngine()
if err != nil {
return fmt.Errorf("models.init(fail to conntect database): %v", err)
}
+
return x.Sync(tables...)
}
func SetEngine() (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":
- var host, port = "127.0.0.1", "5432"
- fields := strings.Split(DbCfg.Host, ":")
- if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
- host = fields[0]
- }
- if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
- port = fields[1]
- }
- x, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
- DbCfg.User, DbCfg.Pwd, host, port, 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", DbCfg.Type)
- }
+ x, err = getEngine()
if err != nil {
return fmt.Errorf("models.init(fail to conntect database): %v", err)
}