From e5e8d0acc8a37e26c01e89986f4605cda325338c Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Mon, 5 Feb 2024 11:47:12 -0800 Subject: [PATCH 1/2] finalize teams schema --- .../migrations/04_team_redesign/migration.sql | 13 +++++++-- db/mysql/schema.prisma | 25 ++++++++++------ .../migrations/04_team_redesign/migration.sql | 9 ++++-- db/postgresql/schema.prisma | 29 +++++++++++-------- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/db/mysql/migrations/04_team_redesign/migration.sql b/db/mysql/migrations/04_team_redesign/migration.sql index 1ff7ee6e..7580f77a 100644 --- a/db/mysql/migrations/04_team_redesign/migration.sql +++ b/db/mysql/migrations/04_team_redesign/migration.sql @@ -5,13 +5,22 @@ */ -- AlterTable -ALTER TABLE `team` ADD COLUMN `deleted_at` TIMESTAMP(0) NULL; +ALTER TABLE `team` ADD COLUMN `deleted_at` TIMESTAMP(0) NULL, + ADD COLUMN `logo_url` VARCHAR(2183) NULL; -- AlterTable -ALTER TABLE `website` ADD COLUMN `team_id` VARCHAR(36) NULL; +ALTER TABLE `user` ADD COLUMN `display_name` VARCHAR(255) NULL, + ADD COLUMN `logo_url` VARCHAR(2183) NULL; + +-- AlterTable +ALTER TABLE `website` ADD COLUMN `created_by` VARCHAR(36) NULL, + ADD COLUMN `team_id` VARCHAR(36) NULL; -- DropTable DROP TABLE `team_website`; -- CreateIndex CREATE INDEX `website_team_id_idx` ON `website`(`team_id`); + +-- CreateIndex +CREATE INDEX `website_created_by_idx` ON `website`(`created_by`); diff --git a/db/mysql/schema.prisma b/db/mysql/schema.prisma index de1a3f7f..8e5cbbc3 100644 --- a/db/mysql/schema.prisma +++ b/db/mysql/schema.prisma @@ -9,15 +9,18 @@ datasource db { } model User { - id String @id @unique @map("user_id") @db.VarChar(36) - username String @unique @db.VarChar(255) - password String @db.VarChar(60) - role String @map("role") @db.VarChar(50) - createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0) - updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamp(0) - deletedAt DateTime? @map("deleted_at") @db.Timestamp(0) + id String @id @unique @map("user_id") @db.VarChar(36) + username String @unique @db.VarChar(255) + password String @db.VarChar(60) + role String @map("role") @db.VarChar(50) + logoUrl String? @map("logo_url") @db.VarChar(2183) + displayName String? @map("display_name") @db.VarChar(255) + createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0) + updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamp(0) + deletedAt DateTime? @map("deleted_at") @db.Timestamp(0) - website Website[] + websiteUser Website[] @relation("user") + websiteCreateUser Website[] @relation("createUser") teamUser TeamUser[] report Report[] @@ -65,11 +68,13 @@ model Website { resetAt DateTime? @map("reset_at") @db.Timestamp(0) userId String? @map("user_id") @db.VarChar(36) teamId String? @map("team_id") @db.VarChar(36) + createdBy String? @map("created_by") @db.VarChar(36) createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0) updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamp(0) deletedAt DateTime? @map("deleted_at") @db.Timestamp(0) - user User? @relation(fields: [userId], references: [id]) + user User? @relation("user", fields: [userId], references: [id]) + createUser User? @relation("createUser", fields: [createdBy], references: [id]) team Team? @relation(fields: [teamId], references: [id]) eventData EventData[] report Report[] @@ -79,6 +84,7 @@ model Website { @@index([teamId]) @@index([createdAt]) @@index([shareId]) + @@index([createdBy]) @@map("website") } @@ -159,6 +165,7 @@ model Team { id String @id() @unique() @map("team_id") @db.VarChar(36) name String @db.VarChar(50) accessCode String? @unique @map("access_code") @db.VarChar(50) + logoUrl String? @map("logo_url") @db.VarChar(2183) createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0) updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamp(0) deletedAt DateTime? @map("deleted_at") @db.Timestamp(0) diff --git a/db/postgresql/migrations/04_team_redesign/migration.sql b/db/postgresql/migrations/04_team_redesign/migration.sql index cdb424e5..a63d2d0b 100644 --- a/db/postgresql/migrations/04_team_redesign/migration.sql +++ b/db/postgresql/migrations/04_team_redesign/migration.sql @@ -9,13 +9,18 @@ ALTER TABLE "team" ADD COLUMN "deleted_at" TIMESTAMPTZ(6) DEFAULT CURRENT_TI ADD COLUMN "logo_url" VARCHAR(2183); -- AlterTable -ALTER TABLE "user" ADD COLUMN "logo_url" VARCHAR(2183); +ALTER TABLE "user" ADD COLUMN "display_name" VARCHAR(255), +ADD COLUMN "logo_url" VARCHAR(2183); -- AlterTable -ALTER TABLE "website" ADD COLUMN "team_id" UUID; +ALTER TABLE "website" ADD COLUMN "created_by" UUID, +ADD COLUMN "team_id" UUID; -- DropTable DROP TABLE "team_website"; -- CreateIndex CREATE INDEX "website_team_id_idx" ON "website"("team_id"); + +-- CreateIndex +CREATE INDEX "website_created_by_idx" ON "website"("created_by"); diff --git a/db/postgresql/schema.prisma b/db/postgresql/schema.prisma index 2a791b66..3f1838be 100644 --- a/db/postgresql/schema.prisma +++ b/db/postgresql/schema.prisma @@ -9,18 +9,20 @@ datasource db { } model User { - id String @id @unique @map("user_id") @db.Uuid - username String @unique @db.VarChar(255) - password String @db.VarChar(60) - role String @map("role") @db.VarChar(50) - logoUrl String? @map("logo_url") @db.VarChar(2183) - createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6) - updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6) - deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6) + id String @id @unique @map("user_id") @db.Uuid + username String @unique @db.VarChar(255) + password String @db.VarChar(60) + role String @map("role") @db.VarChar(50) + logoUrl String? @map("logo_url") @db.VarChar(2183) + displayName String? @map("display_name") @db.VarChar(255) + createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6) + updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6) + deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6) - website Website[] - teamUser TeamUser[] - report Report[] + websiteUser Website[] @relation("user") + websiteCreateUser Website[] @relation("createUser") + teamUser TeamUser[] + report Report[] @@map("user") } @@ -66,11 +68,13 @@ model Website { resetAt DateTime? @map("reset_at") @db.Timestamptz(6) userId String? @map("user_id") @db.Uuid teamId String? @map("team_id") @db.Uuid + createdBy String? @map("created_by") @db.Uuid createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6) updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6) deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6) - user User? @relation(fields: [userId], references: [id]) + user User? @relation("user", fields: [userId], references: [id]) + createUser User? @relation("createUser", fields: [createdBy], references: [id]) team Team? @relation(fields: [teamId], references: [id]) eventData EventData[] report Report[] @@ -80,6 +84,7 @@ model Website { @@index([teamId]) @@index([createdAt]) @@index([shareId]) + @@index([createdBy]) @@map("website") } From e915074c4b2b0ef0c0050eb28290cde7e64fe8e6 Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Mon, 5 Feb 2024 12:09:57 -0800 Subject: [PATCH 2/2] populate created_by in migration script --- db/mysql/migrations/04_team_redesign/migration.sql | 3 +++ db/postgresql/migrations/04_team_redesign/migration.sql | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/db/mysql/migrations/04_team_redesign/migration.sql b/db/mysql/migrations/04_team_redesign/migration.sql index 7580f77a..57f8dc93 100644 --- a/db/mysql/migrations/04_team_redesign/migration.sql +++ b/db/mysql/migrations/04_team_redesign/migration.sql @@ -16,6 +16,9 @@ ALTER TABLE `user` ADD COLUMN `display_name` VARCHAR(255) NULL, ALTER TABLE `website` ADD COLUMN `created_by` VARCHAR(36) NULL, ADD COLUMN `team_id` VARCHAR(36) NULL; +-- MigrateData +UPDATE "website" SET created_by = user_id WHERE team_id IS NULL; + -- DropTable DROP TABLE `team_website`; diff --git a/db/postgresql/migrations/04_team_redesign/migration.sql b/db/postgresql/migrations/04_team_redesign/migration.sql index a63d2d0b..68b96ab0 100644 --- a/db/postgresql/migrations/04_team_redesign/migration.sql +++ b/db/postgresql/migrations/04_team_redesign/migration.sql @@ -16,6 +16,9 @@ ADD COLUMN "logo_url" VARCHAR(2183); ALTER TABLE "website" ADD COLUMN "created_by" UUID, ADD COLUMN "team_id" UUID; +-- MigrateData +UPDATE "website" SET created_by = user_id WHERE team_id IS NULL; + -- DropTable DROP TABLE "team_website"; @@ -24,3 +27,5 @@ CREATE INDEX "website_team_id_idx" ON "website"("team_id"); -- CreateIndex CREATE INDEX "website_created_by_idx" ON "website"("created_by"); + +