1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package dao
- import (
- "fmt"
- _ "github.com/go-sql-driver/mysql"
- "github.com/jinzhu/gorm"
- "iot_manager_service/config"
- "net/url"
- )
- var GDb *gorm.DB
- func InitDB() {
- cfg := config.Instance()
- dsn := cfg.Database.User + ":" + cfg.Database.Password + "@tcp(" + cfg.Database.Host + ":" + cfg.Database.Port + ")/" + cfg.Database.Name + "?charset=utf8&parseTime=True" + "&loc=" + url.QueryEscape(cfg.Database.Timezone)
- db0, err := gorm.Open("mysql", dsn)
- if err != nil {
- panic(err)
- } else {
- GDb = db0
- GDb.DB().SetMaxOpenConns(32)
- GDb.DB().SetMaxIdleConns(5)
- GDb.LogMode(false)
- err = GDb.AutoMigrate(
- &User{},
- &Dict{},
- &LampPoleGroup{},
- &LampPole{},
- &Gateway{},
- &GatewayRelation{},
- &Transformer{},
- &LightControl{},
- &Garbage{},
- &GarbageWayGroup{},
- &OnDemandGroup{},
- &OnDemandSensor{},
- &Zigbee{},
- &CameraDevice{},
- &InfoBoard{},
- &Alarm{},
- &CaptureUint{},
- &CheckPoint{},
- &AlarmTerminal{},
- &Bridge{},
- &BridgeSensor{},
- &IpBroadcast{},
- &ManholeCover{},
- ).Error
- if err != nil {
- panic(fmt.Sprintf("AutoMigrate err : %v", err))
- }
- }
- }
|