From 4ab3ec73c0387bc5a50c4de647ca37c431f369a5 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Wed, 18 Dec 2024 15:14:04 +0000 Subject: [PATCH] chore: Set the stage for a primary key with value uuid() (#18245) * chore: Set the stage for a primary key with value uuid() * fix: Test and ensure it validates the id value is set and defined * Add unique index manually, prisma doesn't support this * revert unique --- .../lib/CalendarService.test.ts | 57 +++++++++++-------- .../migration.sql | 5 ++ packages/prisma/schema.prisma | 2 + 3 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 packages/prisma/migrations/20241218125130_add_optional_uuid_two_step_primary_key/migration.sql diff --git a/packages/app-store/googlecalendar/lib/CalendarService.test.ts b/packages/app-store/googlecalendar/lib/CalendarService.test.ts index 87aab1cc2c..fc7ae8541a 100644 --- a/packages/app-store/googlecalendar/lib/CalendarService.test.ts +++ b/packages/app-store/googlecalendar/lib/CalendarService.test.ts @@ -136,18 +136,23 @@ test("Calendar can be watched and unwatched", async () => { integration: "google_calendar", }, }); - expect(watchedCalendar).toEqual({ - userId: 1, - integration: "google_calendar", - externalId: "example@cal.com", - credentialId: 1, - domainWideDelegationCredentialId: null, - googleChannelId: "mock-channel-id", - googleChannelKind: "api#channel", - googleChannelResourceId: "mock-resource-id", - googleChannelResourceUri: "mock-resource-uri", - googleChannelExpiration: "1111111111", - }); + expect(watchedCalendar).toEqual( + expect.objectContaining({ + userId: 1, + integration: "google_calendar", + externalId: "example@cal.com", + credentialId: 1, + domainWideDelegationCredentialId: null, + googleChannelId: "mock-channel-id", + googleChannelKind: "api#channel", + googleChannelResourceId: "mock-resource-id", + googleChannelResourceUri: "mock-resource-uri", + googleChannelExpiration: "1111111111", + }) + ); + + expect(watchedCalendar?.id).toBeDefined(); + await calendarCache.unwatchCalendar({ calendarId: testSelectedCalendar.externalId }); // There's a bug in prismock where upsert creates duplicate records so we need to acces the second element const [, unWatchedCalendar] = await prismock.selectedCalendar.findMany({ @@ -158,18 +163,22 @@ test("Calendar can be watched and unwatched", async () => { }, }); - expect(unWatchedCalendar).toEqual({ - userId: 1, - integration: "google_calendar", - externalId: "example@cal.com", - credentialId: 1, - domainWideDelegationCredentialId: null, - googleChannelId: null, - googleChannelKind: null, - googleChannelResourceId: null, - googleChannelResourceUri: null, - googleChannelExpiration: null, - }); + expect(unWatchedCalendar).toEqual( + expect.objectContaining({ + userId: 1, + integration: "google_calendar", + externalId: "example@cal.com", + credentialId: 1, + domainWideDelegationCredentialId: null, + googleChannelId: null, + googleChannelKind: null, + googleChannelResourceId: null, + googleChannelResourceUri: null, + googleChannelExpiration: null, + }) + ); + + expect(unWatchedCalendar?.id).toBeDefined(); }); test("`updateTokenObject` should update credential in DB as well as myGoogleAuth", async () => { diff --git a/packages/prisma/migrations/20241218125130_add_optional_uuid_two_step_primary_key/migration.sql b/packages/prisma/migrations/20241218125130_add_optional_uuid_two_step_primary_key/migration.sql new file mode 100644 index 0000000000..6d110e0419 --- /dev/null +++ b/packages/prisma/migrations/20241218125130_add_optional_uuid_two_step_primary_key/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable +ALTER TABLE "SelectedCalendar" ADD COLUMN "id" TEXT; + +-- Update all historical rows (id) to `gen_random_uuid` +UPDATE "SelectedCalendar" SET "id" = gen_random_uuid() WHERE "id" IS NULL; \ No newline at end of file diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma index 4da67c66f6..1f0fde1ab9 100644 --- a/packages/prisma/schema.prisma +++ b/packages/prisma/schema.prisma @@ -709,6 +709,8 @@ model Availability { } model SelectedCalendar { + // Will become required and @id in a seperate PR + id String? @default(uuid()) user User @relation(fields: [userId], references: [id], onDelete: Cascade) userId Int integration String