-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitDB.go
54 lines (40 loc) · 1.17 KB
/
initDB.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
package main
import (
"database/sql"
"log"
"github.com/coopernurse/gorp"
_ "github.com/lib/pq"
)
func populateLocations(locationsMap *gorp.DbMap) {
tuftsResQuad := initLocation("Tufts Res Quad", 42.408565, -71.121765)
hodgkinsPark := initLocation("Hodgkins Park", 42.399566, -71.124595)
danehyPark := initLocation("Danehy Park", 42.388306, -71.137507)
ledermanPark := initLocation("Lederman Park", 42.363649, -71.071774)
err := locationsMap.Insert(tuftsResQuad, hodgkinsPark,
danehyPark, ledermanPark)
if err != nil {
log.Fatal(err)
}
}
func initDB() *gorp.DbMap {
db, err := sql.Open("postgres", "postgres://yourPostgresDatabaseLogin")
if err != nil {
log.Fatal(err)
}
dbMap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
locationsTable :=
dbMap.AddTableWithName(Location{}, "locations").SetKeys(true, "Id")
locationsTable.ColMap("Name").SetNotNull(true).SetUnique(true)
locationsTable.ColMap("Lat").SetNotNull(true)
locationsTable.ColMap("Lng").SetNotNull(true)
err = dbMap.DropTablesIfExists()
if err != nil {
log.Fatal(err)
}
err = dbMap.CreateTablesIfNotExists()
if err != nil {
log.Fatal(err)
}
populateLocations(dbMap)
return dbMap
}