Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user module #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add user module #2

wants to merge 2 commits into from

Conversation

lovelgeorge99
Copy link
Collaborator

@lovelgeorge99 lovelgeorge99 commented Feb 11, 2025

Summary by CodeRabbit

  • New Features
    • Updated environment configuration with clear MySQL variable placeholders.
    • Expanded polling capabilities by introducing support for polls, options, tags, and votes.
    • Enhanced user management with a new creation endpoint and enriched profile attributes.
    • Upgraded dependencies to improve data validation and database operations.

Copy link

coderabbitai bot commented Feb 11, 2025

Walkthrough

The pull request introduces environment variable placeholders, new dependencies, and several new SQL migration scripts. The migrations create and modify tables for a polling application, while the Prisma schema is updated with corresponding models. Additionally, a migration lock file is added. New files under the src directory implement user management functionality, including a controller, service, and DTO for user creation, as well as a Prisma service for database initialization.

Changes

File(s) Change Summary
.env.example Added new environment variable placeholders: DATABASE_URL, MYSQL_DATABASE, and MYSQL_ROOT_PASSWORD.
package.json Added dependencies: @prisma/client (^6.3.1), class-transformer (^0.5.1), and class-validator (^0.14.1).
prisma/migrations/20250205172850_/migration.sql Introduced a new migration to create the User table with columns (id, worldId, username, name, createdAt) and unique indexes on worldId and username.
prisma/migrations/20250205180025_user_entity/migration.sql Modified the User table by dropping the name column and adding pollsCreatedCount (default 0) and profilePicture columns, with a warning about data loss.
prisma/migrations/20250205193050_initail_entities/migration.sql Added migrations to create new tables: Poll, Option, Tag, Vote, and _PollToTag with corresponding columns and foreign key constraints to support relationships for a polling application.
prisma/migrations/migration_lock.toml New file added with migration lock configuration specifying provider = "postgresql".
prisma/schema.prisma Updated Prisma schema by introducing new models: User, Poll, Option, Tag, and Vote to align with the new database structure.
src/app.module.ts Updated to import UsersModule and include UsersController for user-related operations.
src/prisma.service.ts Introduced PrismaService which extends PrismaClient and implements OnModuleInit to connect to the database when the module initializes.
src/users/dtos/CreateUser.dto.ts, src/users/users.controller.ts, src/users/users.module.ts, src/users/users.service.ts Implemented new user management functionality:
CreateUserDto: Defines user data with validation.
UsersController: Handles POST requests for user creation with validation.
UsersModule: Groups and provides user-related components.
UsersService: Uses PrismaService to create new users in the database.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant UsersController
    participant UsersService
    participant PrismaService
    participant Database

    Client->>UsersController: POST /users with CreateUserDto
    UsersController->>UsersService: createUser(data)
    UsersService->>PrismaService: user.create(data)
    PrismaService->>Database: $connect() / SQL INSERT
    Database-->>PrismaService: Confirmation
    PrismaService-->>UsersService: User record created
    UsersService-->>UsersController: Return user details
    UsersController-->>Client: Send Response
Loading

Poem

In a garden of code where new paths unfold,
I hop with joy as fresh changes are told.
Tables and modules spring up overnight,
With envs and dependencies shining so bright.
As I nibble on bytes in a digital spree,
I celebrate this update—happy as can be! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 8

🧹 Nitpick comments (8)
src/users/users.module.ts (1)

2-2: Consider using path aliases for imports.

Instead of using relative paths, consider configuring and using path aliases in tsconfig.json for better maintainability.

Apply this diff:

-import { PrismaService } from 'src/prisma.service';
+import { PrismaService } from '@app/prisma.service';

Add this to tsconfig.json:

{
  "compilerOptions": {
    "paths": {
      "@app/*": ["src/*"]
    }
  }
}
src/users/users.controller.ts (1)

13-13: Fix naming convention in constructor injection.

Follow NestJS conventions by using the same name for the parameter as the service type.

Apply this diff:

-  constructor(private userService: UsersService) {}
+  constructor(private usersService: UsersService) {}
prisma/migrations/20250205172850_/migration.sql (1)

