-
Notifications
You must be signed in to change notification settings - Fork 3
/
session.go
141 lines (118 loc) · 2.99 KB
/
session.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package eventline
import (
"context"
"errors"
"fmt"
"time"
"github.com/exograd/go-daemon/pg"
"github.com/jackc/pgx/v4"
)
type UnknownSessionError struct {
Id Id
}
func (err UnknownSessionError) Error() string {
return fmt.Sprintf("unknown session %q", err.Id)
}
type NewSession struct {
Data *SessionData `json:"data"`
AccountRole AccountRole `json:"account_role"`
AccountSettings *AccountSettings `json:"account_settings"`
}
type Session struct {
Id Id `json:"id"`
AccountId Id `json:"account_id"`
CreationTime time.Time `json:"creation_time"`
UpdateTime time.Time `json:"update_time"`
Data *SessionData `json:"data"`
AccountRole AccountRole `json:"account_role"`
AccountSettings *AccountSettings `json:"account_settings"`
}
type SessionData struct {
ProjectId *Id `json:"project_id,omitempty"`
}
func (s *Session) LoadUpdate(conn pg.Conn, id Id) error {
now := time.Now().UTC()
query := `
UPDATE sessions SET
update_time = $2
WHERE id = $1
RETURNING
id, account_id, creation_time, update_time, data,
account_role, account_settings;
`
err := pg.QueryObject(conn, s, query, id, now)
if errors.Is(err, pgx.ErrNoRows) {
return &UnknownSessionError{Id: id}
}
return err
}
func (s *Session) Insert(conn pg.Conn) error {
query := `
INSERT INTO sessions
(id, account_id, creation_time, update_time, data, account_role,
account_settings)
VALUES
($1, $2, $3, $4, $5, $6,
$7);
`
return pg.Exec(conn, query,
s.Id, s.AccountId, s.CreationTime, s.UpdateTime, s.Data, s.AccountRole,
s.AccountSettings)
}
func (s *Session) UpdateData(conn pg.Conn) error {
query := `
UPDATE sessions SET
data = $2
WHERE id = $1
`
return pg.Exec(conn, query, s.Id, s.Data)
}
func (s *Session) UpdateAccountSettings(conn pg.Conn) error {
query := `
UPDATE sessions SET
account_settings = $2
WHERE id = $1
`
return pg.Exec(conn, query, s.Id, s.AccountSettings)
}
func UpdateSessionsForProjectDeletion(conn pg.Conn, projectId Id) error {
query := `
UPDATE sessions SET
data = data - 'project_id'
WHERE data->>'project_id' = $1
`
return pg.Exec(conn, query, projectId)
}
func (s *Session) Delete(conn pg.Conn) error {
query := `
DELETE FROM sessions
WHERE id = $1;
`
return pg.Exec(conn, query, s.Id)
}
func DeleteOldSessions(conn pg.Conn, retention int) (int64, error) {
ctx := context.Background()
now := time.Now().UTC()
minDate := now.AddDate(0, 0, -retention)
query := `
DELETE FROM sessions
WHERE creation_time < $1
`
res, err := conn.Exec(ctx, query, minDate)
if err != nil {
return -1, err
}
return res.RowsAffected(), nil
}
func (s *Session) FromRow(row pgx.Row) error {
var data SessionData
var accountSettings AccountSettings
err := row.Scan(&s.Id, &s.AccountId, &s.CreationTime, &s.UpdateTime,
&data, &s.AccountRole, &accountSettings)
if err != nil {
return err
}
s.Data = &data
s.AccountSettings = &accountSettings
return nil
}