From 5dab9b62504ca0365bd47a1a080f86d13668194c Mon Sep 17 00:00:00 2001 From: Lim Ding Wen Date: Wed, 7 Aug 2024 18:26:17 +0800 Subject: [PATCH] Add ability to refresh debate bill match view via endpoint --- .../refresh-debate-bill-match-view.sh | 11 +++++++++ site/src/utils/supabase/database.types.ts | 7 +++++- supabase/functions/database.types.ts | 7 +++++- .../refresh-debate-bill-match-view.ts | 23 +++++++++++++++++++ .../refresh-debate-bill-match-view/index.ts | 5 ++++ supabase/schema.sql | 2 +- 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 scripts/deploy-and-run/refresh-debate-bill-match-view.sh create mode 100644 supabase/functions/lib/shared-functions/refresh-debate-bill-match-view.ts create mode 100644 supabase/functions/refresh-debate-bill-match-view/index.ts diff --git a/scripts/deploy-and-run/refresh-debate-bill-match-view.sh b/scripts/deploy-and-run/refresh-debate-bill-match-view.sh new file mode 100644 index 0000000..9e06b45 --- /dev/null +++ b/scripts/deploy-and-run/refresh-debate-bill-match-view.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +source ../.env +source ../lib/functions/deploy.sh +source ../lib/functions/run.sh + +FUNCTION_NAME="refresh-debate-bill-match-view" + +deploy "$SUPABASE_PROJECT_ID" "$FUNCTION_NAME" +run "$SUPABASE_PROJECT_ID" "$SUPABASE_SERVICE_ROLE_KEY" "$FUNCTION_NAME" '{}' \ No newline at end of file diff --git a/site/src/utils/supabase/database.types.ts b/site/src/utils/supabase/database.types.ts index 43a099f..fbdc959 100644 --- a/site/src/utils/supabase/database.types.ts +++ b/site/src/utils/supabase/database.types.ts @@ -403,7 +403,12 @@ export type Database = { } } Functions: { - [_ in never]: never + refresh_materialized_view: { + Args: { + view_name: string + } + Returns: undefined + } } Enums: { [_ in never]: never diff --git a/supabase/functions/database.types.ts b/supabase/functions/database.types.ts index 43a099f..fbdc959 100644 --- a/supabase/functions/database.types.ts +++ b/supabase/functions/database.types.ts @@ -403,7 +403,12 @@ export type Database = { } } Functions: { - [_ in never]: never + refresh_materialized_view: { + Args: { + view_name: string + } + Returns: undefined + } } Enums: { [_ in never]: never diff --git a/supabase/functions/lib/shared-functions/refresh-debate-bill-match-view.ts b/supabase/functions/lib/shared-functions/refresh-debate-bill-match-view.ts new file mode 100644 index 0000000..99e4373 --- /dev/null +++ b/supabase/functions/lib/shared-functions/refresh-debate-bill-match-view.ts @@ -0,0 +1,23 @@ +import { createSupabase } from "../utils/create-supabase.ts"; +import { isAdmin } from "../utils/check-admin.ts"; +import buildResponseProxy from "../utils/build-response-proxy.ts"; + +async function refresh() { + const supabase = createSupabase(); + const { error } = await supabase.rpc("refresh_materialized_view", { + view_name: "public.debate_bill_match_view", + }); + if (error) throw error; +} + +export default async function refreshDebateBillMatchView(req: Request) { + if (!isAdmin(req)) { + return buildResponseProxy({ message: "Unauthorised." }, 401); + } + + await refresh(); + + return buildResponseProxy({ + message: "Refreshed debate_bill_match_view.", + }); +} diff --git a/supabase/functions/refresh-debate-bill-match-view/index.ts b/supabase/functions/refresh-debate-bill-match-view/index.ts new file mode 100644 index 0000000..69dbe57 --- /dev/null +++ b/supabase/functions/refresh-debate-bill-match-view/index.ts @@ -0,0 +1,5 @@ +import "https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts"; +import proxyToResponseWrapper from "../lib/utils/proxy-to-response-wrapper.ts"; +import refreshDebateBillMatchView from "../lib/shared-functions/refresh-debate-bill-match-view.ts"; + +Deno.serve(proxyToResponseWrapper(refreshDebateBillMatchView)); diff --git a/supabase/schema.sql b/supabase/schema.sql index c4ae6e9..2047a42 100644 --- a/supabase/schema.sql +++ b/supabase/schema.sql @@ -33,7 +33,7 @@ CREATE EXTENSION IF NOT EXISTS "supabase_vault" WITH SCHEMA "vault"; CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA "extensions"; CREATE OR REPLACE FUNCTION "public"."refresh_materialized_view"("view_name" "text") RETURNS "void" - LANGUAGE "plpgsql" + LANGUAGE "plpgsql" SECURITY DEFINER SET "search_path" TO '' AS $$ BEGIN