Skip to content

Commit f87ed7f

Browse files
committed
improvement
1 parent 580a1e0 commit f87ed7f

File tree

2 files changed

+78
-27
lines changed

2 files changed

+78
-27
lines changed

packages/prisma/migrations/20240320000001_add_booking_time_status_denormalized/migration.sql

+42-27
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
-- Create the denormalized table
22
CREATE TABLE "BookingTimeStatusDenormalized" (
3-
id INTEGER,
4-
uid TEXT,
3+
id INTEGER NOT NULL PRIMARY KEY,
4+
uid TEXT NOT NULL,
55
"eventTypeId" INTEGER,
66
title TEXT,
77
description TEXT,
8-
"startTime" TIMESTAMP(3),
9-
"endTime" TIMESTAMP(3),
10-
"createdAt" TIMESTAMP(3),
8+
"startTime" TIMESTAMP(3) NOT NULL,
9+
"endTime" TIMESTAMP(3) NOT NULL,
10+
"createdAt" TIMESTAMP(3) NOT NULL,
1111
location TEXT,
1212
paid BOOLEAN,
13-
status TEXT,
13+
status TEXT NOT NULL,
1414
rescheduled BOOLEAN,
1515
"userId" INTEGER,
1616
"teamId" INTEGER,
@@ -26,14 +26,15 @@ CREATE TABLE "BookingTimeStatusDenormalized" (
2626
);
2727

2828
-- Create indexes to match likely query patterns
29-
CREATE INDEX "idx_booking_id" ON "BookingTimeStatusDenormalized" (id);
3029
CREATE INDEX "idx_booking_user_id" ON "BookingTimeStatusDenormalized" ("userId");
3130
CREATE INDEX "idx_booking_created_at" ON "BookingTimeStatusDenormalized" ("createdAt");
31+
CREATE INDEX "idx_event_type_id" ON "BookingTimeStatusDenormalized" ("eventTypeId");
3232
CREATE INDEX "idx_event_type_hierarchy" ON "BookingTimeStatusDenormalized" ("eventTypeId", "eventParentId");
33-
CREATE INDEX "idx_time_status" ON "BookingTimeStatusDenormalized" ("timeStatus");
34-
CREATE INDEX "idx_team_id" ON "BookingTimeStatusDenormalized" ("teamId");
35-
CREATE INDEX "idx_start_time" ON "BookingTimeStatusDenormalized" ("startTime");
36-
CREATE INDEX "idx_end_time" ON "BookingTimeStatusDenormalized" ("endTime");
33+
CREATE INDEX "idx_booking_time_status" ON "BookingTimeStatusDenormalized" ("timeStatus");
34+
CREATE INDEX "idx_booking_team_id" ON "BookingTimeStatusDenormalized" ("teamId");
35+
CREATE INDEX "idx_booking_start_time" ON "BookingTimeStatusDenormalized" ("startTime");
36+
CREATE INDEX "idx_booking_end_time" ON "BookingTimeStatusDenormalized" ("endTime");
37+
CREATE INDEX "idx_booking_status" ON "BookingTimeStatusDenormalized" ("status");
3738

3839
-- Function to calculate timeStatus
3940
CREATE OR REPLACE FUNCTION calculate_time_status(
@@ -104,7 +105,11 @@ $$ LANGUAGE plpgsql;
104105
CREATE OR REPLACE FUNCTION trigger_refresh_booking_time_status_denormalized()
105106
RETURNS TRIGGER AS $$
106107
BEGIN
107-
PERFORM refresh_booking_time_status_denormalized(NEW.id);
108+
BEGIN
109+
PERFORM refresh_booking_time_status_denormalized(NEW.id);
110+
EXCEPTION WHEN OTHERS THEN
111+
RAISE WARNING 'DENORM_ERROR: BookingTimeStatusDenormalized - Failed to handle booking change for id %, error: %', NEW.id, SQLERRM;
112+
END;
108113
RETURN NEW;
109114
END;
110115
$$ LANGUAGE plpgsql;
@@ -113,7 +118,11 @@ $$ LANGUAGE plpgsql;
113118
CREATE OR REPLACE FUNCTION trigger_delete_booking_time_status_denormalized()
114119
RETURNS TRIGGER AS $$
115120
BEGIN
116-
DELETE FROM "BookingTimeStatusDenormalized" WHERE id = OLD.id;
121+
BEGIN
122+
DELETE FROM "BookingTimeStatusDenormalized" WHERE id = OLD.id;
123+
EXCEPTION WHEN OTHERS THEN
124+
RAISE WARNING 'DENORM_ERROR: BookingTimeStatusDenormalized - Failed to delete denormalized booking id %, error: %', OLD.id, SQLERRM;
125+
END;
117126
RETURN OLD;
118127
END;
119128
$$ LANGUAGE plpgsql;
@@ -133,14 +142,17 @@ CREATE TRIGGER booking_delete_trigger
133142
CREATE OR REPLACE FUNCTION trigger_refresh_booking_time_status_denormalized_event_type()
134143
RETURNS TRIGGER AS $$
135144
BEGIN
136-
-- Refresh all bookings associated with this event type
137-
UPDATE "BookingTimeStatusDenormalized" btsd
138-
SET
139-
"teamId" = NEW."teamId",
140-
"eventLength" = NEW.length,
141-
"eventParentId" = NEW."parentId",
142-
"isTeamBooking" = calculate_is_team_booking(NEW."teamId")
143-
WHERE btsd."eventTypeId" = NEW.id;
145+
BEGIN
146+
UPDATE "BookingTimeStatusDenormalized" btsd
147+
SET
148+
"teamId" = NEW."teamId",
149+
"eventLength" = NEW.length,
150+
"eventParentId" = NEW."parentId",
151+
"isTeamBooking" = calculate_is_team_booking(NEW."teamId")
152+
WHERE btsd."eventTypeId" = NEW.id;
153+
EXCEPTION WHEN OTHERS THEN
154+
RAISE WARNING 'DENORM_ERROR: BookingTimeStatusDenormalized - Failed to update EventType changes for id %, error: %', NEW.id, SQLERRM;
155+
END;
144156
RETURN NEW;
145157
END;
146158
$$ LANGUAGE plpgsql;
@@ -149,12 +161,15 @@ $$ LANGUAGE plpgsql;
149161
CREATE OR REPLACE FUNCTION trigger_refresh_booking_time_status_denormalized_user()
150162
RETURNS TRIGGER AS $$
151163
BEGIN
152-
-- Refresh all bookings associated with this user
153-
UPDATE "BookingTimeStatusDenormalized" btsd
154-
SET
155-
"userEmail" = NEW.email,
156-
"username" = NEW.username
157-
WHERE btsd."userId" = NEW.id;
164+
BEGIN
165+
UPDATE "BookingTimeStatusDenormalized" btsd
166+
SET
167+
"userEmail" = NEW.email,
168+
"username" = NEW.username
169+
WHERE btsd."userId" = NEW.id;
170+
EXCEPTION WHEN OTHERS THEN
171+
RAISE WARNING 'DENORM_ERROR: BookingTimeStatusDenormalized - Failed to update user changes for id %, error: %', NEW.id, SQLERRM;
172+
END;
158173
RETURN NEW;
159174
END;
160175
$$ LANGUAGE plpgsql;

packages/prisma/schema.prisma

+36
Original file line numberDiff line numberDiff line change
@@ -2085,3 +2085,39 @@ model WorkflowOptOutContact {
20852085
20862086
@@unique([type, value])
20872087
}
2088+
2089+
model BookingTimeStatusDenormalized {
2090+
id Int @id
2091+
uid String
2092+
eventTypeId Int?
2093+
title String?
2094+
description String?
2095+
startTime DateTime
2096+
endTime DateTime
2097+
createdAt DateTime
2098+
location String?
2099+
paid Boolean?
2100+
status String
2101+
rescheduled Boolean?
2102+
userId Int?
2103+
teamId Int?
2104+
eventLength Int?
2105+
timeStatus String?
2106+
eventParentId Int?
2107+
userEmail String?
2108+
username String?
2109+
ratingFeedback String?
2110+
rating Int?
2111+
noShowHost Boolean?
2112+
isTeamBooking Boolean?
2113+
2114+
@@index([userId])
2115+
@@index([createdAt])
2116+
@@index([eventTypeId])
2117+
@@index([eventTypeId, eventParentId])
2118+
@@index([timeStatus])
2119+
@@index([teamId])
2120+
@@index([startTime])
2121+
@@index([endTime])
2122+
@@index([status])
2123+
}

0 commit comments

Comments
 (0)