1-17: Consider naming conventions and indexing strategy.

  1. The migration filename lacks a descriptive name, making it harder to track changes.
  2. The table name uses PascalCase, which might cause issues with some tools. Consider using snake_case.
  3. Consider adding an index on the createdAt column if you plan to query by date ranges.

Apply this diff to fix the table name:

-- CreateTable
-CREATE TABLE "User" (
+CREATE TABLE "users" (
     "id" SERIAL NOT NULL,
     "worldId" TEXT NOT NULL,
     "username" TEXT NOT NULL,
     "name" TEXT NOT NULL,
     "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

-    CONSTRAINT "User_pkey" PRIMARY KEY ("id")
+    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
 );

 -- CreateIndex
-CREATE UNIQUE INDEX "User_worldId_key" ON "User"("worldId");
+CREATE UNIQUE INDEX "users_worldId_key" ON "users"("worldId");

 -- CreateIndex
-CREATE UNIQUE INDEX "User_username_key" ON "User"("username");
+CREATE UNIQUE INDEX "users_username_key" ON "users"("username");

+-- CreateIndex
+CREATE INDEX "users_createdAt_idx" ON "users"("createdAt");

Also, rename the migration file to something more descriptive, like 20250205172850_create_users_table.sql.

prisma/schema.prisma (3)

16-25: Add indexes and onDelete behavior to User model.

Consider the following improvements:

  1. Add an index on createdAt for efficient sorting/filtering
  2. Define onDelete behavior for the Poll relation
 model User{
   id Int @id @default(autoincrement())
   worldId String @unique()
   username String @unique()
   profilePicture String?
   pollsCreatedCount Int @default(0)
-  createdAt DateTime @default(now())
+  createdAt DateTime @default(now()) @map("created_at") @@index([createdAt(sort: Desc)])
-  poll Poll[]
+  poll Poll[] @relation("UserPolls", onDelete: Cascade)
 }

43-50: Add index and onDelete behavior to Option model.

Consider the following improvements:

  1. Add index on pollId for faster lookups
  2. Define onDelete behavior for relations
 model Option {
   id      Int    @id @default(autoincrement())
   optionText     String 
-  poll    Poll   @relation(fields: [pollId], references: [id])
+  poll    Poll   @relation("PollOptions", fields: [pollId], references: [id])
   pollId  Int    
-  votes  Vote[]
+  votes  Vote[] @relation("OptionVotes", onDelete: Cascade)
   createdAt  DateTime @default(now())
+  @@index([pollId])
 }

52-57: Add unique constraint to Tag model.

Consider adding a unique constraint on the content field to prevent duplicate tags.

 model Tag{
   id Int @id @default(autoincrement())
-  content String?
+  content String? @unique
-  poll Poll[]
+  poll Poll[] @relation("PollTags")
 }
prisma/migrations/20250205193050_initail_entities/migration.sql (2)

16-23: Add index to Option table.

Add an index on pollId for better query performance when fetching options for a poll.

 CREATE TABLE "Option" (
     "id" SERIAL NOT NULL,
     "optionText" TEXT NOT NULL,
     "pollId" INTEGER NOT NULL,
     "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

     CONSTRAINT "Option_pkey" PRIMARY KEY ("id")
 );
+CREATE INDEX "Option_pollId_idx" ON "Option"("pollId");

1-1: Fix typo in migration filename.

The migration filename contains a typo: "initail" should be "initial".

Rename the file from 20250205193050_initail_entities to 20250205193050_initial_entities.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 06e098a and 97f1649.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (13)
  • .env.example (1 hunks)
  • package.json (1 hunks)
  • prisma/migrations/20250205172850_/migration.sql (1 hunks)
  • prisma/migrations/20250205180025_user_entity/migration.sql (1 hunks)
  • prisma/migrations/20250205193050_initail_entities/migration.sql (1 hunks)
  • prisma/migrations/migration_lock.toml (1 hunks)
  • prisma/schema.prisma (1 hunks)
  • src/app.module.ts (1 hunks)
  • src/prisma.service.ts (1 hunks)
  • src/users/dtos/CreateUser.dto.ts (1 hunks)
  • src/users/users.controller.ts (1 hunks)
  • src/users/users.module.ts (1 hunks)
  • src/users/users.service.ts (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • prisma/migrations/migration_lock.toml
  • .env.example
🧰 Additional context used
🪛 ESLint
src/app.module.ts

[error] 4-4: 'UsersService' is defined but never used.

(@typescript-eslint/no-unused-vars)

🔇 Additional comments (3)
src/users/dtos/CreateUser.dto.ts (1)

1-15: LGTM! Well-structured DTO with appropriate validation rules.

The DTO is well-designed with proper validation decorators and consistent naming conventions.

prisma/migrations/20250205180025_user_entity/migration.sql (1)

1-10: Consider data migration strategy before dropping the name column.

The migration drops the required name column without preserving or migrating the existing data. This could lead to data loss in production.

Consider these options:

  1. If the column is no longer needed, ensure there's no valuable data before applying the migration.
  2. If the data needs to be preserved, create a separate migration to:
    • Copy data to a new column if needed
    • Make the column optional first
    • Then drop it in a subsequent migration

Run this script to check if there's existing data:

❌ Verification inconclusive

🏁 Script executed:

#!/bin/bash
# Description: Check if there's data in the name column before dropping it

# Connect to the database and count records with non-null names
psql "$DATABASE_URL" -c "SELECT COUNT(*) FROM \"User\" WHERE name IS NOT NULL;"

Length of output: 161


Action Required: Manually Verify Data Preservation for the name Column

The executed script did not produce output because the environment lacks the psql command, so we couldn’t automatically verify if there’s data in the name column before it’s dropped. Please run the query on an environment where psql is available (for example, your production or staging database) to confirm whether any non-null data exists. If data is present, consider employing a migration strategy that preserves it (e.g., copying to a new column before dropping) to prevent potential data loss.

package.json (1)

26-26: ⚠️ Potential issue

Verify @prisma/client version.

The specified version ^6.3.1 seems incorrect as Prisma's current major version is 5.x.

Update to the correct version:

-    "@prisma/client": "^6.3.1",
+    "@prisma/client": "^5.9.1",
❌ Incorrect review comment

🌐 Web query:

What is the latest version of @prisma/client?

💡 Result:

The latest stable version of @prisma/client as of February 2025 is 6.3.1, released as part of Prisma ORM v6[1][8]. This release introduced several improvements:

Key updates in v6.x

  • Prisma Studio enhancements:
    • Unified model navigation with persistent UI state[1]
    • Bulk row editing via sidebar[1]
  • Breaking changes:
    • Minimum supported Node.js versions: v18 (18.18+), v20 (20.9+), v22 (22.11+)[10]
    • TypeScript requirement increased to v5.1+[10]
  • New features:
    • updateManyAndReturn method[2]
    • ULID support[2]

For production upgrades:

npm install @prisma/client@latest
npm install -D prisma@latest
npx prisma generate

Always check the Prisma Upgrade Guide before major version migrations[2][7].

Citations:


@prisma/client version is correct.

The web query confirms that the latest stable version as of February 2025 is indeed 6.3.1. No changes to the dependency are needed.

Likely an incorrect or invalid review comment.

Comment on lines +6 to +10
async onModuleInit() {
await this.$connect()
.then(() => console.log('Connected to DB'))
.catch((err) => console.log(err));
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance error handling and logging.

  1. Use NestJS Logger instead of console.log for consistent logging across the application.
  2. Consider throwing the error to prevent the application from starting with a failed DB connection.

Apply this diff:

-  async onModuleInit() {
-    await this.$connect()
-      .then(() => console.log('Connected to DB'))
-      .catch((err) => console.log(err));
+  private readonly logger = new Logger(PrismaService.name);
+
+  async onModuleInit() {
+    try {
+      await this.$connect();
+      this.logger.log('Successfully connected to database');
+    } catch (error) {
+      this.logger.error('Failed to connect to database', error);
+      throw error;
+    }
   }

Don't forget to import Logger:

import { Injectable, Logger, OnModuleInit } from '@nestjs/common';

Comment on lines +9 to +12
async createUser(data: Prisma.UserCreateInput) {
console.log(data);
return this.prisma.user.create({ data });
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling, logging, and type safety.

  1. Replace console.log with NestJS Logger
  2. Add error handling for database operations
  3. Add return type annotation

Apply this diff:

+  private readonly logger = new Logger(UsersService.name);
+
-  async createUser(data: Prisma.UserCreateInput) {
-    console.log(data);
-    return this.prisma.user.create({ data });
+  async createUser(data: Prisma.UserCreateInput): Promise<Prisma.User> {
+    this.logger.debug('Creating user', data);
+    try {
+      return await this.prisma.user.create({ data });
+    } catch (error) {
+      this.logger.error('Failed to create user', error);
+      throw new InternalServerErrorException('Failed to create user');
+    }
   }

Don't forget to import:

import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common';

Comment on lines +4 to +6
import { UsersService } from './users/users.service';
import { UsersController } from './users/users.controller';
import { UsersModule } from './users/users.module';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove unused imports and fix module configuration.

  1. The UsersService import is unused and can be removed.
  2. The UsersController is already provided through UsersModule, so it should be removed from the controllers array to avoid duplicate registration.

Apply this diff to fix the issues:

-import { UsersService } from './users/users.service';
-import { UsersController } from './users/users.controller';
 import { UsersModule } from './users/users.module';

 @Module({
   imports: [UsersModule],
-  controllers: [AppController, UsersController],
+  controllers: [AppController],
   providers: [AppService],
 })

Also applies to: 9-10

🧰 Tools
🪛 ESLint

[error] 4-4: 'UsersService' is defined but never used.

(@typescript-eslint/no-unused-vars)

Comment on lines +15 to +19
@Post()
@UsePipes(ValidationPipe)
async createUser(@Body() createUserDto: CreateUserDto) {
return await this.userService.createUser(createUserDto);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance method implementation with response type and error handling.

  1. Update the service name to match the constructor.
  2. Add response type for better type safety.
  3. Add error handling for potential database conflicts.

Apply this diff:

   @Post()
   @UsePipes(ValidationPipe)
-  async createUser(@Body() createUserDto: CreateUserDto) {
-    return await this.userService.createUser(createUserDto);
+  async createUser(@Body() createUserDto: CreateUserDto): Promise<User> {
+    try {
+      return await this.usersService.createUser(createUserDto);
+    } catch (error) {
+      if (error.code === 'P2002') {
+        throw new ConflictException('User with this worldId or username already exists');
+      }
+      throw error;
+    }
   }

Don't forget to add these imports:

import { ConflictException } from '@nestjs/common';
import { User } from '@prisma/client';

Comment on lines +28 to +41
model Poll{
id Int @id @default(autoincrement())
user User @relation(fields: [userId],references: [id])
userId Int
title String
description String?
options Option[]
tags Tag[]
createdAt DateTime @default(now())
startDate DateTime @default(now())
endDate DateTime
isAnonymous Boolean @default(false)

}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation and indexes to Poll model.

Consider the following improvements:

  1. Add validation to ensure startDate is before endDate
  2. Add index on userId for faster lookups
  3. Define onDelete behavior for relations
 model Poll{
   id Int @id @default(autoincrement())
-  user User @relation(fields: [userId],references: [id])
+  user User @relation("UserPolls", fields: [userId],references: [id])
   userId  Int 
   title String 
   description String?
-  options Option[]
+  options Option[] @relation("PollOptions", onDelete: Cascade)
-  tags Tag[]
+  tags Tag[] @relation("PollTags")
   createdAt DateTime @default(now())
   startDate DateTime @default(now())
   endDate DateTime
   isAnonymous Boolean @default(false)

+  @@index([userId])
+  @@check(startDate <= endDate)
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
model Poll{
id Int @id @default(autoincrement())
user User @relation(fields: [userId],references: [id])
userId Int
title String
description String?
options Option[]
tags Tag[]
createdAt DateTime @default(now())
startDate DateTime @default(now())
endDate DateTime
isAnonymous Boolean @default(false)
}
model Poll{
id Int @id @default(autoincrement())
user User @relation("UserPolls", fields: [userId], references: [id])
userId Int
title String
description String?
options Option[] @relation("PollOptions", onDelete: Cascade)
tags Tag[] @relation("PollTags")
createdAt DateTime @default(now())
startDate DateTime @default(now())
endDate DateTime
isAnonymous Boolean @default(false)
@@index([userId])
@@check(startDate <= endDate)
}

Comment on lines +59 to +66
model Vote{
id Int @id @default(autoincrement())
weight Int
option Option @relation(fields: [optionId], references: [id])
optionId Int
createdAt DateTime @default(now())
proof String[]
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation and index to Vote model.

Consider the following improvements:

  1. Add validation for weight field
  2. Add index on optionId for faster lookups
  3. Consider using JSONB for proof array if using PostgreSQL
 model Vote{
   id Int @id @default(autoincrement())
-  weight Int 
+  weight Int @check(weight > 0)
-  option Option @relation(fields: [optionId], references: [id])
+  option Option @relation("OptionVotes", fields: [optionId], references: [id])
   optionId  Int
   createdAt DateTime @default(now())
-  proof String[] 
+  proof String[] @db.JsonB
+  @@index([optionId])
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
model Vote{
id Int @id @default(autoincrement())
weight Int
option Option @relation(fields: [optionId], references: [id])
optionId Int
createdAt DateTime @default(now())
proof String[]
}
model Vote{
id Int @id @default(autoincrement())
weight Int @check(weight > 0)
option Option @relation("OptionVotes", fields: [optionId], references: [id])
optionId Int
createdAt DateTime @default(now())
proof String[] @db.JsonB
@@index([optionId])
}

Comment on lines +34 to +42
CREATE TABLE "Vote" (
"id" SERIAL NOT NULL,
"weight" INTEGER NOT NULL,
"optionId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"proof" TEXT[],

CONSTRAINT "Vote_pkey" PRIMARY KEY ("id")
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add constraints and index to Vote table.

Consider adding:

  1. Check constraint for positive weight values
  2. Index on optionId for better query performance
  3. Use JSONB type for proof array
 CREATE TABLE "Vote" (
     "id" SERIAL NOT NULL,
     "weight" INTEGER NOT NULL,
     "optionId" INTEGER NOT NULL,
     "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
-    "proof" TEXT[],
+    "proof" JSONB,

-    CONSTRAINT "Vote_pkey" PRIMARY KEY ("id")
+    CONSTRAINT "Vote_pkey" PRIMARY KEY ("id"),
+    CONSTRAINT "Vote_weight_check" CHECK ("weight" > 0)
 );
+CREATE INDEX "Vote_optionId_idx" ON "Vote"("optionId");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
CREATE TABLE "Vote" (
"id" SERIAL NOT NULL,
"weight" INTEGER NOT NULL,
"optionId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"proof" TEXT[],
CONSTRAINT "Vote_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "Vote" (
"id" SERIAL NOT NULL,
"weight" INTEGER NOT NULL,
"optionId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"proof" JSONB,
CONSTRAINT "Vote_pkey" PRIMARY KEY ("id"),
CONSTRAINT "Vote_weight_check" CHECK ("weight" > 0)
);
CREATE INDEX "Vote_optionId_idx" ON "Vote"("optionId");

Comment on lines +1 to +13
-- CreateTable
CREATE TABLE "Poll" (
"id" SERIAL NOT NULL,
"userId" INTEGER NOT NULL,
"title" TEXT NOT NULL,
"description" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"startDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"endDate" TIMESTAMP(3) NOT NULL,
"isAnonymous" BOOLEAN NOT NULL DEFAULT false,

CONSTRAINT "Poll_pkey" PRIMARY KEY ("id")
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add missing indexes and constraints to Poll table.

Consider adding:

  1. Index on userId for better query performance
  2. Check constraint to ensure startDate is before endDate
 CREATE TABLE "Poll" (
     "id" SERIAL NOT NULL,
     "userId" INTEGER NOT NULL,
     "title" TEXT NOT NULL,
     "description" TEXT,
     "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
     "startDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
     "endDate" TIMESTAMP(3) NOT NULL,
     "isAnonymous" BOOLEAN NOT NULL DEFAULT false,

     CONSTRAINT "Poll_pkey" PRIMARY KEY ("id")
+    CONSTRAINT "Poll_dates_check" CHECK ("startDate" <= "endDate")
 );
+CREATE INDEX "Poll_userId_idx" ON "Poll"("userId");

Committable suggestion skipped: line range outside the PR's diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant