Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for crossposting to territories #800

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

byoungdale
Copy link

This is the pull request for crossposting to territories. I am starting with the criteria from @huumn in #712 . This is just the changes for the schema so far. I left the old sub field, and added a new subs field. I figured this would be better for migration. This migration will update the new table and ‘subs’ field in Item. Then once the rest of the code is pointed toward using the subs and subNames, sub will be unused and ready for cleanup whenever it’s safely in production. That sound like a good path? I’ll update this branch with the rest of the changes as I go.

Here is a quick glance of what I have so far:

update prisma schema

on Item

+  subs               Sub[]                 @relation("ItemSub")
+  subNames           String[]

on Sub

+  Items        Item[]         @relation("ItemSub")

updated migration file with trigger on new _ItemSub join table to update Item table

-- create new update_subnames trigger
CREATE OR REPLACE FUNCTION public.update_subnames()
    RETURNS TRIGGER
    AS $$
BEGIN
    UPDATE "Item"
    SET "subNames" = (
        SELECT ARRAY_AGG("subName")
        FROM "_ItemSub"
        WHERE "itemId" = NEW."id"
    )
    WHERE "id" = NEW."id";

    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_subnames_trigger
AFTER INSERT OR UPDATE ON "_ItemSub"
FOR EACH ROW
EXECUTE FUNCTION update_subnames();

updated create_item and update_item to update _ItemSub table on create and update of an item

create_item

+    -- Loop over the 'subs' array and insert into "_ItemSub"
+    FOREACH sub_name IN ARRAY subs
+    LOOP
+        INSERT INTO "_ItemSub" ("itemId", "subName")
+        VALUES (item.id, sub_name);
+    END LOOP;

update_item

+    -- Loop over the 'subs' array and insert into "_ItemSub" if it does not already exist
+    FOREACH sub_name IN ARRAY subs
+    LOOP
+        INSERT INTO "_ItemSub" ("itemId", "subName")
+        SELECT item.id, sub_name
+        WHERE NOT EXISTS (
+            SELECT 1 FROM "_ItemSub"
+            WHERE "itemId" = item.id AND "subName" = sub_name
+        );
+    END LOOP;

Update new database fields and tables with existing data

-- Update new database fields and tables with existing data
-- Insert into ItemSub table
INSERT INTO "ItemSub" ("itemId", "subName")
SELECT "id", "subName"
FROM "Item"
WHERE "subName" IS NOT NULL
AND NOT EXISTS (
    SELECT 1 FROM "ItemSub" WHERE "itemId" = "Item"."id" AND "subName" = "Item"."subName"
);

-- Update subs array in Item table
UPDATE "Item" AS A
SET "subNames" = B."subNames"
FROM (
    SELECT "itemId", ARRAY_AGG("subName") AS "subNames"
    FROM "ItemSub"
    GROUP BY "itemId"
) AS B
WHERE A."id" = B."itemId";

byoungdale and others added 3 commits February 8, 2024 11:01
…igration file with trigger on _ItemSub join table to update Item table, and updated create_item and update_item to update _ItemSub table on create and update of an item
keeping prisma schema up to date
@huumn
Copy link
Member

huumn commented Feb 9, 2024

Cool! I'm going to put this in draft until it's the whole hog.

One thing I've noticed off the jump: if you're going to add a table, we'd prefer it be defined in prisma.schema if it can be. Also, unless there's a convention I'm unaware of "_ItemSub" should probably just be "ItemSub."

@huumn huumn marked this pull request as draft February 9, 2024 16:20
@byoungdale
Copy link
Author

Got it. Sounds good. The underscore style (_Name) is what prisma creates automatically when a many-to-many relationship is defined in the schema. It's an implicit pass-through table creation, which I don't personally like. I'll update that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants