func migrateAddColumn(db *sql.DB, table, column, typ, defaul, comment string) error { log.I("begin to add column[%s] to the table[%s]...", column, table) exists, err := tableColumnExist(db, table, column) if err != nil { log.E("get table column exists fail %v", err.Error()) return err } if exists == false { _, err := db.Exec(fmt.Sprintf(`ALTER TABLE %s ADD %s %s default %s comment '%s';`, table, column, typ, defaul, comment)) if err != nil { log.E("add column[%s] fail %v", column, err.Error()) return err } log.I("have added column[%s] to the table[%s]", column, table) //set the old data of this field to default value _, err = db.Exec(fmt.Sprintf(`update %s set %s = ?`, table, column), "") return err } else { log.I("column[%s] already exists no need to add", column) } return nil }
func migrateAddTable(db *sql.DB, table, def string) error { log.I("begin to add table %s", table) exist, err := tableExist(db, table) if err != nil { return err } if exist { log.I("table %s already exist, no need to add", table) return nil } if _, err := db.Exec(def); err != nil { return err } log.I("add table %s success", table) return nil }
func Init(driver string, db string) error { if len(db) < 1 { return errors.New("database connection string can't be empty") } log.I("initial database by %s", db) t_dbMgr.Init(driver, db, "ebs") t_dbMgr.SetMaxOpenConn("ebs", MaxOpenConns) return CheckDb(DbConn()) // Release() // var err error = nil // csm_db, err = sql.Open(driver, db) // if err != nil { // return err // } // csm_db.SetMaxOpenConns(MaxOpenConns) // return CheckDb(csm_db) }
func run(args []string) { defer StopSrv() cfile := "conf/ebs.properties" if len(args) > 1 { cfile = args[1] } fmt.Println("Using config file:", cfile) err := conf.Cfg.InitWithFilePath(cfile) if err != nil { fmt.Println(err.Error()) return } uap_conf.Cfg = conf.Cfg //init js conf conf.JsConf() log.I("Config:\n%v", conf.Cfg.Show()) //test connect if len(conf.DbConn()) < 1 { fmt.Println("DB_CONN is not exist in config") return } // uri := "mongodb://*****:*****@192.168.2.30:27017/fs" // dbName := "fs" // err = trak.DbInit(conf.TRACE_DB_CONN(), conf.TRACE_DB()) // if err != nil { // panic(fmt.Sprintf("connect mongodb err %v", err.Error())) // } dbMgr.Init("mysql", conf.DbConn()) uap.InitDb(dbMgr.DbConn) usr.CheckUcs(dbMgr.DbConn()) if err := nets.RedirectLog(conf.LOG_ADDR(), conf.LOG_PORT(), conf.LOG_KEY(), true); err != nil { fmt.Println("remote log error:", err) } sinfo := `{"description": "ebs接口文档", "version": "1.0.0", "title": "ebs接口文档" }` infoParse := map[string]interface{}{} json.Unmarshal([]byte(sinfo), &infoParse) go_swagger.NewH().InitSwagger(go_swagger.Swagger{ SwaggerVersion: "2.0", Info: infoParse, Host: "ebs2.dev.jxzy.com", BasePath: "", Schemes: []string{"http"}, }).AddTag(go_swagger.Tag{Name: "answer", Description: "作答相关接口"}) // // mux := http.NewServeMux() mux.Handle("/", NewSrvMux(mux, "", "www")) log.D("running server on %s", conf.ListenAddr()) s := http.Server{Addr: conf.ListenAddr(), Handler: mux} err = s.ListenAndServe() if err != nil { fmt.Println(err) } }