Batch-load and validate campaign sources before merging
MergeByCampaignID joined access_review_sources directly from coredata to resolve live sources, so unrecognized or out-of-scope IDs were silently dropped instead of erroring, and in the worst case (every ID invalid) the NOT MATCHED BY SOURCE clause deleted every existing campaign source. The syncCampaignSources ErrResourceNotFound check was therefore unreachable dead code. Add AccessReviewSources.LoadByIDs, matching the existing scoped LoadByIDs pattern (id = ANY(@ids) plus a resolved-count check), and have CreateCampaign, AddCampaignSource, and syncCampaignSources resolve and validate sources up front. MergeByCampaignID now takes the already-loaded sources and builds its desired-state CTE from an unnest() of their values instead of joining access_review_sources, keeping the merge inside the campaign-source entity boundary. Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
@@ -144,6 +144,52 @@ LIMIT 1;
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sources *AccessReviewSources) LoadByIDs(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
ids []gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
connector_id,
|
||||
name,
|
||||
csv_data,
|
||||
name_synced_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
access_review_sources
|
||||
WHERE
|
||||
%s
|
||||
AND id = ANY(@ids)
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"ids": ids}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query access_review_sources: %w", err)
|
||||
}
|
||||
|
||||
result, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[AccessReviewSource])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect access_review_sources: %w", err)
|
||||
}
|
||||
|
||||
*sources = result
|
||||
|
||||
if len(result) != len(gid.NewSet(ids...)) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (as *AccessReviewSource) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Tx,
|
||||
|
||||
Reference in New Issue
Block a user