Skip to content

Commit 91dd701

Browse files
engine/compatmanager: migrate to sqlc (#4104)
* migrate compat module to sqlc * remove old sql code * fix query * fix compat * revert dest migration
1 parent ec6fc7c commit 91dd701

File tree

4 files changed

+274
-96
lines changed

4 files changed

+274
-96
lines changed

engine/compatmanager/db.go

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/target/goalert/engine/processinglock"
88
"github.com/target/goalert/notification/slack"
9-
"github.com/target/goalert/util"
109
)
1110

1211
// DB handles keeping compatibility-related data in sync.
@@ -15,13 +14,6 @@ type DB struct {
1514
lock *processinglock.Lock
1615

1716
cs *slack.ChannelSender
18-
19-
slackSubMissingCM *sql.Stmt
20-
updateSubCMID *sql.Stmt
21-
insertCM *sql.Stmt
22-
23-
cmMissingSub *sql.Stmt
24-
insertSub *sql.Stmt
2517
}
2618

2719
// Name returns the name of the module.
@@ -37,50 +29,9 @@ func NewDB(ctx context.Context, db *sql.DB, cs *slack.ChannelSender) (*DB, error
3729
return nil, err
3830
}
3931

40-
p := &util.Prepare{Ctx: ctx, DB: db}
41-
4232
return &DB{
4333
db: db,
4434
lock: lock,
4535
cs: cs,
46-
47-
// get all entries missing cm_id where provider_id starts with "slack:"
48-
slackSubMissingCM: p.P(`
49-
select id, user_id, subject_id, provider_id from auth_subjects where
50-
provider_id like 'slack:%' and cm_id is null
51-
for update skip locked
52-
limit 10
53-
`),
54-
55-
// update cm_id for a given user_id and subject_id
56-
updateSubCMID: p.P(`
57-
update auth_subjects
58-
set cm_id = (
59-
select id from user_contact_methods
60-
where type = 'SLACK_DM' and value = $2
61-
) where id = $1
62-
`),
63-
64-
insertCM: p.P(`
65-
insert into user_contact_methods (id, name, type, value, user_id, pending)
66-
values ($1, $2, $3, $4, $5, false)
67-
on conflict (type, value) do nothing
68-
`),
69-
70-
// find verified contact methods (disabled false) with no auth subject
71-
cmMissingSub: p.P(`
72-
select id, user_id, value from user_contact_methods where
73-
type = 'SLACK_DM' and not disabled and not exists (
74-
select 1 from auth_subjects where cm_id = user_contact_methods.id
75-
)
76-
for update skip locked
77-
limit 10
78-
`),
79-
80-
insertSub: p.P(`
81-
insert into auth_subjects (user_id, subject_id, provider_id, cm_id)
82-
values ($1, $2, $3, $4)
83-
on conflict (subject_id, provider_id) do update set user_id = $1, cm_id = $4
84-
`),
85-
}, p.Err
36+
}, nil
8637
}

engine/compatmanager/queries.sql

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- name: CompatAuthSubSlackMissingCM :many
2+
-- Get up to 10 auth_subjects (slack only) missing a contact method.
3+
SELECT
4+
*
5+
FROM
6+
auth_subjects
7+
WHERE
8+
provider_id LIKE 'slack:%'
9+
AND cm_id IS NULL
10+
FOR UPDATE
11+
SKIP LOCKED
12+
LIMIT 10;
13+
14+
-- name: CompatAuthSubSetCMID :exec
15+
-- Updates the contact method id for an auth_subject with the given destination.
16+
UPDATE
17+
auth_subjects
18+
SET
19+
cm_id =(
20+
SELECT
21+
id
22+
FROM
23+
user_contact_methods
24+
WHERE
25+
type = 'SLACK_DM'
26+
AND value = $2)
27+
WHERE
28+
auth_subjects.id = $1;
29+
30+
-- name: CompatInsertUserCM :exec
31+
-- Inserts a new contact method for a user.
32+
INSERT INTO user_contact_methods(id, name, type, value, user_id, pending)
33+
VALUES ($1, $2, $3, $4, $5, FALSE)
34+
ON CONFLICT (type, value)
35+
DO NOTHING;
36+
37+
-- name: CompatCMMissingSub :many
38+
-- Get up to 10 contact methods missing an auth_subjects link.
39+
SELECT
40+
id,
41+
user_id,
42+
value
43+
FROM
44+
user_contact_methods
45+
WHERE
46+
type = 'SLACK_DM'
47+
AND NOT disabled
48+
AND NOT EXISTS (
49+
SELECT
50+
1
51+
FROM
52+
auth_subjects
53+
WHERE
54+
cm_id = user_contact_methods.id)
55+
FOR UPDATE
56+
SKIP LOCKED
57+
LIMIT 10;
58+
59+
-- name: CompatUpsertAuthSubject :exec
60+
-- Inserts a new auth_subject for a user.
61+
INSERT INTO auth_subjects(user_id, subject_id, provider_id, cm_id)
62+
VALUES ($1, $2, $3, $4)
63+
ON CONFLICT (subject_id, provider_id)
64+
DO UPDATE SET
65+
user_id = $1, cm_id = $4;
66+

