-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorm.go
146 lines (134 loc) · 3.75 KB
/
gorm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package controllers
import (
"database/sql"
"github.com/gitDashboard/gitDashboard/app/models"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
_ "github.com/lib/pq"
"github.com/revel/revel"
"golang.org/x/crypto/bcrypt"
"os"
)
var Db *gorm.DB
type GormController struct {
BasicController
Tx *gorm.DB
}
func getDbReferences() (string, string) {
var dbType, dbConnection string
dbType, dbTypeFounded := revel.Config.String("db.type")
dbConnection, dbConnectionFounded := revel.Config.String("db.connection")
if !dbTypeFounded || !dbConnectionFounded {
revel.WARN.Println("no settings found for database connection on revel configuration, searching on system environment (GITDASHBOARD_DBTYPE,GITDASHBOARD_DBCONNECTION)")
//search on environment
dbType = os.Getenv("GITDASHBOARD_DBTYPE")
dbConnection = os.Getenv("GITDASHBOARD_DBCONNECTION")
}
if dbType == "" || dbConnection == "" {
revel.ERROR.Println("No Database connection found")
panic("No database connection found")
}
return dbType, dbConnection
}
func InitDB() {
var err error
dbtype, dbConnection := getDbReferences()
Db, err = gorm.Open(dbtype, dbConnection)
if err != nil {
revel.ERROR.Println("FATAL", err)
panic(err)
}
Db.LogMode(true)
Db.DB().Ping()
Db.DB().SetMaxIdleConns(10)
Db.DB().SetMaxOpenConns(100)
perm := &models.Permission{}
user := &models.User{}
repo := &models.Repo{}
folder := &models.Folder{}
event := &models.Event{}
if !Db.HasTable(folder) {
revel.INFO.Println("Creating folders table")
Db.CreateTable(folder)
} else {
Db.AutoMigrate(folder)
}
if !Db.HasTable(repo) {
revel.INFO.Println("Creating repos table")
Db.CreateTable(repo)
} else {
Db.AutoMigrate(repo)
}
if !Db.HasTable(perm) {
revel.INFO.Println("Creating permissions table")
Db.CreateTable(perm)
/*Db.Model(perm).AddForeignKey("repo_id", "repos(id)", "CASCADE", "RESTRICT")
Db.Model(perm).AddForeignKey("user_id", "users(id)", "CASCADE", "RESTRICT")
Db.Model(perm).AddForeignKey("group_id", "groups(id)", "CASCADE", "RESTRICT")*/
} else {
Db.AutoMigrate(perm)
}
if !Db.HasTable(user) {
revel.INFO.Println("Creating users table")
Db.CreateTable(user)
Db.Table("users_permissions").AddForeignKey("permission_id", "permissions(id)", "CASCADE", "RESTRICT")
Db.Table("users_permissions").AddForeignKey("user_id", "users(id)", "CASCADE", "RESTRICT")
var adminPwdFld sql.NullString
adminPwd, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
adminPwdFld.String = string(adminPwd)
adminPwdFld.Valid = true
adminUser := &models.User{Username: "admin", Type: "internal", Password: adminPwdFld, Name: "Admin user", Admin: true}
Db.Create(adminUser)
} else {
Db.AutoMigrate(user)
}
if !Db.HasTable(event) {
revel.INFO.Println("Creating events table")
Db.CreateTable(event)
} else {
Db.AutoMigrate(event)
}
}
func (c *GormController) NewTransaction() *gorm.DB {
txn := Db.Begin()
if txn.Error != nil {
panic(txn.Error)
}
revel.INFO.Println("txn init", txn)
return txn
}
func (c *GormController) RollbackTransaction(tx *gorm.DB) {
if tx == nil {
return
}
tx.Rollback()
if err := tx.Error; err != nil && err != sql.ErrTxDone {
panic(err)
}
revel.INFO.Println("tx rollbacked", tx)
}
func (c *GormController) CommitTransaction(tx *gorm.DB) {
if tx == nil {
return
}
tx.Commit()
if err := tx.Error; err != nil && err != sql.ErrTxDone {
panic(err)
}
revel.INFO.Println("tx commited", tx)
}
func (c *GormController) Begin() revel.Result {
c.Tx = c.NewTransaction()
return nil
}
func (c *GormController) Commit() revel.Result {
c.CommitTransaction(c.Tx)
c.Tx = nil
return nil
}
func (c *GormController) Rollback() revel.Result {
c.RollbackTransaction(c.Tx)
c.Tx = nil
return nil
}