Use coredata models in log export batch loaders

Drop export-only row types; load Identity, PersonalAPIKey,
and MembershipProfile through the usual slice LoadByIDs helpers.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Bryan FRIMIN <bryan@frimin.fr>
This commit is contained in:
Cursor Agent
2026-07-29 21:45:45 +00:00
parent e4d86f0f9e
commit 6cbf16814c
4 changed files with 75 additions and 61 deletions

View File

@@ -388,17 +388,7 @@ WHERE
return count, nil
}
type (
IdentityAuditLogActorRow struct {
ID gid.GID `db:"id"`
EmailAddress mail.Addr `db:"email_address"`
FullName string `db:"full_name"`
}
IdentityAuditLogActorRows []*IdentityAuditLogActorRow
)
func (rows *IdentityAuditLogActorRows) LoadByIDs(
func (i *Identities) LoadByIDs(
ctx context.Context,
conn pg.Querier,
identityIDs []gid.GID,
@@ -407,7 +397,13 @@ func (rows *IdentityAuditLogActorRows) LoadByIDs(
SELECT
id,
email_address,
full_name
full_name,
hashed_password,
email_address_verified,
saml_subject,
locale,
created_at,
updated_at
FROM
identities
WHERE
@@ -416,17 +412,17 @@ WHERE
args := pgx.StrictNamedArgs{"identity_ids": identityIDs}
rowsResult, err := conn.Query(ctx, q, args)
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query identity audit log actors: %w", err)
return fmt.Errorf("cannot query identities: %w", err)
}
actors, err := pgx.CollectRows(rowsResult, pgx.RowToAddrOfStructByName[IdentityAuditLogActorRow])
identities, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Identity])
if err != nil {
return fmt.Errorf("cannot collect identity audit log actors: %w", err)
return fmt.Errorf("cannot collect identities: %w", err)
}
*rows = actors
*i = identities
return nil
}

View File

@@ -495,25 +495,7 @@ WHERE
return nil
}
type (
MembershipProfileByUserNameRow struct {
UserName *string `db:"user_name"`
FullName string `db:"full_name"`
FormattedName *string `db:"formatted_name"`
}
MembershipProfileByUserNameRows []*MembershipProfileByUserNameRow
)
func (p *MembershipProfileByUserNameRow) DisplayName() string {
if p.FormattedName != nil && *p.FormattedName != "" {
return *p.FormattedName
}
return p.FullName
}
func (rows *MembershipProfileByUserNameRows) LoadByOrganizationIDAndUserNames(
func (p *MembershipProfiles) LoadByOrganizationIDAndUserNames(
ctx context.Context,
conn pg.Querier,
scope Scoper,
@@ -522,9 +504,40 @@ func (rows *MembershipProfileByUserNameRows) LoadByOrganizationIDAndUserNames(
) error {
q := `
SELECT
p.user_name,
p.id,
p.identity_id,
p.organization_id,
''::citext AS email_address,
p.source,
p.state,
p.full_name,
p.formatted_name
p.kind,
p.additional_email_addresses,
p.position,
p.contract_start_date,
p.contract_end_date,
'' AS organization_name,
p.user_name,
p.external_id,
p.nickname,
p.locale,
p.timezone,
p.profile_url,
p.preferred_language,
p.given_name,
p.family_name,
p.formatted_name,
p.middle_name,
p.honorific_prefix,
p.honorific_suffix,
p.employee_number,
p.department,
p.cost_center,
p.enterprise_organization,
p.division,
p.manager_value,
p.created_at,
p.updated_at
FROM
iam_membership_profiles p
WHERE
@@ -541,17 +554,17 @@ WHERE
}
maps.Copy(args, scope.SQLArguments())
rowsResult, err := conn.Query(ctx, q, args)
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query profiles by user names: %w", err)
}
profiles, err := pgx.CollectRows(rowsResult, pgx.RowToAddrOfStructByName[MembershipProfileByUserNameRow])
profiles, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[MembershipProfile])
if err != nil {
return fmt.Errorf("cannot collect profiles by user names: %w", err)
}
*rows = profiles
*p = profiles
return nil
}

View File

@@ -302,16 +302,7 @@ WHERE
return nil
}
type (
PersonalAPIKeyAuditLogActorRow struct {
ID gid.GID `db:"id"`
Name string `db:"name"`
}
PersonalAPIKeyAuditLogActorRows []*PersonalAPIKeyAuditLogActorRow
)
func (rows *PersonalAPIKeyAuditLogActorRows) LoadByIDs(
func (a *PersonalAPIKeys) LoadByIDs(
ctx context.Context,
conn pg.Querier,
apiKeyIDs []gid.GID,
@@ -319,7 +310,13 @@ func (rows *PersonalAPIKeyAuditLogActorRows) LoadByIDs(
q := `
SELECT
id,
name
identity_id,
name,
expires_at,
expire_reason,
last_used_at,
created_at,
updated_at
FROM
iam_personal_api_keys
WHERE
@@ -328,17 +325,17 @@ WHERE
args := pgx.StrictNamedArgs{"api_key_ids": apiKeyIDs}
rowsResult, err := conn.Query(ctx, q, args)
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query personal api key audit log actors: %w", err)
return fmt.Errorf("cannot query personal api keys: %w", err)
}
actors, err := pgx.CollectRows(rowsResult, pgx.RowToAddrOfStructByName[PersonalAPIKeyAuditLogActorRow])
apiKeys, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[PersonalAPIKey])
if err != nil {
return fmt.Errorf("cannot collect personal api key audit log actors: %w", err)
return fmt.Errorf("cannot collect personal api keys: %w", err)
}
*rows = actors
*a = apiKeys
return nil
}

View File

@@ -270,7 +270,7 @@ func loadAuditLogActorExportInfo(
result := make(map[gid.GID]auditLogActorExportInfo)
var identities coredata.IdentityAuditLogActorRows
var identities coredata.Identities
if err := identities.LoadByIDs(ctx, conn, identityIDs); err != nil {
return nil, fmt.Errorf("cannot load audit log actor identities: %w", err)
}
@@ -282,7 +282,7 @@ func loadAuditLogActorExportInfo(
}
}
var apiKeys coredata.PersonalAPIKeyAuditLogActorRows
var apiKeys coredata.PersonalAPIKeys
if err := apiKeys.LoadByIDs(ctx, conn, apiKeyIDs); err != nil {
return nil, fmt.Errorf("cannot load audit log actor API keys: %w", err)
}
@@ -308,7 +308,7 @@ func loadSCIMProfileExportInfo(
return map[string]scimProfileExportInfo{}, nil
}
var profiles coredata.MembershipProfileByUserNameRows
var profiles coredata.MembershipProfiles
if err := profiles.LoadByOrganizationIDAndUserNames(
ctx,
conn,
@@ -328,7 +328,7 @@ func loadSCIMProfileExportInfo(
key := strings.ToLower(*profile.UserName)
result[key] = scimProfileExportInfo{
email: scimEmailFromUserName(*profile.UserName),
fullName: profile.DisplayName(),
fullName: profileFullName(profile),
}
}
@@ -379,6 +379,14 @@ func scimEmailFromUserName(userName string) string {
return ""
}
func profileFullName(profile *coredata.MembershipProfile) string {
if profile.FormattedName != nil && *profile.FormattedName != "" {
return *profile.FormattedName
}
return profile.FullName
}
func stringPtrValue(value *string) string {
if value == nil {
return ""