From 3b9f80fffd077edc3f55497b073d7e72010cbf5f Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Thu, 4 Dec 2025 18:07:58 -0500 Subject: [PATCH] update metadata tables to support tags --- .../20251204130000_add_tags_to_drafts.sql | 7 ++++ ...51204140000_update_pull_data_with_tags.sql | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 supabase/migrations/20251204130000_add_tags_to_drafts.sql create mode 100644 supabase/migrations/20251204140000_update_pull_data_with_tags.sql diff --git a/supabase/migrations/20251204130000_add_tags_to_drafts.sql b/supabase/migrations/20251204130000_add_tags_to_drafts.sql new file mode 100644 index 00000000..91667f74 --- /dev/null +++ b/supabase/migrations/20251204130000_add_tags_to_drafts.sql @@ -0,0 +1,7 @@ +-- Add tags column to leaflets_in_publications for publication drafts +ALTER TABLE "public"."leaflets_in_publications" +ADD COLUMN "tags" text[] DEFAULT ARRAY[]::text[]; + +-- Add tags column to leaflets_to_documents for standalone document drafts +ALTER TABLE "public"."leaflets_to_documents" +ADD COLUMN "tags" text[] DEFAULT ARRAY[]::text[]; diff --git a/supabase/migrations/20251204140000_update_pull_data_with_tags.sql b/supabase/migrations/20251204140000_update_pull_data_with_tags.sql new file mode 100644 index 00000000..9b55bff4 --- /dev/null +++ b/supabase/migrations/20251204140000_update_pull_data_with_tags.sql @@ -0,0 +1,38 @@ +set check_function_bodies = off; + +CREATE OR REPLACE FUNCTION public.pull_data(token_id uuid, client_group_id text) + RETURNS pull_result + LANGUAGE plpgsql +AS $function$DECLARE + result pull_result; +BEGIN + -- Get client group data as JSON array + SELECT json_agg(row_to_json(rc)) + FROM replicache_clients rc + WHERE rc.client_group = client_group_id + INTO result.client_groups; + + -- Get facts as JSON array + SELECT json_agg(row_to_json(f)) + FROM permission_tokens pt, + get_facts(pt.root_entity) f + WHERE pt.id = token_id + INTO result.facts; + + -- Get publication data - try leaflets_in_publications first, then leaflets_to_documents + SELECT json_agg(row_to_json(lip)) + FROM leaflets_in_publications lip + WHERE lip.leaflet = token_id + INTO result.publications; + + -- If no publication data found, try leaflets_to_documents (for standalone documents) + IF result.publications IS NULL THEN + SELECT json_agg(row_to_json(ltd)) + FROM leaflets_to_documents ltd + WHERE ltd.leaflet = token_id + INTO result.publications; + END IF; + + RETURN result; +END;$function$ +; -- 2.51.2