Implement postgres version of the tokenStore.
Create a postgres implementation for the tokenStore. Note that because pq
doesn't support Postgres' array types (see https://github.com/lib/pq/issues/49),
we couldn't just store the token.Scopes field as a Postgres array of varchars,
which would have been the ideal. Instead, we need a many-to-many table that maps
tokens to scopes. This meant we needed a special tokenScope type for our
database mapping, and we needed to complicate the token storage/retrieval
functions a little bit. It's kind of ugly, I'm not a huge fan of it, and I'd
much rather be using the Postgres array types, but... well, here we are.
We also added the postgres tokenStore to our slice of tokenStores to test when
the correct environment variables are present.
We wrote initialization SQL for the tables required by the postgres tokenStore.
Also, added a helper script for emptying the test database, because I got tired
of doing it by hand. We should be doing that in an automated fashion in the
tests themselves, but that would mean extending the *Store interfaces.
6 "code.secondbit.org/uuid.hg"
9 "github.com/secondbit/pan"
12 func (s Session) GetSQLTableName() string {
16 func (p *postgres) createSessionSQL(session Session) *pan.Query {
17 fields, values := pan.GetFields(session)
18 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(session))
19 query.Include("(" + pan.QueryList(fields) + ")")
20 query.Include("VALUES")
21 query.Include("("+pan.VariableList(len(values))+")", values...)
22 return query.FlushExpressions(" ")
25 func (p *postgres) createSession(session Session) error {
26 query := p.createSessionSQL(session)
27 _, err := p.db.Exec(query.String(), query.Args...)
28 if e, ok := err.(*pq.Error); ok && e.Constraint == "sessions_pkey" {
29 err = ErrSessionAlreadyExists
34 func (p *postgres) getSessionSQL(id string) *pan.Query {
36 fields, _ := pan.GetFields(session)
37 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(session))
39 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
40 return query.FlushExpressions(" ")
43 func (p *postgres) getSession(id string) (Session, error) {
44 query := p.getSessionSQL(id)
45 rows, err := p.db.Query(query.String(), query.Args...)
52 err := pan.Unmarshal(rows, &session)
58 if err = rows.Err(); err != nil {
62 return session, ErrSessionNotFound
67 func (p *postgres) removeSessionSQL(id string) *pan.Query {
69 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(session))
71 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
72 return query.FlushExpressions(" ")
75 func (p *postgres) removeSession(id string) error {
76 query := p.removeSessionSQL(id)
77 res, err := p.db.Exec(query.String(), query.Args...)
81 rows, err := res.RowsAffected()
86 return ErrSessionNotFound
91 func (p *postgres) listSessionsSQL(profile uuid.ID, before time.Time, num int64) *pan.Query {
93 fields, _ := pan.GetFields(session)
94 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(session))
96 query.Include(pan.GetUnquotedColumn(session, "ProfileID")+" = ?", profile)
98 query.Include(pan.GetUnquotedColumn(session, "Created")+" < ?", before)
100 query.FlushExpressions(" AND ")
102 query.IncludeLimit(num)
104 return query.FlushExpressions(" ")
107 func (p *postgres) listSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) {
108 query := p.listSessionsSQL(profile, before, num)
109 rows, err := p.db.Query(query.String(), query.Args...)
111 return []Session{}, err
113 var sessions []Session
116 err := pan.Unmarshal(rows, &session)
120 sessions = append(sessions, session)
122 if err = rows.Err(); err != nil {