purge_org_check_data removes an organization's check results including rows already in the aggregates; it runs with owner rights since the application role cannot reach TimescaleDB's internal schema. Refs #66 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
532 lines
12 KiB
Go
532 lines
12 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: gdpr.sql
|
|
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const deleteAccountAudit = `-- name: DeleteAccountAudit :exec
|
|
DELETE FROM audit_events WHERE kind = 'user' AND actor_user_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteAccountAudit(ctx context.Context, actorUserID *uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteAccountAudit, actorUserID)
|
|
return err
|
|
}
|
|
|
|
const deleteInvitationsForEmail = `-- name: DeleteInvitationsForEmail :exec
|
|
DELETE FROM invitations WHERE email = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteInvitationsForEmail(ctx context.Context, email string) error {
|
|
_, err := q.db.Exec(ctx, deleteInvitationsForEmail, email)
|
|
return err
|
|
}
|
|
|
|
const deleteOrgStorage = `-- name: DeleteOrgStorage :exec
|
|
DELETE FROM agent_storage WHERE owner = 'org:' || $1::uuid::text
|
|
`
|
|
|
|
func (q *Queries) DeleteOrgStorage(ctx context.Context, orgID uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteOrgStorage, orgID)
|
|
return err
|
|
}
|
|
|
|
const forgetAuditActor = `-- name: ForgetAuditActor :exec
|
|
UPDATE audit_events
|
|
SET actor_user_id = NULL, details = details || '{"actor_deleted": true}'
|
|
WHERE actor_user_id = $1
|
|
`
|
|
|
|
// Keeps organization and platform entries readable as "deleted user" but
|
|
// drops who it was.
|
|
func (q *Queries) ForgetAuditActor(ctx context.Context, actorUserID *uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, forgetAuditActor, actorUserID)
|
|
return err
|
|
}
|
|
|
|
const orgAssignmentsExport = `-- name: OrgAssignmentsExport :many
|
|
SELECT ra.user_id, r.name AS role, ra.resource_type, ra.resource_id
|
|
FROM role_assignments ra JOIN roles r ON r.id = ra.role_id
|
|
WHERE ra.org_id = $1
|
|
`
|
|
|
|
type OrgAssignmentsExportRow struct {
|
|
UserID uuid.UUID
|
|
Role string
|
|
ResourceType *string
|
|
ResourceID *uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) OrgAssignmentsExport(ctx context.Context, orgID uuid.UUID) ([]OrgAssignmentsExportRow, error) {
|
|
rows, err := q.db.Query(ctx, orgAssignmentsExport, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []OrgAssignmentsExportRow{}
|
|
for rows.Next() {
|
|
var i OrgAssignmentsExportRow
|
|
if err := rows.Scan(
|
|
&i.UserID,
|
|
&i.Role,
|
|
&i.ResourceType,
|
|
&i.ResourceID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const orgAuditExport = `-- name: OrgAuditExport :many
|
|
SELECT e.time, e.action, coalesce(u.email::text, '')::text AS actor, e.target_id, e.details, e.status,
|
|
coalesce(host(e.ip), '')::text AS ip
|
|
FROM audit_events e LEFT JOIN users u ON u.id = e.actor_user_id
|
|
WHERE e.kind = 'org' AND e.org_id = $1::uuid ORDER BY e.time
|
|
`
|
|
|
|
type OrgAuditExportRow struct {
|
|
Time time.Time
|
|
Action string
|
|
Actor string
|
|
TargetID *uuid.UUID
|
|
Details []byte
|
|
Status int32
|
|
Ip string
|
|
}
|
|
|
|
func (q *Queries) OrgAuditExport(ctx context.Context, orgID uuid.UUID) ([]OrgAuditExportRow, error) {
|
|
rows, err := q.db.Query(ctx, orgAuditExport, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []OrgAuditExportRow{}
|
|
for rows.Next() {
|
|
var i OrgAuditExportRow
|
|
if err := rows.Scan(
|
|
&i.Time,
|
|
&i.Action,
|
|
&i.Actor,
|
|
&i.TargetID,
|
|
&i.Details,
|
|
&i.Status,
|
|
&i.Ip,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const orgDailyStats = `-- name: OrgDailyStats :many
|
|
SELECT monitor_id, region, bucket::timestamptz AS day, total::bigint AS total, ok_count::bigint AS ok_count, avg_latency_ms::real AS avg_latency_ms
|
|
FROM check_results_1d WHERE org_id = $1 ORDER BY monitor_id, region, day
|
|
`
|
|
|
|
type OrgDailyStatsRow struct {
|
|
MonitorID uuid.UUID
|
|
Region string
|
|
Day time.Time
|
|
Total int64
|
|
OkCount int64
|
|
AvgLatencyMs float32
|
|
}
|
|
|
|
func (q *Queries) OrgDailyStats(ctx context.Context, orgID uuid.UUID) ([]OrgDailyStatsRow, error) {
|
|
rows, err := q.db.Query(ctx, orgDailyStats, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []OrgDailyStatsRow{}
|
|
for rows.Next() {
|
|
var i OrgDailyStatsRow
|
|
if err := rows.Scan(
|
|
&i.MonitorID,
|
|
&i.Region,
|
|
&i.Day,
|
|
&i.Total,
|
|
&i.OkCount,
|
|
&i.AvgLatencyMs,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const orgIncidentsExport = `-- name: OrgIncidentsExport :many
|
|
SELECT i.id, i.monitor_id, m.name AS monitor, i.started_at, i.resolved_at, i.cause
|
|
FROM incidents i LEFT JOIN monitors m ON m.id = i.monitor_id
|
|
WHERE i.org_id = $1 ORDER BY i.started_at
|
|
`
|
|
|
|
type OrgIncidentsExportRow struct {
|
|
ID uuid.UUID
|
|
MonitorID uuid.UUID
|
|
Monitor *string
|
|
StartedAt time.Time
|
|
ResolvedAt *time.Time
|
|
Cause []byte
|
|
}
|
|
|
|
func (q *Queries) OrgIncidentsExport(ctx context.Context, orgID uuid.UUID) ([]OrgIncidentsExportRow, error) {
|
|
rows, err := q.db.Query(ctx, orgIncidentsExport, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []OrgIncidentsExportRow{}
|
|
for rows.Next() {
|
|
var i OrgIncidentsExportRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.MonitorID,
|
|
&i.Monitor,
|
|
&i.StartedAt,
|
|
&i.ResolvedAt,
|
|
&i.Cause,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const orgMembersExport = `-- name: OrgMembersExport :many
|
|
SELECT u.id, u.email::text AS email, u.display_name, m.is_owner, m.created_at
|
|
FROM memberships m JOIN users u ON u.id = m.user_id
|
|
WHERE m.org_id = $1 ORDER BY u.email
|
|
`
|
|
|
|
type OrgMembersExportRow struct {
|
|
ID uuid.UUID
|
|
Email string
|
|
DisplayName string
|
|
IsOwner bool
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) OrgMembersExport(ctx context.Context, orgID uuid.UUID) ([]OrgMembersExportRow, error) {
|
|
rows, err := q.db.Query(ctx, orgMembersExport, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []OrgMembersExportRow{}
|
|
for rows.Next() {
|
|
var i OrgMembersExportRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Email,
|
|
&i.DisplayName,
|
|
&i.IsOwner,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ownedOrgs = `-- name: OwnedOrgs :many
|
|
SELECT o.id, o.slug, o.name,
|
|
(SELECT count(*) FROM memberships m2 WHERE m2.org_id = o.id)::bigint AS members
|
|
FROM orgs o JOIN memberships m ON m.org_id = o.id
|
|
WHERE m.user_id = $1 AND m.is_owner
|
|
ORDER BY o.name
|
|
`
|
|
|
|
type OwnedOrgsRow struct {
|
|
ID uuid.UUID
|
|
Slug string
|
|
Name string
|
|
Members int64
|
|
}
|
|
|
|
// Organizations user owns, with their member count.
|
|
func (q *Queries) OwnedOrgs(ctx context.Context, userID uuid.UUID) ([]OwnedOrgsRow, error) {
|
|
rows, err := q.db.Query(ctx, ownedOrgs, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []OwnedOrgsRow{}
|
|
for rows.Next() {
|
|
var i OwnedOrgsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Slug,
|
|
&i.Name,
|
|
&i.Members,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const purgeOrgCheckData = `-- name: PurgeOrgCheckData :exec
|
|
SELECT purge_org_check_data($1)
|
|
`
|
|
|
|
func (q *Queries) PurgeOrgCheckData(ctx context.Context, org uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, purgeOrgCheckData, org)
|
|
return err
|
|
}
|
|
|
|
const scrubAuditEmail = `-- name: ScrubAuditEmail :exec
|
|
UPDATE audit_events SET details = details - 'email' WHERE lower(details->>'email') = lower($1::text)
|
|
`
|
|
|
|
func (q *Queries) ScrubAuditEmail(ctx context.Context, email string) error {
|
|
_, err := q.db.Exec(ctx, scrubAuditEmail, email)
|
|
return err
|
|
}
|
|
|
|
const userAuditEvents = `-- name: UserAuditEvents :many
|
|
SELECT time, kind, org_id, action, target_id, details, status, coalesce(host(ip), '')::text AS ip,
|
|
coalesce(user_agent, '') AS user_agent
|
|
FROM audit_events WHERE actor_user_id = $1 ORDER BY time
|
|
`
|
|
|
|
type UserAuditEventsRow struct {
|
|
Time time.Time
|
|
Kind string
|
|
OrgID *uuid.UUID
|
|
Action string
|
|
TargetID *uuid.UUID
|
|
Details []byte
|
|
Status int32
|
|
Ip string
|
|
UserAgent string
|
|
}
|
|
|
|
// Everything the user did, in any organization, and their own account log.
|
|
func (q *Queries) UserAuditEvents(ctx context.Context, actorUserID *uuid.UUID) ([]UserAuditEventsRow, error) {
|
|
rows, err := q.db.Query(ctx, userAuditEvents, actorUserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []UserAuditEventsRow{}
|
|
for rows.Next() {
|
|
var i UserAuditEventsRow
|
|
if err := rows.Scan(
|
|
&i.Time,
|
|
&i.Kind,
|
|
&i.OrgID,
|
|
&i.Action,
|
|
&i.TargetID,
|
|
&i.Details,
|
|
&i.Status,
|
|
&i.Ip,
|
|
&i.UserAgent,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const userIdentities = `-- name: UserIdentities :many
|
|
SELECT p.name AS provider, i.org_id, i.subject, i.email, i.created_at, i.last_login_at
|
|
FROM user_identities i JOIN sso_providers p ON p.id = i.provider_id
|
|
WHERE i.user_id = $1
|
|
`
|
|
|
|
type UserIdentitiesRow struct {
|
|
Provider string
|
|
OrgID uuid.UUID
|
|
Subject string
|
|
Email *string
|
|
CreatedAt time.Time
|
|
LastLoginAt time.Time
|
|
}
|
|
|
|
func (q *Queries) UserIdentities(ctx context.Context, userID uuid.UUID) ([]UserIdentitiesRow, error) {
|
|
rows, err := q.db.Query(ctx, userIdentities, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []UserIdentitiesRow{}
|
|
for rows.Next() {
|
|
var i UserIdentitiesRow
|
|
if err := rows.Scan(
|
|
&i.Provider,
|
|
&i.OrgID,
|
|
&i.Subject,
|
|
&i.Email,
|
|
&i.CreatedAt,
|
|
&i.LastLoginAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const userMemberships = `-- name: UserMemberships :many
|
|
SELECT o.id, o.slug, o.name, m.is_owner, m.created_at
|
|
FROM memberships m JOIN orgs o ON o.id = m.org_id
|
|
WHERE m.user_id = $1
|
|
ORDER BY o.name
|
|
`
|
|
|
|
type UserMembershipsRow struct {
|
|
ID uuid.UUID
|
|
Slug string
|
|
Name string
|
|
IsOwner bool
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) UserMemberships(ctx context.Context, userID uuid.UUID) ([]UserMembershipsRow, error) {
|
|
rows, err := q.db.Query(ctx, userMemberships, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []UserMembershipsRow{}
|
|
for rows.Next() {
|
|
var i UserMembershipsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Slug,
|
|
&i.Name,
|
|
&i.IsOwner,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const userRoleNames = `-- name: UserRoleNames :many
|
|
SELECT ra.org_id, r.name, ra.resource_type, ra.resource_id
|
|
FROM role_assignments ra JOIN roles r ON r.id = ra.role_id
|
|
WHERE ra.user_id = $1
|
|
`
|
|
|
|
type UserRoleNamesRow struct {
|
|
OrgID uuid.UUID
|
|
Name string
|
|
ResourceType *string
|
|
ResourceID *uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) UserRoleNames(ctx context.Context, userID uuid.UUID) ([]UserRoleNamesRow, error) {
|
|
rows, err := q.db.Query(ctx, userRoleNames, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []UserRoleNamesRow{}
|
|
for rows.Next() {
|
|
var i UserRoleNamesRow
|
|
if err := rows.Scan(
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.ResourceType,
|
|
&i.ResourceID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const userTokens = `-- name: UserTokens :many
|
|
SELECT org_id, name, prefix, permissions, expires_at, last_used_at, created_at
|
|
FROM api_tokens WHERE user_id = $1 ORDER BY created_at
|
|
`
|
|
|
|
type UserTokensRow struct {
|
|
OrgID uuid.UUID
|
|
Name string
|
|
Prefix string
|
|
Permissions []string
|
|
ExpiresAt *time.Time
|
|
LastUsedAt *time.Time
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) UserTokens(ctx context.Context, userID uuid.UUID) ([]UserTokensRow, error) {
|
|
rows, err := q.db.Query(ctx, userTokens, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []UserTokensRow{}
|
|
for rows.Next() {
|
|
var i UserTokensRow
|
|
if err := rows.Scan(
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Prefix,
|
|
&i.Permissions,
|
|
&i.ExpiresAt,
|
|
&i.LastUsedAt,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|