Add CountByConnectorID helper on access sources

Helper for callers that need to know whether a given connector row is
still referenced by any access_sources. Used by the SCIM disconnect
flow, which would otherwise fail with a foreign-key violation when the
connector is shared with an access source.

Mirrors the shape of CountByOrganizationID.

Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
This commit is contained in:
Aurélien Sibiril
2026-04-16 14:13:52 +02:00
committed by Sacha Al Himdani
parent 0a5dfc652f
commit 2ea5f56ad6

View File

@@ -307,6 +307,32 @@ WHERE
return count, nil
}
func (sources *AccessSources) CountByConnectorID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
connectorID gid.GID,
) (int, error) {
q := `
SELECT COUNT(id)
FROM access_sources
WHERE
%s
AND connector_id = @connector_id;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"connector_id": connectorID}
maps.Copy(args, scope.SQLArguments())
var count int
if err := conn.QueryRow(ctx, q, args).Scan(&count); err != nil {
return 0, fmt.Errorf("cannot count access_sources by connector ID: %w", err)
}
return count, nil
}
// LoadScopeSourcesByCampaignID loads the campaign scope sources in deterministic
// name order. Only explicitly scoped sources are returned.
func (sources *AccessSources) LoadScopeSourcesByCampaignID(