From 8f462c88d301c26f8861a16cb43a715126cba2f0 Mon Sep 17 00:00:00 2001 From: NISHIZAWA Shuntaro Date: Sun, 10 Nov 2024 07:01:40 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=83=A9=E3=83=B3=E3=82=AD=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=83=93=E3=83=A5=E3=83=BC=E3=81=AE=E5=AE=9A=E7=BE=A9?= =?UTF-8?q?=E3=82=92=E3=83=9F=E3=82=B9=E3=81=A3=E3=81=A6=E3=81=9F=E3=81=AE?= =?UTF-8?q?=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...241109220031_fix_filter_guest_on_stats.sql | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 supabase/migrations/20241109220031_fix_filter_guest_on_stats.sql diff --git a/supabase/migrations/20241109220031_fix_filter_guest_on_stats.sql b/supabase/migrations/20241109220031_fix_filter_guest_on_stats.sql new file mode 100644 index 0000000..894542c --- /dev/null +++ b/supabase/migrations/20241109220031_fix_filter_guest_on_stats.sql @@ -0,0 +1,40 @@ +-- ゲストを除外したプロフィールのステータスを取得するビューを作成する +DROP VIEW public.profiles_with_stats; + +CREATE VIEW public.profiles_with_stats +WITH (security_invoker = TRUE) +AS +SELECT + profiles.*, + CASE + WHEN profiles.is_guest = TRUE THEN NULL + ELSE player_stats.high_score + END AS high_score, + CASE + WHEN profiles.is_guest = TRUE THEN 0 + ELSE player_stats.play_count + END AS play_count, + CASE + WHEN profiles.is_guest = TRUE THEN NULL + ELSE player_stats.rank + END AS rank +FROM + public.profiles +LEFT JOIN ( + SELECT + profiles.id AS profile_id, + max(players.score) AS high_score, + count(players.id) AS play_count, + CASE WHEN max(players.score) IS NULL + THEN NULL + ELSE rank() OVER (ORDER BY max(players.score) DESC NULLS LAST) + END AS rank + FROM + public.profiles + LEFT JOIN + public.players ON profiles.user_id = players.user_id + WHERE + profiles.is_guest = FALSE + GROUP BY + profiles.id +) AS player_stats ON profiles.id = player_stats.profile_id;