From a5f365e8e9f61e23ecdae6877a58659e67dacb84 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Wed, 9 Oct 2024 14:32:51 -0500 Subject: [PATCH 1/5] migrate compat module to sqlc --- engine/compatmanager/queries.sql | 57 +++++++++++ engine/compatmanager/update.go | 82 +++++++-------- gadb/queries.sql.go | 170 +++++++++++++++++++++++++++++++ sqlc.yaml | 1 + 4 files changed, 264 insertions(+), 46 deletions(-) create mode 100644 engine/compatmanager/queries.sql diff --git a/engine/compatmanager/queries.sql b/engine/compatmanager/queries.sql new file mode 100644 index 0000000000..fb1eb0a045 --- /dev/null +++ b/engine/compatmanager/queries.sql @@ -0,0 +1,57 @@ +-- name: CompatSlackSubMissingCM :many +-- Get up to 10 auth_subjects (slack only) missing a contact method. +SELECT + * +FROM + auth_subjects +WHERE + provider_id LIKE 'slack:%' + AND cm_id IS NULL +FOR UPDATE + SKIP LOCKED +LIMIT 10; + +-- name: CompatLinkAuthSubjectCM :exec +-- Updates the contact method id for an auth_subject with the given destination. +UPDATE + auth_subjects +SET + cm_id = $2 +WHERE + auth_subjects.id = $1; + +-- name: CompatInsertUserCM :exec +-- Inserts a new contact method for a user. +INSERT INTO user_contact_methods(id, name, dest, user_id, pending) + VALUES ($1, $2, $3, $4, FALSE) +ON CONFLICT (dest) + DO NOTHING; + +-- name: CompatCMMissingSub :many +-- Get up to 10 contact methods missing a auth_subjects link. +SELECT + * +FROM + user_contact_methods +WHERE + dest -> type = @dest_type::text + AND NOT disabled + AND NOT EXISTS ( + SELECT + 1 + FROM + auth_subjects + WHERE + cm_id = user_contact_methods.id) +FOR UPDATE + SKIP LOCKED +LIMIT 10; + +-- name: CompatUpsertAuthSubject :exec +-- Inserts a new auth_subject for a user. +INSERT INTO auth_subjects(user_id, subject_id, provider_id, cm_id) + VALUES ($1, $2, $3, $4) +ON CONFLICT (subject_id, provider_id) + DO UPDATE SET + user_id = $1, cm_id = $4; + diff --git a/engine/compatmanager/update.go b/engine/compatmanager/update.go index 679b67530c..973456a06d 100644 --- a/engine/compatmanager/update.go +++ b/engine/compatmanager/update.go @@ -6,6 +6,8 @@ import ( "strings" "github.com/google/uuid" + "github.com/target/goalert/gadb" + "github.com/target/goalert/notification/slack" "github.com/target/goalert/permission" "github.com/target/goalert/util/log" "github.com/target/goalert/util/sqlutil" @@ -39,39 +41,26 @@ func (db *DB) updateAuthSubjects(ctx context.Context) error { } defer sqlutil.Rollback(ctx, "engine: update auth subjects", tx) - type cm struct { - ID uuid.UUID - UserID uuid.UUID - SlackUserID string - SlackTeamID string - } - - var cms []cm - rows, err := tx.StmtContext(ctx, db.cmMissingSub).QueryContext(ctx) + q := gadb.New(tx) + rows, err := q.CompatCMMissingSub(ctx, slack.DestTypeSlackDirectMessage) if err != nil { return fmt.Errorf("query: %w", err) } - for rows.Next() { - var c cm - err = rows.Scan(&c.ID, &c.UserID, &c.SlackUserID) + for _, row := range rows { + u, err := db.cs.User(ctx, row.Dest.DestV1.Arg(slack.FieldSlackUserID)) if err != nil { - return fmt.Errorf("scan: %w", err) - } - - u, err := db.cs.User(ctx, c.SlackUserID) - if err != nil { - log.Log(ctx, fmt.Errorf("update auth subjects: lookup Slack user (%s): %w", c.SlackUserID, err)) + log.Log(ctx, fmt.Errorf("update auth subjects: lookup Slack user (%s): %w", row.Dest.DestV1.Arg(slack.FieldSlackUserID), err)) continue } - c.SlackTeamID = u.TeamID - cms = append(cms, c) - } - - for _, c := range cms { - _, err = tx.StmtContext(ctx, db.insertSub).ExecContext(ctx, c.UserID, c.SlackUserID, "slack:"+c.SlackTeamID, c.ID) + err = q.CompatUpsertAuthSubject(ctx, gadb.CompatUpsertAuthSubjectParams{ + UserID: row.UserID, + ProviderID: "slack:" + u.TeamID, + SubjectID: u.ID, + CmID: uuid.NullUUID{UUID: row.ID, Valid: true}, + }) if err != nil { - return fmt.Errorf("insert: %w", err) + return fmt.Errorf("upsert auth subject: %w", err) } } @@ -90,29 +79,13 @@ func (db *DB) updateContactMethods(ctx context.Context) error { } defer sqlutil.Rollback(ctx, "engine: update contact methods", tx) - type sub struct { - ID int - UserID string - SubjectID string - ProviderID string - } - - var subs []sub - rows, err := tx.StmtContext(ctx, db.slackSubMissingCM).QueryContext(ctx) + q := gadb.New(tx) + rows, err := q.CompatSlackSubMissingCM(ctx) if err != nil { return fmt.Errorf("query: %w", err) } - for rows.Next() { - var s sub - err = rows.Scan(&s.ID, &s.UserID, &s.SubjectID, &s.ProviderID) - if err != nil { - return fmt.Errorf("scan: %w", err) - } - subs = append(subs, s) - } - - for _, s := range subs { + for _, s := range rows { // provider id contains the team id in the format "slack:team_id" // but we need to store the contact method id in the format "team_id:subject_id" teamID := strings.TrimPrefix(s.ProviderID, "slack:") @@ -123,12 +96,29 @@ func (db *DB) updateContactMethods(ctx context.Context) error { continue } - _, err = tx.StmtContext(ctx, db.insertCM).ExecContext(ctx, uuid.New(), team.Name, "SLACK_DM", value, s.UserID) + id := uuid.New() + err = q.CompatInsertUserCM(ctx, gadb.CompatInsertUserCMParams{ + ID: id, + Name: team.Name, + Dest: gadb.NullDestV1{ + DestV1: gadb.DestV1{ + Type: slack.DestTypeSlackDirectMessage, + Args: map[string]string{ + slack.FieldSlackUserID: value, + }, + }, + Valid: true, + }, + UserID: s.UserID, + }) if err != nil { return fmt.Errorf("insert cm: %w", err) } - _, err = tx.StmtContext(ctx, db.updateSubCMID).ExecContext(ctx, s.ID, value) + err = q.CompatLinkAuthSubjectCM(ctx, gadb.CompatLinkAuthSubjectCMParams{ + ID: s.ID, + CmID: uuid.NullUUID{UUID: id, Valid: true}, + }) if err != nil { return fmt.Errorf("update sub cm_id: %w", err) } diff --git a/gadb/queries.sql.go b/gadb/queries.sql.go index f653d991ce..e517daf991 100644 --- a/gadb/queries.sql.go +++ b/gadb/queries.sql.go @@ -835,6 +835,176 @@ func (q *Queries) CalSubUserNames(ctx context.Context, dollar_1 []uuid.UUID) ([] return items, nil } +const compatCMMissingSub = `-- name: CompatCMMissingSub :many +SELECT + dest, disabled, enable_status_updates, id, last_test_verify_at, metadata, name, pending, type, user_id, value +FROM + user_contact_methods +WHERE + dest -> type = $1::text + AND NOT disabled + AND NOT EXISTS ( + SELECT + 1 + FROM + auth_subjects + WHERE + cm_id = user_contact_methods.id) +FOR UPDATE + SKIP LOCKED +LIMIT 10 +` + +// Get up to 10 contact methods missing a auth_subjects link. +func (q *Queries) CompatCMMissingSub(ctx context.Context, destType string) ([]UserContactMethod, error) { + rows, err := q.db.QueryContext(ctx, compatCMMissingSub, destType) + if err != nil { + return nil, err + } + defer rows.Close() + var items []UserContactMethod + for rows.Next() { + var i UserContactMethod + if err := rows.Scan( + &i.Dest, + &i.Disabled, + &i.EnableStatusUpdates, + &i.ID, + &i.LastTestVerifyAt, + &i.Metadata, + &i.Name, + &i.Pending, + &i.Type, + &i.UserID, + &i.Value, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const compatInsertUserCM = `-- name: CompatInsertUserCM :exec +INSERT INTO user_contact_methods(id, name, dest, user_id, pending) + VALUES ($1, $2, $3, $4, FALSE) +ON CONFLICT (dest) + DO NOTHING +` + +type CompatInsertUserCMParams struct { + ID uuid.UUID + Name string + Dest NullDestV1 + UserID uuid.UUID +} + +// Inserts a new contact method for a user. +func (q *Queries) CompatInsertUserCM(ctx context.Context, arg CompatInsertUserCMParams) error { + _, err := q.db.ExecContext(ctx, compatInsertUserCM, + arg.ID, + arg.Name, + arg.Dest, + arg.UserID, + ) + return err +} + +const compatLinkAuthSubjectCM = `-- name: CompatLinkAuthSubjectCM :exec +UPDATE + auth_subjects +SET + cm_id = $2 +WHERE + auth_subjects.id = $1 +` + +type CompatLinkAuthSubjectCMParams struct { + ID int64 + CmID uuid.NullUUID +} + +// Updates the contact method id for an auth_subject with the given destination. +func (q *Queries) CompatLinkAuthSubjectCM(ctx context.Context, arg CompatLinkAuthSubjectCMParams) error { + _, err := q.db.ExecContext(ctx, compatLinkAuthSubjectCM, arg.ID, arg.CmID) + return err +} + +const compatSlackSubMissingCM = `-- name: CompatSlackSubMissingCM :many +SELECT + cm_id, id, provider_id, subject_id, user_id +FROM + auth_subjects +WHERE + provider_id LIKE 'slack:%' + AND cm_id IS NULL +FOR UPDATE + SKIP LOCKED +LIMIT 10 +` + +// Get up to 10 auth_subjects (slack only) missing a contact method. +func (q *Queries) CompatSlackSubMissingCM(ctx context.Context) ([]AuthSubject, error) { + rows, err := q.db.QueryContext(ctx, compatSlackSubMissingCM) + if err != nil { + return nil, err + } + defer rows.Close() + var items []AuthSubject + for rows.Next() { + var i AuthSubject + if err := rows.Scan( + &i.CmID, + &i.ID, + &i.ProviderID, + &i.SubjectID, + &i.UserID, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const compatUpsertAuthSubject = `-- name: CompatUpsertAuthSubject :exec +INSERT INTO auth_subjects(user_id, subject_id, provider_id, cm_id) + VALUES ($1, $2, $3, $4) +ON CONFLICT (subject_id, provider_id) + DO UPDATE SET + user_id = $1, cm_id = $4 +` + +type CompatUpsertAuthSubjectParams struct { + UserID uuid.UUID + SubjectID string + ProviderID string + CmID uuid.NullUUID +} + +// Inserts a new auth_subject for a user. +func (q *Queries) CompatUpsertAuthSubject(ctx context.Context, arg CompatUpsertAuthSubjectParams) error { + _, err := q.db.ExecContext(ctx, compatUpsertAuthSubject, + arg.UserID, + arg.SubjectID, + arg.ProviderID, + arg.CmID, + ) + return err +} + const contactMethodAdd = `-- name: ContactMethodAdd :exec INSERT INTO user_contact_methods(id, name, dest, disabled, user_id, enable_status_updates) VALUES ($1, $2, $3, $4, $5, $6) diff --git a/sqlc.yaml b/sqlc.yaml index d46710722a..fe8bf70507 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -21,6 +21,7 @@ sql: - alert/queries.sql - notice/queries.sql - graphql2/graphqlapp/queries.sql + - engine/compatmanager/queries.sql - engine/statusmgr/queries.sql - engine/message/queries.sql - engine/schedulemanager/queries.sql From 7f8f22e38ec1627d8d136ff296b021cd73e41f70 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Wed, 9 Oct 2024 14:33:34 -0500 Subject: [PATCH 2/5] remove old sql code --- engine/compatmanager/db.go | 51 +------------------------------------- 1 file changed, 1 insertion(+), 50 deletions(-) diff --git a/engine/compatmanager/db.go b/engine/compatmanager/db.go index 72292694a6..87a998657e 100644 --- a/engine/compatmanager/db.go +++ b/engine/compatmanager/db.go @@ -6,7 +6,6 @@ import ( "github.com/target/goalert/engine/processinglock" "github.com/target/goalert/notification/slack" - "github.com/target/goalert/util" ) // DB handles keeping compatibility-related data in sync. @@ -15,13 +14,6 @@ type DB struct { lock *processinglock.Lock cs *slack.ChannelSender - - slackSubMissingCM *sql.Stmt - updateSubCMID *sql.Stmt - insertCM *sql.Stmt - - cmMissingSub *sql.Stmt - insertSub *sql.Stmt } // Name returns the name of the module. @@ -37,50 +29,9 @@ func NewDB(ctx context.Context, db *sql.DB, cs *slack.ChannelSender) (*DB, error return nil, err } - p := &util.Prepare{Ctx: ctx, DB: db} - return &DB{ db: db, lock: lock, cs: cs, - - // get all entries missing cm_id where provider_id starts with "slack:" - slackSubMissingCM: p.P(` - select id, user_id, subject_id, provider_id from auth_subjects where - provider_id like 'slack:%' and cm_id is null - for update skip locked - limit 10 - `), - - // update cm_id for a given user_id and subject_id - updateSubCMID: p.P(` - update auth_subjects - set cm_id = ( - select id from user_contact_methods - where type = 'SLACK_DM' and value = $2 - ) where id = $1 - `), - - insertCM: p.P(` - insert into user_contact_methods (id, name, type, value, user_id, pending) - values ($1, $2, $3, $4, $5, false) - on conflict (type, value) do nothing - `), - - // find verified contact methods (disabled false) with no auth subject - cmMissingSub: p.P(` - select id, user_id, value from user_contact_methods where - type = 'SLACK_DM' and not disabled and not exists ( - select 1 from auth_subjects where cm_id = user_contact_methods.id - ) - for update skip locked - limit 10 - `), - - insertSub: p.P(` - insert into auth_subjects (user_id, subject_id, provider_id, cm_id) - values ($1, $2, $3, $4) - on conflict (subject_id, provider_id) do update set user_id = $1, cm_id = $4 - `), - }, p.Err + }, nil } From eddc3a58d6e2a75c7d910c7ed830c36b325f36d7 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Wed, 9 Oct 2024 14:36:27 -0500 Subject: [PATCH 3/5] fix query --- engine/compatmanager/queries.sql | 4 ++-- gadb/queries.sql.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/engine/compatmanager/queries.sql b/engine/compatmanager/queries.sql index fb1eb0a045..4d00090ef5 100644 --- a/engine/compatmanager/queries.sql +++ b/engine/compatmanager/queries.sql @@ -28,13 +28,13 @@ ON CONFLICT (dest) DO NOTHING; -- name: CompatCMMissingSub :many --- Get up to 10 contact methods missing a auth_subjects link. +-- Get up to 10 contact methods missing an auth_subjects link. SELECT * FROM user_contact_methods WHERE - dest -> type = @dest_type::text + dest ->> 'type' = @dest_type::text AND NOT disabled AND NOT EXISTS ( SELECT diff --git a/gadb/queries.sql.go b/gadb/queries.sql.go index e517daf991..1d9d56de10 100644 --- a/gadb/queries.sql.go +++ b/gadb/queries.sql.go @@ -841,7 +841,7 @@ SELECT FROM user_contact_methods WHERE - dest -> type = $1::text + dest ->> 'type' = $1::text AND NOT disabled AND NOT EXISTS ( SELECT @@ -855,7 +855,7 @@ FOR UPDATE LIMIT 10 ` -// Get up to 10 contact methods missing a auth_subjects link. +// Get up to 10 contact methods missing an auth_subjects link. func (q *Queries) CompatCMMissingSub(ctx context.Context, destType string) ([]UserContactMethod, error) { rows, err := q.db.QueryContext(ctx, compatCMMissingSub, destType) if err != nil { From eddd1101a0b3898c18d2549551d1b586855eb669 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Wed, 9 Oct 2024 15:02:50 -0500 Subject: [PATCH 4/5] fix compat --- engine/compatmanager/queries.sql | 14 +++- engine/compatmanager/update.go | 33 ++++---- gadb/queries.sql.go | 134 ++++++++++++++++--------------- 3 files changed, 101 insertions(+), 80 deletions(-) diff --git a/engine/compatmanager/queries.sql b/engine/compatmanager/queries.sql index 4d00090ef5..72cea1fa49 100644 --- a/engine/compatmanager/queries.sql +++ b/engine/compatmanager/queries.sql @@ -1,4 +1,4 @@ --- name: CompatSlackSubMissingCM :many +-- name: CompatAuthSubSlackMissingCM :many -- Get up to 10 auth_subjects (slack only) missing a contact method. SELECT * @@ -11,12 +11,20 @@ FOR UPDATE SKIP LOCKED LIMIT 10; --- name: CompatLinkAuthSubjectCM :exec +-- name: CompatAuthSubSetCMID :exec -- Updates the contact method id for an auth_subject with the given destination. UPDATE auth_subjects SET - cm_id = $2 + cm_id =( + SELECT + id + FROM + user_contact_methods cm + WHERE + dest = $2 + AND cm.user_id = auth_subjects.user_id + LIMIT 1) WHERE auth_subjects.id = $1; diff --git a/engine/compatmanager/update.go b/engine/compatmanager/update.go index 973456a06d..cf668ab709 100644 --- a/engine/compatmanager/update.go +++ b/engine/compatmanager/update.go @@ -72,6 +72,11 @@ func (db *DB) updateAuthSubjects(ctx context.Context) error { return nil } +// updateContactMethods will create contact methods for associated auth_subjects (e.g. Slack direct message). +// +// To do this, we look for auth_subjects that are missing the contact method ID +// field (`cm_id`) for slack, and create a Slack DM contact method for the user +// associated with the record. func (db *DB) updateContactMethods(ctx context.Context) error { tx, err := db.lock.BeginTx(ctx, nil) if err != nil { @@ -80,7 +85,7 @@ func (db *DB) updateContactMethods(ctx context.Context) error { defer sqlutil.Rollback(ctx, "engine: update contact methods", tx) q := gadb.New(tx) - rows, err := q.CompatSlackSubMissingCM(ctx) + rows, err := q.CompatAuthSubSlackMissingCM(ctx) if err != nil { return fmt.Errorf("query: %w", err) } @@ -96,28 +101,28 @@ func (db *DB) updateContactMethods(ctx context.Context) error { continue } - id := uuid.New() - err = q.CompatInsertUserCM(ctx, gadb.CompatInsertUserCMParams{ - ID: id, - Name: team.Name, - Dest: gadb.NullDestV1{ - DestV1: gadb.DestV1{ - Type: slack.DestTypeSlackDirectMessage, - Args: map[string]string{ - slack.FieldSlackUserID: value, - }, + dest := gadb.NullDestV1{ + DestV1: gadb.DestV1{ + Type: slack.DestTypeSlackDirectMessage, + Args: map[string]string{ + slack.FieldSlackUserID: value, }, - Valid: true, }, + Valid: true, + } + err = q.CompatInsertUserCM(ctx, gadb.CompatInsertUserCMParams{ + ID: uuid.New(), + Name: team.Name, + Dest: dest, UserID: s.UserID, }) if err != nil { return fmt.Errorf("insert cm: %w", err) } - err = q.CompatLinkAuthSubjectCM(ctx, gadb.CompatLinkAuthSubjectCMParams{ + err = q.CompatAuthSubSetCMID(ctx, gadb.CompatAuthSubSetCMIDParams{ ID: s.ID, - CmID: uuid.NullUUID{UUID: id, Valid: true}, + Dest: dest, }) if err != nil { return fmt.Errorf("update sub cm_id: %w", err) diff --git a/gadb/queries.sql.go b/gadb/queries.sql.go index 1d9d56de10..984ed43dfb 100644 --- a/gadb/queries.sql.go +++ b/gadb/queries.sql.go @@ -835,6 +835,77 @@ func (q *Queries) CalSubUserNames(ctx context.Context, dollar_1 []uuid.UUID) ([] return items, nil } +const compatAuthSubSetCMID = `-- name: CompatAuthSubSetCMID :exec +UPDATE + auth_subjects +SET + cm_id =( + SELECT + id + FROM + user_contact_methods cm + WHERE + dest = $2 + AND cm.user_id = auth_subjects.user_id + LIMIT 1) +WHERE + auth_subjects.id = $1 +` + +type CompatAuthSubSetCMIDParams struct { + ID int64 + Dest NullDestV1 +} + +// Updates the contact method id for an auth_subject with the given destination. +func (q *Queries) CompatAuthSubSetCMID(ctx context.Context, arg CompatAuthSubSetCMIDParams) error { + _, err := q.db.ExecContext(ctx, compatAuthSubSetCMID, arg.ID, arg.Dest) + return err +} + +const compatAuthSubSlackMissingCM = `-- name: CompatAuthSubSlackMissingCM :many +SELECT + cm_id, id, provider_id, subject_id, user_id +FROM + auth_subjects +WHERE + provider_id LIKE 'slack:%' + AND cm_id IS NULL +FOR UPDATE + SKIP LOCKED +LIMIT 10 +` + +// Get up to 10 auth_subjects (slack only) missing a contact method. +func (q *Queries) CompatAuthSubSlackMissingCM(ctx context.Context) ([]AuthSubject, error) { + rows, err := q.db.QueryContext(ctx, compatAuthSubSlackMissingCM) + if err != nil { + return nil, err + } + defer rows.Close() + var items []AuthSubject + for rows.Next() { + var i AuthSubject + if err := rows.Scan( + &i.CmID, + &i.ID, + &i.ProviderID, + &i.SubjectID, + &i.UserID, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const compatCMMissingSub = `-- name: CompatCMMissingSub :many SELECT dest, disabled, enable_status_updates, id, last_test_verify_at, metadata, name, pending, type, user_id, value @@ -916,69 +987,6 @@ func (q *Queries) CompatInsertUserCM(ctx context.Context, arg CompatInsertUserCM return err } -const compatLinkAuthSubjectCM = `-- name: CompatLinkAuthSubjectCM :exec -UPDATE - auth_subjects -SET - cm_id = $2 -WHERE - auth_subjects.id = $1 -` - -type CompatLinkAuthSubjectCMParams struct { - ID int64 - CmID uuid.NullUUID -} - -// Updates the contact method id for an auth_subject with the given destination. -func (q *Queries) CompatLinkAuthSubjectCM(ctx context.Context, arg CompatLinkAuthSubjectCMParams) error { - _, err := q.db.ExecContext(ctx, compatLinkAuthSubjectCM, arg.ID, arg.CmID) - return err -} - -const compatSlackSubMissingCM = `-- name: CompatSlackSubMissingCM :many -SELECT - cm_id, id, provider_id, subject_id, user_id -FROM - auth_subjects -WHERE - provider_id LIKE 'slack:%' - AND cm_id IS NULL -FOR UPDATE - SKIP LOCKED -LIMIT 10 -` - -// Get up to 10 auth_subjects (slack only) missing a contact method. -func (q *Queries) CompatSlackSubMissingCM(ctx context.Context) ([]AuthSubject, error) { - rows, err := q.db.QueryContext(ctx, compatSlackSubMissingCM) - if err != nil { - return nil, err - } - defer rows.Close() - var items []AuthSubject - for rows.Next() { - var i AuthSubject - if err := rows.Scan( - &i.CmID, - &i.ID, - &i.ProviderID, - &i.SubjectID, - &i.UserID, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - const compatUpsertAuthSubject = `-- name: CompatUpsertAuthSubject :exec INSERT INTO auth_subjects(user_id, subject_id, provider_id, cm_id) VALUES ($1, $2, $3, $4) From 318fc6b589ae4fcc50987ff32ca31c5244f78cf7 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Wed, 9 Oct 2024 15:08:51 -0500 Subject: [PATCH 5/5] revert dest migration --- engine/compatmanager/queries.sql | 19 +++++----- engine/compatmanager/update.go | 23 ++++--------- gadb/queries.sql.go | 59 +++++++++++++++----------------- 3 files changed, 45 insertions(+), 56 deletions(-) diff --git a/engine/compatmanager/queries.sql b/engine/compatmanager/queries.sql index 72cea1fa49..87c7f49d52 100644 --- a/engine/compatmanager/queries.sql +++ b/engine/compatmanager/queries.sql @@ -20,29 +20,30 @@ SET SELECT id FROM - user_contact_methods cm + user_contact_methods WHERE - dest = $2 - AND cm.user_id = auth_subjects.user_id - LIMIT 1) + type = 'SLACK_DM' + AND value = $2) WHERE auth_subjects.id = $1; -- name: CompatInsertUserCM :exec -- Inserts a new contact method for a user. -INSERT INTO user_contact_methods(id, name, dest, user_id, pending) - VALUES ($1, $2, $3, $4, FALSE) -ON CONFLICT (dest) +INSERT INTO user_contact_methods(id, name, type, value, user_id, pending) + VALUES ($1, $2, $3, $4, $5, FALSE) +ON CONFLICT (type, value) DO NOTHING; -- name: CompatCMMissingSub :many -- Get up to 10 contact methods missing an auth_subjects link. SELECT - * + id, + user_id, + value FROM user_contact_methods WHERE - dest ->> 'type' = @dest_type::text + type = 'SLACK_DM' AND NOT disabled AND NOT EXISTS ( SELECT diff --git a/engine/compatmanager/update.go b/engine/compatmanager/update.go index cf668ab709..746ed19783 100644 --- a/engine/compatmanager/update.go +++ b/engine/compatmanager/update.go @@ -7,7 +7,6 @@ import ( "github.com/google/uuid" "github.com/target/goalert/gadb" - "github.com/target/goalert/notification/slack" "github.com/target/goalert/permission" "github.com/target/goalert/util/log" "github.com/target/goalert/util/sqlutil" @@ -42,14 +41,14 @@ func (db *DB) updateAuthSubjects(ctx context.Context) error { defer sqlutil.Rollback(ctx, "engine: update auth subjects", tx) q := gadb.New(tx) - rows, err := q.CompatCMMissingSub(ctx, slack.DestTypeSlackDirectMessage) + rows, err := q.CompatCMMissingSub(ctx) if err != nil { return fmt.Errorf("query: %w", err) } for _, row := range rows { - u, err := db.cs.User(ctx, row.Dest.DestV1.Arg(slack.FieldSlackUserID)) + u, err := db.cs.User(ctx, row.Value) if err != nil { - log.Log(ctx, fmt.Errorf("update auth subjects: lookup Slack user (%s): %w", row.Dest.DestV1.Arg(slack.FieldSlackUserID), err)) + log.Log(ctx, fmt.Errorf("update auth subjects: lookup Slack user (%s): %w", row.Value, err)) continue } @@ -101,19 +100,11 @@ func (db *DB) updateContactMethods(ctx context.Context) error { continue } - dest := gadb.NullDestV1{ - DestV1: gadb.DestV1{ - Type: slack.DestTypeSlackDirectMessage, - Args: map[string]string{ - slack.FieldSlackUserID: value, - }, - }, - Valid: true, - } err = q.CompatInsertUserCM(ctx, gadb.CompatInsertUserCMParams{ ID: uuid.New(), Name: team.Name, - Dest: dest, + Type: gadb.EnumUserContactMethodTypeSLACKDM, + Value: value, UserID: s.UserID, }) if err != nil { @@ -121,8 +112,8 @@ func (db *DB) updateContactMethods(ctx context.Context) error { } err = q.CompatAuthSubSetCMID(ctx, gadb.CompatAuthSubSetCMIDParams{ - ID: s.ID, - Dest: dest, + ID: s.ID, + Value: value, }) if err != nil { return fmt.Errorf("update sub cm_id: %w", err) diff --git a/gadb/queries.sql.go b/gadb/queries.sql.go index 984ed43dfb..02292e44a0 100644 --- a/gadb/queries.sql.go +++ b/gadb/queries.sql.go @@ -843,23 +843,22 @@ SET SELECT id FROM - user_contact_methods cm + user_contact_methods WHERE - dest = $2 - AND cm.user_id = auth_subjects.user_id - LIMIT 1) + type = 'SLACK_DM' + AND value = $2) WHERE auth_subjects.id = $1 ` type CompatAuthSubSetCMIDParams struct { - ID int64 - Dest NullDestV1 + ID int64 + Value string } // Updates the contact method id for an auth_subject with the given destination. func (q *Queries) CompatAuthSubSetCMID(ctx context.Context, arg CompatAuthSubSetCMIDParams) error { - _, err := q.db.ExecContext(ctx, compatAuthSubSetCMID, arg.ID, arg.Dest) + _, err := q.db.ExecContext(ctx, compatAuthSubSetCMID, arg.ID, arg.Value) return err } @@ -908,11 +907,13 @@ func (q *Queries) CompatAuthSubSlackMissingCM(ctx context.Context) ([]AuthSubjec const compatCMMissingSub = `-- name: CompatCMMissingSub :many SELECT - dest, disabled, enable_status_updates, id, last_test_verify_at, metadata, name, pending, type, user_id, value + id, + user_id, + value FROM user_contact_methods WHERE - dest ->> 'type' = $1::text + type = 'SLACK_DM' AND NOT disabled AND NOT EXISTS ( SELECT @@ -926,29 +927,23 @@ FOR UPDATE LIMIT 10 ` +type CompatCMMissingSubRow struct { + ID uuid.UUID + UserID uuid.UUID + Value string +} + // Get up to 10 contact methods missing an auth_subjects link. -func (q *Queries) CompatCMMissingSub(ctx context.Context, destType string) ([]UserContactMethod, error) { - rows, err := q.db.QueryContext(ctx, compatCMMissingSub, destType) +func (q *Queries) CompatCMMissingSub(ctx context.Context) ([]CompatCMMissingSubRow, error) { + rows, err := q.db.QueryContext(ctx, compatCMMissingSub) if err != nil { return nil, err } defer rows.Close() - var items []UserContactMethod + var items []CompatCMMissingSubRow for rows.Next() { - var i UserContactMethod - if err := rows.Scan( - &i.Dest, - &i.Disabled, - &i.EnableStatusUpdates, - &i.ID, - &i.LastTestVerifyAt, - &i.Metadata, - &i.Name, - &i.Pending, - &i.Type, - &i.UserID, - &i.Value, - ); err != nil { + var i CompatCMMissingSubRow + if err := rows.Scan(&i.ID, &i.UserID, &i.Value); err != nil { return nil, err } items = append(items, i) @@ -963,16 +958,17 @@ func (q *Queries) CompatCMMissingSub(ctx context.Context, destType string) ([]Us } const compatInsertUserCM = `-- name: CompatInsertUserCM :exec -INSERT INTO user_contact_methods(id, name, dest, user_id, pending) - VALUES ($1, $2, $3, $4, FALSE) -ON CONFLICT (dest) +INSERT INTO user_contact_methods(id, name, type, value, user_id, pending) + VALUES ($1, $2, $3, $4, $5, FALSE) +ON CONFLICT (type, value) DO NOTHING ` type CompatInsertUserCMParams struct { ID uuid.UUID Name string - Dest NullDestV1 + Type EnumUserContactMethodType + Value string UserID uuid.UUID } @@ -981,7 +977,8 @@ func (q *Queries) CompatInsertUserCM(ctx context.Context, arg CompatInsertUserCM _, err := q.db.ExecContext(ctx, compatInsertUserCM, arg.ID, arg.Name, - arg.Dest, + arg.Type, + arg.Value, arg.UserID, ) return err