forked from gaulliath/operative-framework
/
connection.go
45 lines (37 loc) · 847 Bytes
/
connection.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
package session
import (
"database/sql"
"github.com/jinzhu/gorm"
)
type Connection struct {
ConnectionInstance
ORM *gorm.DB `json:"-"`
Migrations map[string]interface{} `json:"-"`
}
type ConnectionInstance interface {
GetORM() *gorm.DB
GetDB() *sql.DB
Migrate() bool
}
func (c *Connection) GetORM() *gorm.DB {
return c.ORM
}
func (c *Connection) GetDB() *sql.DB {
return c.ORM.DB()
}
func (c *Connection) LoadMigration() {
c.Migrations["sessions"] = Session{}
c.Migrations["targets"] = Target{}
c.Migrations["target_links"] = Linking{}
c.Migrations["target_results"] = OpfResults{}
}
func (c *Connection) Migrate() bool {
c.LoadMigration()
for _, migration := range c.Migrations {
//if c.ORM.HasTable(migration){
// c.ORM.DropTable(migration)
//}
c.ORM.AutoMigrate(migration)
}
return true
}