186_group_auth_cache_image_generation.sql 990 B

12345678910111213141516171819202122232425262728293031
  1. -- Group image-generation permission is part of the API-key auth snapshot.
  2. -- Extend the existing durable invalidation trigger without changing migration 184.
  3. CREATE OR REPLACE FUNCTION enqueue_group_auth_cache_invalidation()
  4. RETURNS TRIGGER
  5. LANGUAGE plpgsql
  6. AS $$
  7. DECLARE
  8. target_group_id BIGINT;
  9. BEGIN
  10. target_group_id := OLD.id;
  11. IF TG_OP = 'UPDATE'
  12. AND OLD.status IS NOT DISTINCT FROM NEW.status
  13. AND OLD.is_exclusive IS NOT DISTINCT FROM NEW.is_exclusive
  14. AND OLD.allow_image_generation IS NOT DISTINCT FROM NEW.allow_image_generation
  15. AND OLD.deleted_at IS NOT DISTINCT FROM NEW.deleted_at THEN
  16. RETURN NEW;
  17. END IF;
  18. INSERT INTO auth_cache_invalidation_outbox (cache_key)
  19. SELECT encode(sha256(convert_to(k.key, 'UTF8')), 'hex')
  20. FROM api_keys AS k
  21. WHERE k.group_id = target_group_id
  22. AND k.deleted_at IS NULL
  23. AND k.key <> '';
  24. IF TG_OP = 'DELETE' THEN
  25. RETURN OLD;
  26. END IF;
  27. RETURN NEW;
  28. END;
  29. $$;