engine/compatmanager/update.go

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/google/uuid"
9+
"github.com/target/goalert/gadb"
910
"github.com/target/goalert/permission"
1011
"github.com/target/goalert/util/log"
1112
"github.com/target/goalert/util/sqlutil"
@@ -39,39 +40,26 @@ func (db *DB) updateAuthSubjects(ctx context.Context) error {
3940
}
4041
defer sqlutil.Rollback(ctx, "engine: update auth subjects", tx)
4142

42-
type cm struct {
43-
ID uuid.UUID
44-
UserID uuid.UUID
45-
SlackUserID string
46-
SlackTeamID string
47-
}
48-
49-
var cms []cm
50-
rows, err := tx.StmtContext(ctx, db.cmMissingSub).QueryContext(ctx)
43+
q := gadb.New(tx)
44+
rows, err := q.CompatCMMissingSub(ctx)
5145
if err != nil {
5246
return fmt.Errorf("query: %w", err)
5347
}
54-
for rows.Next() {
55-
var c cm
56-
err = rows.Scan(&c.ID, &c.UserID, &c.SlackUserID)
48+
for _, row := range rows {
49+
u, err := db.cs.User(ctx, row.Value)
5750
if err != nil {
58-
return fmt.Errorf("scan: %w", err)
59-
}
60-
61-
u, err := db.cs.User(ctx, c.SlackUserID)
62-
if err != nil {
63-
log.Log(ctx, fmt.Errorf("update auth subjects: lookup Slack user (%s): %w", c.SlackUserID, err))
51+
log.Log(ctx, fmt.Errorf("update auth subjects: lookup Slack user (%s): %w", row.Value, err))
6452
continue
6553
}
6654

67-
c.SlackTeamID = u.TeamID
68-
cms = append(cms, c)
69-
}
70-
71-
for _, c := range cms {
72-
_, err = tx.StmtContext(ctx, db.insertSub).ExecContext(ctx, c.UserID, c.SlackUserID, "slack:"+c.SlackTeamID, c.ID)
55+
err = q.CompatUpsertAuthSubject(ctx, gadb.CompatUpsertAuthSubjectParams{
56+
UserID: row.UserID,
57+
ProviderID: "slack:" + u.TeamID,
58+
SubjectID: u.ID,
59+
CmID: uuid.NullUUID{UUID: row.ID, Valid: true},
60+
})
7361
if err != nil {
74-
return fmt.Errorf("insert: %w", err)
62+
return fmt.Errorf("upsert auth subject: %w", err)
7563
}
7664
}
7765

@@ -83,36 +71,25 @@ func (db *DB) updateAuthSubjects(ctx context.Context) error {
8371
return nil
8472
}
8573

74+
// updateContactMethods will create contact methods for associated auth_subjects (e.g. Slack direct message).
75+
//
76+
// To do this, we look for auth_subjects that are missing the contact method ID
77+
// field (`cm_id`) for slack, and create a Slack DM contact method for the user
78+
// associated with the record.
8679
func (db *DB) updateContactMethods(ctx context.Context) error {
8780
tx, err := db.lock.BeginTx(ctx, nil)
8881
if err != nil {
8982
return fmt.Errorf("begin tx: %w", err)
9083
}
9184
defer sqlutil.Rollback(ctx, "engine: update contact methods", tx)
9285

93-
type sub struct {
94-
ID int
95-
UserID string
96-
SubjectID string
97-
ProviderID string
98-
}
99-
100-
var subs []sub
101-
rows, err := tx.StmtContext(ctx, db.slackSubMissingCM).QueryContext(ctx)
86+
q := gadb.New(tx)
87+
rows, err := q.CompatAuthSubSlackMissingCM(ctx)
10288
if err != nil {
10389
return fmt.Errorf("query: %w", err)
10490
}
10591

106-
for rows.Next() {
107-
var s sub
108-
err = rows.Scan(&s.ID, &s.UserID, &s.SubjectID, &s.ProviderID)
109-
if err != nil {
110-
return fmt.Errorf("scan: %w", err)
111-
}
112-
subs = append(subs, s)
113-
}
114-
115-
for _, s := range subs {
92+
for _, s := range rows {
11693
// provider id contains the team id in the format "slack:team_id"
11794
// but we need to store the contact method id in the format "team_id:subject_id"
11895
teamID := strings.TrimPrefix(s.ProviderID, "slack:")
@@ -123,12 +100,21 @@ func (db *DB) updateContactMethods(ctx context.Context) error {
123100
continue
124101
}
125102

126-
_, err = tx.StmtContext(ctx, db.insertCM).ExecContext(ctx, uuid.New(), team.Name, "SLACK_DM", value, s.UserID)
103+
err = q.CompatInsertUserCM(ctx, gadb.CompatInsertUserCMParams{
104+
ID: uuid.New(),
105+
Name: team.Name,
106+
Type: gadb.EnumUserContactMethodTypeSLACKDM,
107+
Value: value,
108+
UserID: s.UserID,
109+
})
127110
if err != nil {
128111
return fmt.Errorf("insert cm: %w", err)
129112
}
130113

131-
_, err = tx.StmtContext(ctx, db.updateSubCMID).ExecContext(ctx, s.ID, value)
114+
err = q.CompatAuthSubSetCMID(ctx, gadb.CompatAuthSubSetCMIDParams{
115+
ID: s.ID,
116+
Value: value,
117+
})
132118
if err != nil {
133119
return fmt.Errorf("update sub cm_id: %w", err)
134120
}

0 commit comments

Comments
 (0)