Implement postgres version of scopeStore.
Update the authd server to use postgres as its scopeStore, instead of memstore.
panic when starting the authd server if the CreateScopes call fails. This
should, ideally, ignore ErrScopeAlreadyExists errors, but does not as of this
commit.
Update the simple.gotmpl template to properly display scopes, after switching to
the Scope type instead of simply passing around the string the client supplied
broke the template and I never bothered fixing it.
Update the updateScopes method on the scopeStore (and the corresponding
UpdateScopes method on the Context type) to be updateScope/UpdateScope.
Operating on several scopes at a time like that is simply too challenging in
SQL and I can't justify the complexity with a use case.
Add a helper method to ScopeChange called Empty(), which returns true if the
ScopeChange is full of nil values.
Remove the ID from the ScopeChange type, because we're no longer accepting
multiple ScopeChange types in UpdateScope, so we can supply that information
outside the ScopeChange, which matches the rest of our update* methods.
Correct our tests in scope_test.go to correctly use the updateScope method
instead of the old updateScopes method. This generally just resulted in calling
updateScope multiple times, as opposed to just once.
Add a scope table initialization to the sql/postgres_init.sql script.
4 "github.com/secondbit/pan"
7 func (s Scope) GetSQLTableName() string {
11 func (p *postgres) createScopesSQL(scopes []Scope) *pan.Query {
12 fields, _ := pan.GetFields(scopes[0])
13 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(scopes[0]))
14 query.Include("(" + pan.QueryList(fields) + ")")
15 query.Include("VALUES")
16 for _, scope := range scopes {
17 _, values := pan.GetFields(scope)
18 query.Include("("+pan.VariableList(len(values))+")", values...)
20 return query.FlushExpressions(" ")
23 func (p *postgres) createScopes(scopes []Scope) error {
27 query := p.createScopesSQL(scopes)
28 _, err := p.db.Exec(query.String(), query.Args...)
32 func (p *postgres) getScopesSQL(ids []string) *pan.Query {
34 intids := make([]interface{}, len(ids))
35 for pos, id := range ids {
38 fields, _ := pan.GetFields(scope)
39 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope))
41 query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN")
42 query.Include("("+pan.VariableList(len(ids))+")", intids...)
43 return query.FlushExpressions(" ")
46 func (p *postgres) getScopes(ids []string) ([]Scope, error) {
47 query := p.getScopesSQL(ids)
48 rows, err := p.db.Query(query.String(), query.Args...)
55 err := pan.Unmarshal(rows, &scope)
59 scopes = append(scopes, scope)
61 if err = rows.Err(); err != nil {
67 func (p *postgres) updateScopeSQL(id string, change ScopeChange) *pan.Query {
69 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(scope)+" SET ")
70 query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Name")+" = ?", change.Name)
71 query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Description")+" = ?", change.Description)
72 query.FlushExpressions(", ")
74 query.Include(pan.GetUnquotedColumn(scope, "ID")+" = ?", id)
75 return query.FlushExpressions(" ")
78 func (p *postgres) updateScope(id string, change ScopeChange) error {
82 query := p.updateScopeSQL(id, change)
83 _, err := p.db.Exec(query.String(), query.Args...)
87 func (p *postgres) removeScopesSQL(ids []string) *pan.Query {
89 intids := make([]interface{}, len(ids))
90 for pos, id := range ids {
93 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(scope))
95 query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN")
96 query.Include("("+pan.VariableList(len(ids))+")", intids...)
97 return query.FlushExpressions(" ")
100 func (p *postgres) removeScopes(ids []string) error {
101 query := p.removeScopesSQL(ids)
102 _, err := p.db.Exec(query.String(), query.Args...)
109 func (p *postgres) listScopesSQL() *pan.Query {
111 fields, _ := pan.GetFields(scope)
112 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope))
113 return query.FlushExpressions(" ")
116 func (p *postgres) listScopes() ([]Scope, error) {
117 query := p.listScopesSQL()
118 rows, err := p.db.Query(query.String(), query.Args...)
120 return []Scope{}, err
125 err = pan.Unmarshal(rows, &scope)
129 scopes = append(scopes, scope)
131 if err = rows.Err(); err != nil {