-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgstore.go
39 lines (34 loc) · 835 Bytes
/
pgstore.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
package db
import (
"database/sql"
"fmt"
_ "github.com/lib/pq" // The Postgres driver
"github.com/qustavo/dotsql"
)
// A PGStore implements storage against PostGres
type PGStore struct {
conn *sql.DB
}
// NewPGStore connects to a postgres database
func NewPGStore(u, p, d string) (*PGStore, error) {
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
u, p, d)
db, err := sql.Open("postgres", dbinfo)
if err != nil {
return nil, err
}
db.SetMaxIdleConns(5)
return &PGStore{conn: db}, nil
}
// ExecuteSchema runs all the sql commands int he given file to
// initialize the database
func (pg *PGStore) ExecuteSchema(filename string) error {
dot, err := dotsql.LoadFromFile(filename)
if err != nil {
return err
}
for query := range dot.QueryMap() {
dot.Exec(pg.conn, query)
}
return nil
}