From 4c55610ec419537dbdbbd3e234116a86f6b11133 Mon Sep 17 00:00:00 2001 From: Nicolas Drolet Date: Thu, 17 Jul 2025 13:06:41 -0400 Subject: [PATCH] fix(schema): change user id field to uuid instead of text, remove some unique constraints --- .gitignore | 2 +- .../20250715133954_init/migration.sql | 62 -------- .../20250715142024_nullable/migration.sql | 32 ---- .../20250717164330_dev_v1/migration.sql | 144 ++++++++++++++++++ prisma/schema.prisma | 40 ++--- 5 files changed, 165 insertions(+), 115 deletions(-) delete mode 100644 prisma/migrations/20250715133954_init/migration.sql delete mode 100644 prisma/migrations/20250715142024_nullable/migration.sql create mode 100644 prisma/migrations/20250717164330_dev_v1/migration.sql diff --git a/.gitignore b/.gitignore index 78cff21..fd17afc 100644 --- a/.gitignore +++ b/.gitignore @@ -38,7 +38,7 @@ lerna-debug.log* # dotenv environment variable files .env .env.development.local -.env.test.local +.env.staging.local .env.production.local .env.local diff --git a/prisma/migrations/20250715133954_init/migration.sql b/prisma/migrations/20250715133954_init/migration.sql deleted file mode 100644 index 4e8173a..0000000 --- a/prisma/migrations/20250715133954_init/migration.sql +++ /dev/null @@ -1,62 +0,0 @@ --- CreateEnum -CREATE TYPE "roles" AS ENUM ('USER', 'ADMIN', 'CUSTOMER', 'DEALER', 'EMPLOYEE', 'HR', 'SUPERVISER', 'ACCOUNTING'); - --- CreateTable -CREATE TABLE "users" ( - "user_id" SERIAL NOT NULL, - "first_name" TEXT NOT NULL, - "last_name" TEXT NOT NULL, - "email" TEXT NOT NULL, - "phone_number" INTEGER NOT NULL, - "residence" TEXT NOT NULL, - "role" "roles" NOT NULL DEFAULT 'USER', - - CONSTRAINT "users_pkey" PRIMARY KEY ("user_id") -); - --- CreateTable -CREATE TABLE "employees" ( - "employee_id" SERIAL NOT NULL, - "user_id" INTEGER NOT NULL, - "external_payroll_id" INTEGER NOT NULL, - "company_code" INTEGER NOT NULL, - "first_work_day" TIMESTAMP(3) NOT NULL, - "last_work_day" TIMESTAMP(3) NOT NULL, - "role" "roles" NOT NULL DEFAULT 'EMPLOYEE', - - CONSTRAINT "employees_pkey" PRIMARY KEY ("employee_id") -); - --- CreateTable -CREATE TABLE "customers" ( - "customer_id" SERIAL NOT NULL, - "user_id" INTEGER NOT NULL, - "invoice_id" INTEGER NOT NULL, - "role" "roles" NOT NULL DEFAULT 'CUSTOMER', - - CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id") -); - --- CreateIndex -CREATE UNIQUE INDEX "users_email_key" ON "users"("email"); - --- CreateIndex -CREATE UNIQUE INDEX "users_phone_number_key" ON "users"("phone_number"); - --- CreateIndex -CREATE UNIQUE INDEX "employees_user_id_key" ON "employees"("user_id"); - --- CreateIndex -CREATE UNIQUE INDEX "employees_external_payroll_id_key" ON "employees"("external_payroll_id"); - --- CreateIndex -CREATE UNIQUE INDEX "customers_user_id_key" ON "customers"("user_id"); - --- CreateIndex -CREATE UNIQUE INDEX "customers_invoice_id_key" ON "customers"("invoice_id"); - --- AddForeignKey -ALTER TABLE "employees" ADD CONSTRAINT "employees_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("user_id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "customers" ADD CONSTRAINT "customers_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("user_id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20250715142024_nullable/migration.sql b/prisma/migrations/20250715142024_nullable/migration.sql deleted file mode 100644 index 50783b6..0000000 --- a/prisma/migrations/20250715142024_nullable/migration.sql +++ /dev/null @@ -1,32 +0,0 @@ -/* - Warnings: - - - The values [SUPERVISER] on the enum `roles` will be removed. If these variants are still used in the database, this will fail. - -*/ --- AlterEnum -BEGIN; -CREATE TYPE "roles_new" AS ENUM ('USER', 'ADMIN', 'CUSTOMER', 'DEALER', 'EMPLOYEE', 'HR', 'SUPERVISOR', 'ACCOUNTING'); -ALTER TABLE "customers" ALTER COLUMN "role" DROP DEFAULT; -ALTER TABLE "employees" ALTER COLUMN "role" DROP DEFAULT; -ALTER TABLE "users" ALTER COLUMN "role" DROP DEFAULT; -ALTER TABLE "users" ALTER COLUMN "role" TYPE "roles_new" USING ("role"::text::"roles_new"); -ALTER TABLE "employees" ALTER COLUMN "role" TYPE "roles_new" USING ("role"::text::"roles_new"); -ALTER TABLE "customers" ALTER COLUMN "role" TYPE "roles_new" USING ("role"::text::"roles_new"); -ALTER TYPE "roles" RENAME TO "roles_old"; -ALTER TYPE "roles_new" RENAME TO "roles"; -DROP TYPE "roles_old"; -ALTER TABLE "customers" ALTER COLUMN "role" SET DEFAULT 'CUSTOMER'; -ALTER TABLE "employees" ALTER COLUMN "role" SET DEFAULT 'EMPLOYEE'; -ALTER TABLE "users" ALTER COLUMN "role" SET DEFAULT 'USER'; -COMMIT; - --- AlterTable -ALTER TABLE "customers" ALTER COLUMN "invoice_id" DROP NOT NULL; - --- AlterTable -ALTER TABLE "employees" ALTER COLUMN "last_work_day" DROP NOT NULL; - --- AlterTable -ALTER TABLE "users" ALTER COLUMN "email" DROP NOT NULL, -ALTER COLUMN "residence" DROP NOT NULL; diff --git a/prisma/migrations/20250717164330_dev_v1/migration.sql b/prisma/migrations/20250717164330_dev_v1/migration.sql new file mode 100644 index 0000000..20bbc1a --- /dev/null +++ b/prisma/migrations/20250717164330_dev_v1/migration.sql @@ -0,0 +1,144 @@ +-- CreateEnum +CREATE TYPE "roles" AS ENUM ('ADMIN', 'SUPERVISOR', 'HR', 'ACCOUNTING', 'EMPLOYEE', 'DEALER', 'CUSTOMER', 'GUEST'); + +-- CreateEnum +CREATE TYPE "leave_types" AS ENUM ('SICK', 'VACATION', 'UNPAID', 'BEREAVEMENT', 'PARENTAL', 'LEGAL'); + +-- CreateEnum +CREATE TYPE "leave_approval_status" AS ENUM ('PENDING', 'APPROVED', 'DENIED', 'CANCELLED', 'ESCALATED'); + +-- CreateTable +CREATE TABLE "users" ( + "id" UUID NOT NULL DEFAULT gen_random_uuid(), + "first_name" TEXT NOT NULL, + "last_name" TEXT NOT NULL, + "email" TEXT NOT NULL, + "phone_number" INTEGER NOT NULL, + "residence" TEXT, + "role" "roles" NOT NULL DEFAULT 'GUEST', + + CONSTRAINT "users_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "employees" ( + "id" SERIAL NOT NULL, + "user_id" UUID NOT NULL, + "external_payroll_id" INTEGER NOT NULL, + "company_code" INTEGER NOT NULL, + "first_work_day" TIMESTAMP(3) NOT NULL, + "last_work_day" TIMESTAMP(3), + + CONSTRAINT "employees_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "customers" ( + "id" SERIAL NOT NULL, + "user_id" UUID NOT NULL, + "invoice_id" INTEGER, + + CONSTRAINT "customers_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "leave_requests" ( + "id" SERIAL NOT NULL, + "employee_id" INTEGER NOT NULL, + "leave_type" "leave_types" NOT NULL, + "start_date_time" TIMESTAMP(3) NOT NULL, + "end_date_time" TIMESTAMP(3), + "comment" TEXT NOT NULL, + "approval_status" "leave_approval_status" NOT NULL DEFAULT 'PENDING', + + CONSTRAINT "leave_requests_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "timesheets" ( + "id" SERIAL NOT NULL, + "employee_id" INTEGER NOT NULL, + "is_approved" BOOLEAN NOT NULL DEFAULT false, + + CONSTRAINT "timesheets_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "shifts" ( + "id" SERIAL NOT NULL, + "timesheet_id" INTEGER NOT NULL, + "shift_code_id" INTEGER NOT NULL, + "date" TIMESTAMP(3) NOT NULL, + "start_time" TIMESTAMP(3) NOT NULL, + "end_time" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "shifts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "shift_codes" ( + "id" SERIAL NOT NULL, + "shift_type" TEXT NOT NULL, + "bank_code" TEXT NOT NULL, + + CONSTRAINT "shift_codes_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "refresh_tokens" ( + "id" TEXT NOT NULL, + "user_id" UUID NOT NULL, + "application" TEXT NOT NULL, + "access_token" TEXT NOT NULL, + "refresh_token" TEXT NOT NULL, + "access_token_expiry" TIMESTAMP(3) NOT NULL, + "refresh_token_expiry" TIMESTAMP(3), + "is_revoked" BOOLEAN NOT NULL DEFAULT false, + "scopes" JSONB NOT NULL DEFAULT '[]', + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3), + + CONSTRAINT "refresh_tokens_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "users_email_key" ON "users"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "users_phone_number_key" ON "users"("phone_number"); + +-- CreateIndex +CREATE UNIQUE INDEX "employees_user_id_key" ON "employees"("user_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "customers_user_id_key" ON "customers"("user_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "customers_invoice_id_key" ON "customers"("invoice_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "refresh_tokens_access_token_key" ON "refresh_tokens"("access_token"); + +-- CreateIndex +CREATE UNIQUE INDEX "refresh_tokens_refresh_token_key" ON "refresh_tokens"("refresh_token"); + +-- AddForeignKey +ALTER TABLE "employees" ADD CONSTRAINT "employees_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "customers" ADD CONSTRAINT "customers_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "leave_requests" ADD CONSTRAINT "leave_requests_employee_id_fkey" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "timesheets" ADD CONSTRAINT "timesheets_employee_id_fkey" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "shifts" ADD CONSTRAINT "shifts_timesheet_id_fkey" FOREIGN KEY ("timesheet_id") REFERENCES "timesheets"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "shifts" ADD CONSTRAINT "shifts_shift_code_id_fkey" FOREIGN KEY ("shift_code_id") REFERENCES "shift_codes"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "refresh_tokens" ADD CONSTRAINT "refresh_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 68a05ac..cfb79d2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -10,22 +10,22 @@ generator client { datasource db { provider = "postgresql" - url = env("DATABASE_URL") + url = env("DATABASE_URL_DEV") } model Users { - id String @id @default(uuid()) + id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid first_name String last_name String - email String @unique - phone_number Int @unique + email String @unique + phone_number Int @unique residence String? - role Roles @default(GUEST) + role Roles @default(GUEST) - employee Employees? @relation("UserEmployee") - customer Customers? @relation("UserCustomer") - oauth_access_tokens OAuthAccessTokens? @relation("UserOAuthAccessToken") + employee Employees? @relation("UserEmployee") + customer Customers? @relation("UserCustomer") + oauth_access_tokens OAuthAccessTokens[] @relation("UserOAuthAccessToken") @@map("users") } @@ -33,14 +33,14 @@ model Users { model Employees { id Int @id @default(autoincrement()) user Users @relation("UserEmployee", fields: [user_id], references:[id]) - user_id String @unique - external_payroll_id Int @unique + user_id String @unique @db.Uuid + external_payroll_id Int company_code Int first_work_day DateTime last_work_day DateTime? - timesheet Timesheets? @relation("TimesheetEmployee") - leave_request LeaveRequests? @relation("LeaveRequestEmployee") + timesheet Timesheets[] @relation("TimesheetEmployee") + leave_request LeaveRequests[] @relation("LeaveRequestEmployee") @@map("employees") } @@ -48,7 +48,7 @@ model Employees { model Customers { id Int @id @default(autoincrement()) user Users @relation("UserCustomer", fields: [user_id], references:[id]) - user_id String @unique + user_id String @unique @db.Uuid invoice_id Int? @unique @@map("customers") @@ -57,7 +57,7 @@ model Customers { model LeaveRequests { id Int @id @default(autoincrement()) employee Employees @relation("LeaveRequestEmployee", fields: [employee_id], references:[id]) - employee_id Int @unique + employee_id Int leave_type LeaveTypes start_date_time DateTime end_date_time DateTime? @@ -70,11 +70,11 @@ model LeaveRequests { model Timesheets { id Int @id @default(autoincrement()) employee Employees @relation("TimesheetEmployee", fields: [employee_id], references:[id]) - employee_id Int @unique + employee_id Int is_approved Boolean @default(false) - shift Shifts? @relation("ShiftTimesheet") + shift Shifts[] @relation("ShiftTimesheet") @@map("timesheets") } @@ -82,9 +82,9 @@ model Timesheets { model Shifts { id Int @id @default(autoincrement()) timesheet Timesheets @relation("ShiftTimesheet", fields: [timesheet_id], references: [id]) - timesheet_id Int @unique + timesheet_id Int shift_code ShiftCodes @relation("ShiftShiftCode", fields: [shift_code_id], references: [id]) - shift_code_id Int @unique + shift_code_id Int date DateTime start_time DateTime end_time DateTime @@ -98,7 +98,7 @@ model ShiftCodes { bank_code String - shift Shifts? @relation("ShiftShiftCode") + shift Shifts[] @relation("ShiftShiftCode") @@map("shift_codes") } @@ -106,7 +106,7 @@ model ShiftCodes { model OAuthAccessTokens { id String @id @default(cuid()) user Users @relation("UserOAuthAccessToken", fields: [user_id], references: [id]) - user_id String @unique + user_id String @db.Uuid application String access_token String @unique refresh_token String @unique