Skip to content

Create migration script for reporting #1316

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

Merged
merged 1 commit into from
May 8, 2025

Conversation

HarshP4585
Copy link
Collaborator

@HarshP4585 HarshP4585 commented May 8, 2025

Describe your changes

  • Added migration script for adding Report to file source enum

CC: @eieimon

Write your issue number after "Fixes "

Fixes #1313

Please ensure all items are checked off before requesting a review:

  • I deployed the code locally.
  • I have performed a self-review of my code.
  • I have included the issue # in the PR.
  • I have labelled the PR correctly.
  • The issue I am working on is assigned to me.
  • I didn't use any hardcoded values (otherwise it will not scale, and will make it difficult to maintain consistency across the application).
  • I made sure font sizes, color choices etc are all referenced from the theme.
  • My PR is granular and targeted to one specific feature.
  • I took a screenshot or a video and attached to this PR if there is a UI change.

Summary by CodeRabbit

  • New Features
    • Added support for "Report" as a valid source category when uploading and managing files.
    • Users can now upload files with "Report" as their source type.

@HarshP4585 HarshP4585 self-assigned this May 8, 2025
@HarshP4585 HarshP4585 added the backend Backend related tasks/issues label May 8, 2025
Copy link
Contributor

coderabbitai bot commented May 8, 2025

Walkthrough

The changes introduce a new "Report" value to the source field for files, updating the PostgreSQL enum via a migration, TypeScript type definitions, and Sequelize model. The uploadFile utility function is also updated to accept "Report" as a valid source, ensuring consistency across the backend codebase.

Changes

File(s) Change Summary
Servers/database/migrations/20250508061148-update-enum_files_source-reporting.js Adds a migration script to include 'Report' in the enum_files_source PostgreSQL enum.
Servers/models/file.model.ts Updates TypeScript interfaces and Sequelize model to include "Report" as a valid source value for files.
Servers/utils/fileUpload.utils.ts Expands the source parameter type in uploadFile to accept "Report" in addition to existing values.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant API
    participant FileModel
    participant Database

    User->>API: Upload file with source = "Report"
    API->>FileModel: Create new File entry (source: "Report")
    FileModel->>Database: Insert file record (source: "Report")
    Database-->>FileModel: Confirmation
    FileModel-->>API: File created
    API-->>User: Success response
Loading

Assessment against linked issues

Objective Addressed Explanation
Create migration script for reporting - BE (#1313)

Poem

A hop, a skip, a brand-new source,
"Report" now joins the file’s course!
Through models, types, and enums too,
The backend’s ready for something new.
With every upload, bunnies cheer,
For tidy migrations, let’s all hear:
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @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
Contributor

@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: 1

🧹 Nitpick comments (3)
Servers/database/migrations/20250508061148-update-enum_files_source-reporting.js (3)

1-1: Remove redundant 'use strict' directive

JavaScript modules are automatically in strict mode, so this directive is unnecessary.

-'use strict';
🧰 Tools
🪛 Biome (1.9.4)

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)


8-11: Consider adding a check if the enum value already exists

To make the migration more robust, consider checking if the enum value already exists before attempting to add it. This prevents errors when running the migration multiple times.

 await queryInterface.sequelize.query(
-  `ALTER TYPE enum_files_source ADD VALUE 'Report';`,
+  `DO $$ 
+   BEGIN 
+     IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'Report' AND 
+                   enumtypid = (SELECT oid FROM pg_type WHERE typname = 'enum_files_source')) THEN
+       ALTER TYPE enum_files_source ADD VALUE 'Report';
+     END IF;
+   END $$;`,
   { transaction }
 );

19-21: Consider implementing a proper 'down' method

While removing enum values in PostgreSQL is complex and your comment is valid, it's good practice to have a complete migration. For a proper 'down' method, you could create a workaround to handle this limitation.

 async down(queryInterface, Sequelize) {
-  // The enum value won't cause any issues if it is not removed
+  // PostgreSQL doesn't support removing enum values directly
+  // We would need to:
+  // 1. Create a new enum type without the 'Report' value
+  // 2. Update the column to use the new type
+  // 3. Drop the old type
+  // This is complex and potentially risky, so we're leaving it as is
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4a3ab11 and 5bf375f.

📒 Files selected for processing (3)
  • Servers/database/migrations/20250508061148-update-enum_files_source-reporting.js (1 hunks)
  • Servers/models/file.model.ts (3 hunks)
  • Servers/utils/fileUpload.utils.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
Servers/database/migrations/20250508061148-update-enum_files_source-reporting.js

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)

🔇 Additional comments (2)
Servers/database/migrations/20250508061148-update-enum_files_source-reporting.js (1)

5-17: LGTM! Good use of transactions for safe migrations

The migration correctly uses a transaction to ensure atomic changes, with proper error handling and rollback in case of failure. This is a best practice for database migrations.

Servers/models/file.model.ts (1)

11-11: LGTM! All type definitions and model configurations properly updated

The changes consistently add "Report" as a valid source value across all relevant places:

  1. The File interface type definition
  2. The FileType interface type definition
  3. The Sequelize model's ENUM column definition and type annotation

This thorough approach ensures type safety and database consistency.

Also applies to: 21-21, 63-65

@@ -10,7 +10,7 @@ export const uploadFile = async (
file: UploadedFile,
user_id: number,
project_framework_id: number,
source: "Assessment tracker group" | "Compliance tracker group",
source: "Assessment tracker group" | "Compliance tracker group" | "Report",
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

LGTM! Type definition correctly updated for 'Report' source

The type definition for the source parameter has been appropriately expanded to include the new "Report" option, maintaining consistency with the database migration and model changes.

Consider whether any downstream consumers of this function need to be updated to handle the new source type. If the source type affects file handling behavior elsewhere in the application, those areas might need updates as well.


🏁 Script executed:

#!/bin/bash
# Check for any other references to the source field that might need updating
rg -A 1 -B 1 "source: (\"Assessment tracker group\" \| \"Compliance tracker group\")" --type ts --glob "!Servers/utils/fileUpload.utils.ts" --glob "!Servers/models/file.model.ts"

Length of output: 176


🏁 Script executed:

#!/bin/bash
# Search for any mentions of the source literal values in TypeScript files
rg "Assessment tracker group" --type ts
rg "Compliance tracker group" --type ts

# Inspect the 'source' property definition in the file model to ensure it's updated
rg "source" -A2 -B2 Servers/models/file.model.ts

Length of output: 2570


Ensure ‘Report’ source is handled downstream

The type definition and model enum have been correctly expanded to include "Report", but a search across the codebase did not uncover any calls to uploadFile(..., "Report"):

  • Controllers (reporting.ctrl.ts, file.ctrl.ts) still only use "Assessment tracker group"
  • subControl.utils.ts only uses "Compliance tracker group"
  • No other TS files pass "Report" to uploadFile

Please add or update any controllers, services, or UI components that should upload files with the "Report" source to ensure this new option is actually used.

@HarshP4585 HarshP4585 requested a review from samuel-coutinho May 8, 2025 17:43
@samuel-coutinho samuel-coutinho merged commit 17e24bd into develop May 8, 2025
1 check passed
@samuel-coutinho samuel-coutinho deleted the hp-may-8-update-files-source-enum branch May 8, 2025 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Backend related tasks/issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create migration script for reporting - BE
2 participants