|
| 1 | +-- complete migration 20250610201801_create_access_table.sql |
| 2 | + |
| 3 | +revoke delete on table "public"."access_token" from "anon"; |
| 4 | + |
| 5 | +revoke references on table "public"."access_token" from "anon"; |
| 6 | + |
| 7 | +revoke trigger on table "public"."access_token" from "anon"; |
| 8 | + |
| 9 | +revoke truncate on table "public"."access_token" from "anon"; |
| 10 | + |
| 11 | +revoke update on table "public"."access_token" from "anon"; |
| 12 | + |
| 13 | +-- rename in schema was not ported to migration 20250611180757_concept_upsert_functions.sql |
| 14 | + |
| 15 | +alter function "public".local_concept_to_db_concept(data concept_local_input) rename to _local_concept_to_db_concept; |
| 16 | + |
| 17 | +CREATE OR REPLACE FUNCTION public.upsert_concepts(v_space_id bigint, data jsonb) |
| 18 | + RETURNS SETOF bigint |
| 19 | + LANGUAGE plpgsql |
| 20 | +AS $function$ |
| 21 | +DECLARE |
| 22 | + v_platform public."Platform"; |
| 23 | + local_concept public.concept_local_input; |
| 24 | + db_concept public."Concept"%ROWTYPE; |
| 25 | + concept_row JSONB; |
| 26 | + concept_id BIGINT; |
| 27 | +BEGIN |
| 28 | + SELECT platform INTO STRICT v_platform FROM public."Space" WHERE id=v_space_id; |
| 29 | + FOR concept_row IN SELECT * FROM jsonb_array_elements(data) |
| 30 | + LOOP |
| 31 | + -- first set defaults |
| 32 | + local_concept := jsonb_populate_record(NULL::public.concept_local_input, '{"epistemic_status": "unknown", "literal_content":{},"reference_content":{},"is_schema":false}'); |
| 33 | + -- then input values |
| 34 | + local_concept := jsonb_populate_record(local_concept, concept_row); |
| 35 | + local_concept.space_id := v_space_id; |
| 36 | + db_concept := public._local_concept_to_db_concept(local_concept); |
| 37 | + BEGIN |
| 38 | + -- cannot use db_concept.* because of refs. |
| 39 | + INSERT INTO public."Concept" ( |
| 40 | + epistemic_status, name, description, author_id, created, last_modified, space_id, schema_id, literal_content, is_schema, represented_by_id, reference_content |
| 41 | + ) VALUES ( |
| 42 | + db_concept.epistemic_status, db_concept.name, db_concept.description, db_concept.author_id, db_concept.created, db_concept.last_modified, db_concept.space_id, db_concept.schema_id, db_concept.literal_content, db_concept.is_schema, db_concept.represented_by_id, db_concept.reference_content |
| 43 | + ) |
| 44 | + ON CONFLICT (represented_by_id) DO UPDATE SET |
| 45 | + epistemic_status = db_concept.epistemic_status, |
| 46 | + name = db_concept.name, |
| 47 | + description = db_concept.description, |
| 48 | + author_id = db_concept.author_id, |
| 49 | + created = db_concept.created, |
| 50 | + last_modified = db_concept.last_modified, |
| 51 | + space_id = db_concept.space_id, |
| 52 | + schema_id = db_concept.schema_id, |
| 53 | + literal_content = db_concept.literal_content, |
| 54 | + is_schema = db_concept.is_schema, |
| 55 | + reference_content = db_concept.reference_content |
| 56 | + -- ON CONFLICT (space_id, name) DO NOTHING... why can't I specify two conflict clauses? |
| 57 | + RETURNING id INTO concept_id; |
| 58 | + RETURN NEXT concept_id; |
| 59 | + EXCEPTION |
| 60 | + WHEN unique_violation THEN |
| 61 | + -- a distinct unique constraint failed |
| 62 | + RAISE WARNING 'Concept with space_id: % and name % already exists', v_space_id, local_concept.name; |
| 63 | + RETURN NEXT -1; -- Return a special value to indicate conflict |
| 64 | + END; |
| 65 | + END LOOP; |
| 66 | + RAISE DEBUG 'Completed upsert_content successfully'; |
| 67 | +END; |
| 68 | +$function$ |
| 69 | +; |
0 commit comments