Skip to content

Commit

Permalink
Add ability to refresh debate bill match view via endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
limdingwen committed Aug 7, 2024
1 parent b65e7f7 commit 5dab9b6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
11 changes: 11 additions & 0 deletions scripts/deploy-and-run/refresh-debate-bill-match-view.sh
Original file line number Diff line number Diff line change
@@ -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" '{}'
7 changes: 6 additions & 1 deletion site/src/utils/supabase/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,12 @@ export type Database = {
}
}
Functions: {
[_ in never]: never
refresh_materialized_view: {
Args: {
view_name: string
}
Returns: undefined
}
}
Enums: {
[_ in never]: never
Expand Down
7 changes: 6 additions & 1 deletion supabase/functions/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,12 @@ export type Database = {
}
}
Functions: {
[_ in never]: never
refresh_materialized_view: {
Args: {
view_name: string
}
Returns: undefined
}
}
Enums: {
[_ in never]: never
Expand Down
Original file line number Diff line number Diff line change
@@ -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.",
});
}
5 changes: 5 additions & 0 deletions supabase/functions/refresh-debate-bill-match-view/index.ts
Original file line number Diff line number Diff line change
@@ -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));
2 changes: 1 addition & 1 deletion supabase/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5dab9b6

Please sign in to comment.