-- +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);