Skip to content

Commit 2bd402b

Browse files
committed
don't use trigger to migrate column (remove tricky constraint for now)
1 parent 7f75af5 commit 2bd402b

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

prisma/migrations/20251228234658_subnames/migration.sql

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ CREATE TABLE "ItemSub" (
1111
CONSTRAINT "ItemSub_pkey" PRIMARY KEY ("itemId","subName")
1212
);
1313

14+
-- migrate old subnames to ItemSub
15+
INSERT INTO "ItemSub" ("itemId", "subName") SELECT "id", "subName" FROM "Item" WHERE "subName" IS NOT NULL;
16+
17+
UPDATE "Item" SET "subNames" = ARRAY["subName"] WHERE "subName" IS NOT NULL;
18+
1419
CREATE FUNCTION denormalize_subnames() RETURNS TRIGGER AS $$
1520
BEGIN
1621
UPDATE "Item" SET "subNames" = subquery."subNames"
@@ -28,23 +33,8 @@ CREATE TRIGGER "denormalize_subnames_trigger"
2833
AFTER INSERT OR UPDATE OR DELETE ON "ItemSub"
2934
FOR EACH ROW EXECUTE FUNCTION denormalize_subnames();
3035

31-
-- migrate old subnames to ItemSub
32-
INSERT INTO "ItemSub" ("itemId", "subName") SELECT "id", "subName" FROM "Item" WHERE "subName" IS NOT NULL;
33-
3436
CREATE EXTENSION IF NOT EXISTS btree_gin;
3537

36-
-- can't use subName in the unique time constraint anymore
37-
ALTER TABLE "Item" DROP CONSTRAINT "Item_unique_time_constraint";
38-
ALTER TABLE "Item" ADD CONSTRAINT "Item_unique_time_constraint"
39-
EXCLUDE USING gist (
40-
"userId" WITH =,
41-
COALESCE("parentId", -1) WITH =,
42-
md5(COALESCE("title", '')) WITH =,
43-
md5(COALESCE("text", '')) WITH =,
44-
tsrange(created_at, created_at + INTERVAL '10 minutes') WITH &&
45-
)
46-
WHERE (created_at > '2024-12-30' AND "deletedAt" IS NULL);
47-
4838
-- CreateIndex
4939
CREATE INDEX "ItemSub_itemId_idx" ON "ItemSub"("itemId");
5040

0 commit comments

Comments
 (0)