From 2ea5f56ad62add855d20a4cea435515fe36ee89d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Sibiril?= <81782+aureliensibiril@users.noreply.github.com> Date: Thu, 16 Apr 2026 14:13:52 +0200 Subject: [PATCH] Add CountByConnectorID helper on access sources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- pkg/coredata/access_source.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/coredata/access_source.go b/pkg/coredata/access_source.go index 01b8cf1fa..023eccfc7 100644 --- a/pkg/coredata/access_source.go +++ b/pkg/coredata/access_source.go @@ -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(