|
| 1 | +CREATE TYPE public."ContentVariant" AS ENUM ( |
| 2 | + 'direct', |
| 3 | + 'direct_and_children', |
| 4 | + 'direct_and_description' |
| 5 | +); |
| 6 | + |
| 7 | +ALTER TYPE public."ContentVariant" OWNER TO postgres; |
| 8 | + |
| 9 | +ALTER TABLE public."Content" ADD COLUMN variant public."ContentVariant" NOT NULL DEFAULT 'direct'; |
| 10 | + |
| 11 | +DROP INDEX IF EXISTS public."content_space_and_local_id_idx"; |
| 12 | + |
| 13 | +CREATE UNIQUE INDEX content_space_local_id_variant_idx ON public."Content" USING btree (space_id, source_local_id, variant); |
| 14 | + |
| 15 | +ALTER TYPE public.content_local_input ADD ATTRIBUTE variant public."ContentVariant"; |
| 16 | + |
| 17 | +CREATE OR REPLACE FUNCTION public.upsert_content(v_space_id bigint, data jsonb, v_creator_id BIGINT, content_as_document boolean DEFAULT true) |
| 18 | +RETURNS SETOF BIGINT |
| 19 | +LANGUAGE plpgsql |
| 20 | +AS $$ |
| 21 | +DECLARE |
| 22 | + v_platform public."Platform"; |
| 23 | + db_document public."Document"%ROWTYPE; |
| 24 | + document_id BIGINT; |
| 25 | + local_content public.content_local_input; |
| 26 | + db_content public."Content"%ROWTYPE; |
| 27 | + content_row JSONB; |
| 28 | + upsert_id BIGINT; |
| 29 | +BEGIN |
| 30 | + SELECT platform INTO STRICT v_platform FROM public."Space" WHERE id=v_space_id; |
| 31 | + FOR content_row IN SELECT * FROM jsonb_array_elements(data) |
| 32 | + LOOP |
| 33 | + local_content := jsonb_populate_record(NULL::public.content_local_input, content_row); |
| 34 | + local_content.space_id := v_space_id; |
| 35 | + db_content := public._local_content_to_db_content(local_content); |
| 36 | + IF account_local_id(author_inline(local_content)) IS NOT NULL THEN |
| 37 | + SELECT public.create_account_in_space( |
| 38 | + v_space_id, |
| 39 | + account_local_id(author_inline(local_content)), |
| 40 | + name(author_inline(local_content)) |
| 41 | + ) INTO STRICT upsert_id; |
| 42 | + local_content.author_id := upsert_id; |
| 43 | + END IF; |
| 44 | + IF account_local_id(creator_inline(local_content)) IS NOT NULL THEN |
| 45 | + SELECT public.create_account_in_space( |
| 46 | + v_space_id, |
| 47 | + account_local_id(creator_inline(local_content)), |
| 48 | + name(creator_inline(local_content)) |
| 49 | + ) INTO STRICT upsert_id; |
| 50 | + local_content.creator_id := upsert_id; |
| 51 | + END IF; |
| 52 | + IF content_as_document THEN |
| 53 | + db_content.scale = 'document'; |
| 54 | + END IF; |
| 55 | + IF content_as_document AND document_id(db_content) IS NULL AND source_local_id(document_inline(local_content)) IS NULL THEN |
| 56 | + local_content.document_inline.space_id := v_space_id; |
| 57 | + local_content.document_inline.source_local_id := db_content.source_local_id; |
| 58 | + local_content.document_inline.last_modified := db_content.last_modified; |
| 59 | + local_content.document_inline.created := db_content.created; |
| 60 | + local_content.document_inline.author_id := db_content.author_id; |
| 61 | + END IF; |
| 62 | + IF source_local_id(document_inline(local_content)) IS NOT NULL THEN |
| 63 | + db_document := _local_document_to_db_document(document_inline(local_content)); |
| 64 | + INSERT INTO public."Document" ( |
| 65 | + space_id, |
| 66 | + source_local_id, |
| 67 | + url, |
| 68 | + created, |
| 69 | + metadata, |
| 70 | + last_modified, |
| 71 | + author_id, |
| 72 | + contents |
| 73 | + ) VALUES ( |
| 74 | + COALESCE(db_document.space_id, v_space_id), |
| 75 | + db_document.source_local_id, |
| 76 | + db_document.url, |
| 77 | + db_document.created, |
| 78 | + COALESCE(db_document.metadata, '{}'::jsonb), |
| 79 | + db_document.last_modified, |
| 80 | + db_document.author_id, |
| 81 | + db_document.contents |
| 82 | + ) |
| 83 | + ON CONFLICT (space_id, source_local_id) DO UPDATE SET |
| 84 | + url = COALESCE(db_document.url, EXCLUDED.url), |
| 85 | + created = COALESCE(db_document.created, EXCLUDED.created), |
| 86 | + metadata = COALESCE(db_document.metadata, EXCLUDED.metadata), |
| 87 | + last_modified = COALESCE(db_document.last_modified, EXCLUDED.last_modified), |
| 88 | + author_id = COALESCE(db_document.author_id, EXCLUDED.author_id), |
| 89 | + contents = COALESCE(db_document.contents, EXCLUDED.contents) |
| 90 | + RETURNING id INTO STRICT document_id; |
| 91 | + db_content.document_id := document_id; |
| 92 | + END IF; |
| 93 | + INSERT INTO public."Content" ( |
| 94 | + document_id, |
| 95 | + source_local_id, |
| 96 | + variant, |
| 97 | + author_id, |
| 98 | + creator_id, |
| 99 | + created, |
| 100 | + text, |
| 101 | + metadata, |
| 102 | + scale, |
| 103 | + space_id, |
| 104 | + last_modified, |
| 105 | + part_of_id |
| 106 | + ) VALUES ( |
| 107 | + db_content.document_id, |
| 108 | + db_content.source_local_id, |
| 109 | + COALESCE(db_content.variant, 'direct'::public."ContentVariant"), |
| 110 | + db_content.author_id, |
| 111 | + db_content.creator_id, |
| 112 | + db_content.created, |
| 113 | + db_content.text, |
| 114 | + COALESCE(db_content.metadata, '{}'::jsonb), |
| 115 | + db_content.scale, |
| 116 | + db_content.space_id, |
| 117 | + db_content.last_modified, |
| 118 | + db_content.part_of_id |
| 119 | + ) |
| 120 | + ON CONFLICT (space_id, source_local_id, variant) DO UPDATE SET |
| 121 | + document_id = COALESCE(db_content.document_id, EXCLUDED.document_id), |
| 122 | + author_id = COALESCE(db_content.author_id, EXCLUDED.author_id), |
| 123 | + creator_id = COALESCE(db_content.creator_id, EXCLUDED.creator_id), |
| 124 | + created = COALESCE(db_content.created, EXCLUDED.created), |
| 125 | + text = COALESCE(db_content.text, EXCLUDED.text), |
| 126 | + metadata = COALESCE(db_content.metadata, EXCLUDED.metadata), |
| 127 | + scale = COALESCE(db_content.scale, EXCLUDED.scale), |
| 128 | + last_modified = COALESCE(db_content.last_modified, EXCLUDED.last_modified), |
| 129 | + part_of_id = COALESCE(db_content.part_of_id, EXCLUDED.part_of_id) |
| 130 | + RETURNING id INTO STRICT upsert_id; |
| 131 | + IF model(embedding_inline(local_content)) IS NOT NULL THEN |
| 132 | + PERFORM public.upsert_content_embedding(upsert_id, model(embedding_inline(local_content)), vector(embedding_inline(local_content))); |
| 133 | + END IF; |
| 134 | + RETURN NEXT upsert_id; |
| 135 | + END LOOP; |
| 136 | +END; |
| 137 | +$$; |
0 commit comments