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
27 lines
1010 B
PL/PgSQL
27 lines
1010 B
PL/PgSQL
-- +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);
|