-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializer.go
35 lines (26 loc) · 929 Bytes
/
initializer.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
package controllers
import (
"bitbucket.org/liamstask/goose/lib/goose"
"database/sql"
"flag"
"github.com/coopernurse/gorp"
"github.com/jpibarra1130/simple-api-go/models"
"log"
"os"
)
// global options. available to any subcommands. This was taken from goose library
var flagPath = flag.String("path", "db", "folder containing db info")
var flagEnv = flag.String("env", "development", "which DB environment to use")
func initDb() *gorp.DbMap {
conf, err := goose.NewDBConf(*flagPath, *flagEnv)
if err != nil {
log.Fatal(err)
return nil
}
log.Printf("Connecting to %v, %v", conf.Driver.Name, conf.Driver.OpenStr)
db, err := sql.Open(conf.Driver.Name, conf.Driver.OpenStr)
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
dbmap.TraceOn("[gorp]", log.New(os.Stdout, "myapp:", log.Lmicroseconds))
dbmap.AddTableWithName(models.User{}, "users").SetKeys(true, "Id")
return dbmap
}