feat(store): queries for data exports and erasure
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
This commit is contained in:
1 parent
358b191976
commit
4182097542
4 files changed
+658
No files matched your search
@@ -0,0 +1,26 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- Check results carry no foreign key to orgs (hypertables with row-level
|
||||||
|
-- security cannot be compressed), so deleting an organization removes them
|
||||||
|
-- here, including rows already materialized into the aggregates. Runs with
|
||||||
|
-- the owner's rights because the application role may not reach
|
||||||
|
-- TimescaleDB's internal schema.
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE FUNCTION purge_org_check_data(org uuid) RETURNS void
|
||||||
|
LANGUAGE plpgsql SECURITY DEFINER SET search_path = public, pg_temp AS $$
|
||||||
|
DECLARE
|
||||||
|
agg record;
|
||||||
|
BEGIN
|
||||||
|
DELETE FROM check_results WHERE org_id = org;
|
||||||
|
FOR agg IN
|
||||||
|
SELECT materialization_hypertable_schema AS s, materialization_hypertable_name AS t
|
||||||
|
FROM timescaledb_information.continuous_aggregates
|
||||||
|
WHERE view_name IN ('check_results_1h', 'check_results_1d')
|
||||||
|
LOOP
|
||||||
|
EXECUTE format('DELETE FROM %I.%I WHERE org_id = $1', agg.s, agg.t) USING org;
|
||||||
|
END LOOP;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP FUNCTION purge_org_check_data(uuid);
|
||||||
@@ -0,0 +1,531 @@
|
|||||||
|
// 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
|
||||||
|
}
|
||||||
@@ -43,15 +43,18 @@ type Querier interface {
|
|||||||
DailyBuckets(ctx context.Context, arg DailyBucketsParams) ([]DailyBucketsRow, error)
|
DailyBuckets(ctx context.Context, arg DailyBucketsParams) ([]DailyBucketsRow, error)
|
||||||
DailyBucketsForMonitors(ctx context.Context, arg DailyBucketsForMonitorsParams) ([]DailyBucketsForMonitorsRow, error)
|
DailyBucketsForMonitors(ctx context.Context, arg DailyBucketsForMonitorsParams) ([]DailyBucketsForMonitorsRow, error)
|
||||||
DeleteAPIToken(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteAPIToken(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
|
DeleteAccountAudit(ctx context.Context, actorUserID *uuid.UUID) error
|
||||||
DeleteAgent(ctx context.Context, arg DeleteAgentParams) (int64, error)
|
DeleteAgent(ctx context.Context, arg DeleteAgentParams) (int64, error)
|
||||||
DeleteAssignment(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteAssignment(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteAssignmentsForResource(ctx context.Context, arg DeleteAssignmentsForResourceParams) error
|
DeleteAssignmentsForResource(ctx context.Context, arg DeleteAssignmentsForResourceParams) error
|
||||||
DeleteChannel(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteChannel(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteGroup(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteGroup(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteInvitation(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteInvitation(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
|
DeleteInvitationsForEmail(ctx context.Context, email string) error
|
||||||
DeleteMonitor(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteMonitor(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error
|
DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error
|
||||||
DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
|
DeleteOrgStorage(ctx context.Context, orgID uuid.UUID) error
|
||||||
DeleteRecoveryCodes(ctx context.Context, userID uuid.UUID) error
|
DeleteRecoveryCodes(ctx context.Context, userID uuid.UUID) error
|
||||||
DeleteRegion(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteRegion(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteRole(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteRole(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
@@ -71,6 +74,9 @@ type Querier interface {
|
|||||||
// When each monitor and region first reported since $2, exactly. Used for
|
// When each monitor and region first reported since $2, exactly. Used for
|
||||||
// data younger than the daily aggregate's resolution.
|
// data younger than the daily aggregate's resolution.
|
||||||
FirstDataRecent(ctx context.Context, arg FirstDataRecentParams) ([]FirstDataRecentRow, error)
|
FirstDataRecent(ctx context.Context, arg FirstDataRecentParams) ([]FirstDataRecentRow, error)
|
||||||
|
// Keeps organization and platform entries readable as "deleted user" but
|
||||||
|
// drops who it was.
|
||||||
|
ForgetAuditActor(ctx context.Context, actorUserID *uuid.UUID) error
|
||||||
GetAPIToken(ctx context.Context, id uuid.UUID) (ApiToken, error)
|
GetAPIToken(ctx context.Context, id uuid.UUID) (ApiToken, error)
|
||||||
GetAPITokenByHash(ctx context.Context, tokenHash []byte) (ApiToken, error)
|
GetAPITokenByHash(ctx context.Context, tokenHash []byte) (ApiToken, error)
|
||||||
// System scope.
|
// System scope.
|
||||||
@@ -154,7 +160,14 @@ type Querier interface {
|
|||||||
NotificationDetails(ctx context.Context, ids []uuid.UUID) ([]NotificationDetailsRow, error)
|
NotificationDetails(ctx context.Context, ids []uuid.UUID) ([]NotificationDetailsRow, error)
|
||||||
OpenIncident(ctx context.Context, arg OpenIncidentParams) (Incident, error)
|
OpenIncident(ctx context.Context, arg OpenIncidentParams) (Incident, error)
|
||||||
OpenIncidents(ctx context.Context) ([]Incident, error)
|
OpenIncidents(ctx context.Context) ([]Incident, error)
|
||||||
|
OrgAssignmentsExport(ctx context.Context, orgID uuid.UUID) ([]OrgAssignmentsExportRow, error)
|
||||||
|
OrgAuditExport(ctx context.Context, orgID uuid.UUID) ([]OrgAuditExportRow, error)
|
||||||
|
OrgDailyStats(ctx context.Context, orgID uuid.UUID) ([]OrgDailyStatsRow, error)
|
||||||
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
||||||
|
OrgIncidentsExport(ctx context.Context, orgID uuid.UUID) ([]OrgIncidentsExportRow, error)
|
||||||
|
OrgMembersExport(ctx context.Context, orgID uuid.UUID) ([]OrgMembersExportRow, error)
|
||||||
|
// Organizations user owns, with their member count.
|
||||||
|
OwnedOrgs(ctx context.Context, userID uuid.UUID) ([]OwnedOrgsRow, error)
|
||||||
PurgeAuditEvents(ctx context.Context, argTime time.Time) (int64, error)
|
PurgeAuditEvents(ctx context.Context, argTime time.Time) (int64, error)
|
||||||
PurgeExpiredTokens(ctx context.Context, expiresAt *time.Time) (int64, error)
|
PurgeExpiredTokens(ctx context.Context, expiresAt *time.Time) (int64, error)
|
||||||
// Incidents resolved before the oldest data still charted.
|
// Incidents resolved before the oldest data still charted.
|
||||||
@@ -162,6 +175,7 @@ type Querier interface {
|
|||||||
PurgeInvitations(ctx context.Context, expiresAt time.Time) (int64, error)
|
PurgeInvitations(ctx context.Context, expiresAt time.Time) (int64, error)
|
||||||
// Delivered and given-up notifications; pending ones are still being worked.
|
// Delivered and given-up notifications; pending ones are still being worked.
|
||||||
PurgeNotifications(ctx context.Context, createdAt time.Time) (int64, error)
|
PurgeNotifications(ctx context.Context, createdAt time.Time) (int64, error)
|
||||||
|
PurgeOrgCheckData(ctx context.Context, org uuid.UUID) error
|
||||||
// Raw results bucketed by width since a point in time, for short ranges.
|
// Raw results bucketed by width since a point in time, for short ranges.
|
||||||
RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]RangeBucketsRow, error)
|
RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]RangeBucketsRow, error)
|
||||||
// The daily aggregate rebucketed by width, for ranges of months.
|
// The daily aggregate rebucketed by width, for ranges of months.
|
||||||
@@ -178,6 +192,7 @@ type Querier interface {
|
|||||||
// Moves the subgroups of a group that is about to be deleted.
|
// Moves the subgroups of a group that is about to be deleted.
|
||||||
ReparentGroups(ctx context.Context, arg ReparentGroupsParams) error
|
ReparentGroups(ctx context.Context, arg ReparentGroupsParams) error
|
||||||
ResolveIncident(ctx context.Context, arg ResolveIncidentParams) (Incident, error)
|
ResolveIncident(ctx context.Context, arg ResolveIncidentParams) (Incident, error)
|
||||||
|
ScrubAuditEmail(ctx context.Context, email string) error
|
||||||
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
||||||
// Changing the domain drops a previous verification.
|
// Changing the domain drops a previous verification.
|
||||||
SetStatusPageDomain(ctx context.Context, arg SetStatusPageDomainParams) (StatusPage, error)
|
SetStatusPageDomain(ctx context.Context, arg SetStatusPageDomainParams) (StatusPage, error)
|
||||||
@@ -203,6 +218,12 @@ type Querier interface {
|
|||||||
// materialized most recent hours (real-time aggregation).
|
// materialized most recent hours (real-time aggregation).
|
||||||
UptimeSince(ctx context.Context, arg UptimeSinceParams) ([]UptimeSinceRow, error)
|
UptimeSince(ctx context.Context, arg UptimeSinceParams) ([]UptimeSinceRow, error)
|
||||||
UseRecoveryCode(ctx context.Context, arg UseRecoveryCodeParams) (int64, error)
|
UseRecoveryCode(ctx context.Context, arg UseRecoveryCodeParams) (int64, error)
|
||||||
|
// Everything the user did, in any organization, and their own account log.
|
||||||
|
UserAuditEvents(ctx context.Context, actorUserID *uuid.UUID) ([]UserAuditEventsRow, error)
|
||||||
|
UserIdentities(ctx context.Context, userID uuid.UUID) ([]UserIdentitiesRow, error)
|
||||||
|
UserMemberships(ctx context.Context, userID uuid.UUID) ([]UserMembershipsRow, error)
|
||||||
|
UserRoleNames(ctx context.Context, userID uuid.UUID) ([]UserRoleNamesRow, error)
|
||||||
|
UserTokens(ctx context.Context, userID uuid.UUID) ([]UserTokensRow, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Querier = (*Queries)(nil)
|
var _ Querier = (*Queries)(nil)
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
-- name: OwnedOrgs :many
|
||||||
|
-- Organizations user owns, with their member count.
|
||||||
|
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;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- name: UserAuditEvents :many
|
||||||
|
-- Everything the user did, in any organization, and their own account log.
|
||||||
|
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;
|
||||||
|
|
||||||
|
-- name: DeleteAccountAudit :exec
|
||||||
|
DELETE FROM audit_events WHERE kind = 'user' AND actor_user_id = $1;
|
||||||
|
|
||||||
|
-- name: ForgetAuditActor :exec
|
||||||
|
-- Keeps organization and platform entries readable as "deleted user" but
|
||||||
|
-- drops who it was.
|
||||||
|
UPDATE audit_events
|
||||||
|
SET actor_user_id = NULL, details = details || '{"actor_deleted": true}'
|
||||||
|
WHERE actor_user_id = $1;
|
||||||
|
|
||||||
|
-- name: ScrubAuditEmail :exec
|
||||||
|
UPDATE audit_events SET details = details - 'email' WHERE lower(details->>'email') = lower(sqlc.arg(email)::text);
|
||||||
|
|
||||||
|
-- name: DeleteInvitationsForEmail :exec
|
||||||
|
DELETE FROM invitations WHERE email = $1;
|
||||||
|
|
||||||
|
-- name: PurgeOrgCheckData :exec
|
||||||
|
SELECT purge_org_check_data($1);
|
||||||
|
|
||||||
|
-- name: DeleteOrgStorage :exec
|
||||||
|
DELETE FROM agent_storage WHERE owner = 'org:' || sqlc.arg(org_id)::uuid::text;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- 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 = sqlc.arg(org_id)::uuid ORDER BY e.time;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
Reference in new issue
Block a user