Skip to content

Comments

[WEB-2631] chore: changed the cascading logic for soft delete#5823

Closed
NarayanBavisetti wants to merge 0 commit intopreviewfrom
chore/soft-deletion-cascading
Closed

[WEB-2631] chore: changed the cascading logic for soft delete#5823
NarayanBavisetti wants to merge 0 commit intopreviewfrom
chore/soft-deletion-cascading

Conversation

@NarayanBavisetti
Copy link
Collaborator

@NarayanBavisetti NarayanBavisetti commented Oct 14, 2024

chore:

  • changed the cascading logic for soft delete

Issue Link: WEB-2631

Summary by CodeRabbit

  • New Features

    • Enhanced user notification preferences with distinct fields for email and in-app notifications, allowing for more tailored settings.
    • Added a new choice, "inbox," to the deploy board options.
  • Bug Fixes

    • Improved the logic for soft-deleting related objects to ensure correct handling based on relationship types.
  • Refactor

    • Updated the conditional checks in the soft delete functionality for better accuracy regarding the on_delete attribute.
    • Simplified the instantiation process for user notification preferences.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 14, 2024

Walkthrough

The changes in this pull request modify the soft_delete_related_objects function in the apiserver/plane/bgtasks/deletion_task.py file. The main alteration involves the conditional check for the on_delete attribute of a model field. The logic now checks if on_delete is either non-existent or equals the string "CASCADE". This affects how related objects are soft-deleted based on their relationship type. Additionally, several migration changes are introduced to the usernotificationpreference and deployboard models, enhancing the notification preferences and modifying the DeployBoard class.

Changes

File Path Change Summary
apiserver/plane/bgtasks/deletion_task.py Modified soft_delete_related_objects to change the conditional check for on_delete attribute.
apiserver/plane/db/migrations/0081_remove_user... Renamed fields in usernotificationpreference, added new notification preference fields, and modified deployboard model.
apiserver/plane/db/models/deploy_board.py Added is_disabled field and new choice "inbox" to TYPE_CHOICES in DeployBoard.
apiserver/plane/db/models/notification.py Removed old notification fields and added new granular fields for email and in-app preferences.
apiserver/plane/db/models/user.py Simplified instantiation of UserNotificationPreference by removing certain parameters.

Possibly related PRs

  • [WEB-2092] chore: soft delete operation #5244: This PR introduces a soft delete operation, which is directly related to the changes in the soft_delete_related_objects function in the main PR, as both involve managing the deletion of related objects.
  • [WEB-2504] chore: issue relation hard delete #5671: This PR implements a hard delete functionality for issue relations, which aligns with the main PR's focus on deletion logic, specifically in the context of soft and hard deletion processes.
  • [WEB-2600] fix: estimate point deletion #5762: This PR addresses the deletion of estimate points and their associated issues, which connects to the main PR's modifications in deletion behavior and logic for related objects.

Suggested labels

🔄migrations

Suggested reviewers

  • pablohashescobar
  • sriramveeraghanta

Poem

In the meadow where bunnies play,
Soft deletes are here to stay.
With a hop and a skip, we change the way,
Related objects find their day.
"CASCADE" sings, as we softly sway! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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

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 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: 0

🧹 Outside diff range and nitpick comments (1)
apiserver/plane/bgtasks/deletion_task.py (1)

24-25: Review cascading behavior and consider implementing restore_related_objects

The changes to soft_delete_related_objects may have broader implications:

  1. The modified condition for soft deletion might affect the cascading behavior. Ensure this aligns with the intended functionality across the application.

  2. The restore_related_objects function is still not implemented. This could lead to inconsistencies between deletion and restoration processes.

Consider the following improvements:

  1. Implement the restore_related_objects function to ensure consistency between deletion and restoration processes.
  2. Add unit tests to verify the new soft deletion behavior, especially for edge cases where on_delete is not defined.
  3. Update documentation to reflect the changes in cascading behavior during soft deletions.

Would you like assistance in implementing the restore_related_objects function or creating unit tests for the new soft deletion behavior?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 701af73 and 1f5cbe9.

📒 Files selected for processing (1)
  • apiserver/plane/bgtasks/deletion_task.py (1 hunks)
🧰 Additional context used
🔇 Additional comments (1)
apiserver/plane/bgtasks/deletion_task.py (1)

24-25: ⚠️ Potential issue

Consider using Django's models.CASCADE instead of string comparison

The change in the conditional check for the on_delete attribute introduces a potential issue:

  1. The use of the string "CASCADE" instead of models.CASCADE might lead to inconsistencies if Django's internals change in the future.
  2. The logic now includes cases where on_delete doesn't exist, which might unintentionally broaden the scope of soft deletion.

Consider refactoring the condition to use Django's models.CASCADE:

from django.db import models

# ...

if (
    not hasattr(field.remote_field, "on_delete")
    or field.remote_field.on_delete == models.CASCADE
):
    # ... rest of the code

This change ensures consistency with Django's API and makes the code more robust against potential future changes in Django.

To ensure this change doesn't introduce unintended side effects, please run the following script to check for any other occurrences of string-based CASCADE comparisons:

✅ Verification successful

[/run_scripts]


Refactor to Use Django's models.CASCADE

The review comment is valid. Only the identified line in apiserver/plane/bgtasks/deletion_task.py uses a string comparison for on_delete. To maintain consistency and robustness, update this line to use Django's models.CASCADE:

from django.db import models

# ...

if (
    not hasattr(field.remote_field, "on_delete")
    or field.remote_field.on_delete == models.CASCADE
):
    # ... rest of the code
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other occurrences of string-based CASCADE comparisons
rg --type python '"CASCADE"' apiserver/

Length of output: 72


Script:

#!/bin/bash
# Search for other occurrences of string-based CASCADE comparisons using the correct type
rg --type py '"CASCADE"' apiserver/

Length of output: 143

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: 2

🧹 Outside diff range and nitpick comments (2)
apiserver/plane/db/models/deploy_board.py (2)

23-23: LGTM: New "inbox" choice added to TYPE_CHOICES

The addition of "inbox" to TYPE_CHOICES is consistent with the existing structure and naming convention. This change expands the options for the entity_name field, potentially supporting new inbox-related functionality.

Consider updating any relevant documentation or comments to reflect this new option and its intended use case.


Line range hint 1-63: Overall assessment: Changes align with PR objectives

The modifications to the DeployBoard model, including the addition of the "inbox" choice and the is_disabled field, are consistent with the PR objectives of changing the cascading logic for soft delete. These changes appear to be part of a larger feature implementation or system update.

Consider the following architectural points:

  1. Ensure that the new "inbox" type and the is_disabled field are properly handled in all relevant parts of the system, including views, serializers, and any caching mechanisms.
  2. If the is_disabled field affects the soft delete functionality, make sure it's consistently used across related models and operations.
  3. Update any documentation, API specifications, and test cases to reflect these new changes.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1f5cbe9 and a5dc0c5.

📒 Files selected for processing (4)
  • apiserver/plane/db/migrations/0081_remove_usernotificationpreference_comment_and_more.py (1 hunks)
  • apiserver/plane/db/models/deploy_board.py (2 hunks)
  • apiserver/plane/db/models/notification.py (1 hunks)
  • apiserver/plane/db/models/user.py (0 hunks)
💤 Files with no reviewable changes (1)
  • apiserver/plane/db/models/user.py
🧰 Additional context used
🔇 Additional comments (6)
apiserver/plane/db/models/deploy_board.py (1)

45-45: LGTM: New is_disabled field added to DeployBoard model

The addition of the is_disabled field with a default value of False is a good way to implement a disable/enable feature for deploy boards. This change allows for more flexible management of deploy boards.

Please ensure the following:

  1. A corresponding migration file has been created for this model change.
  2. Any existing queries or business logic that interact with deploy boards have been updated to consider this new field.
  3. The UI and API have been updated to support this new field if necessary.

To verify the impact on existing code, you can run:

apiserver/plane/db/models/notification.py (2)

106-116: ⚠️ Potential issue

Verify the impact of changing default values for in-app notification preferences

Similar to the email preferences, the in-app notification preference fields are also initialized with default=False. If in-app notifications were previously enabled by default, this change might disable them for existing users, potentially affecting their in-app experience.

Ensure that this change is intentional and consider communicating it to users if necessary.

Run the following script to check the default values of previous in-app notification settings:

#!/bin/bash
# Description: Verify previous default values for in-app notification preferences

# Since in-app notification fields are new, confirm if they replace any old fields with different defaults
rg --type py 'class UserNotificationPreference' -A 50 -- 'models/notification.py'

This script helps to determine if the in-app notification preferences are replacing any existing fields and what their default values were.


93-103: ⚠️ Potential issue

Verify the impact of changing default values for email notification preferences

The new email notification preference fields are initialized with default=False, whereas the previous fields had default=True. This change means that existing users who previously received email notifications will no longer receive them unless they manually update their preferences. This could lead to unintended disruptions in the user experience.

Please verify if setting these defaults to False is intentional and aligns with product requirements.

Run the following script to identify the default values of the previous notification preference fields:

This script searches for instances where the old email notification preference fields were defined with default=True to confirm the previous default settings.

apiserver/plane/db/migrations/0081_remove_usernotificationpreference_comment_and_more.py (3)

63-67: Approved: Addition of 'is_disabled' field to 'deployboard' model

The addition of the is_disabled Boolean field with a default value of False to the deployboard model is appropriate and follows best practices.


68-97: Approved: Addition of new notification preference fields

The introduction of email_* and in_app_* Boolean fields to the usernotificationpreference model enhances the granularity of user notification settings. The fields are correctly implemented with default values of False.

Also applies to: 99-152


153-168: ⚠️ Potential issue

Verify that existing 'entity_name' values conform to new choices

Adding choices to the entity_name field in the deployboard model restricts it to specific values. If existing records have values outside these choices, it could lead to IntegrityError exceptions when saving or accessing these records.

Recommendation:

Ensure that all existing entity_name values in the database match one of the new choices. Update any records with invalid values before applying the migration.

Verification Script:

Action Required:

  • Run the script to identify any hardcoded invalid entity_name assignments in the codebase.
  • Review and update any code or database records that do not conform to the new choices.

Comment on lines 92 to 116
# email preference fields
email_property_change = models.BooleanField(default=False)
email_state_change = models.BooleanField(default=False)
email_priority_change = models.BooleanField(default=False)
email_assignee_change = models.BooleanField(default=False)
email_start_target_date_change = models.BooleanField(default=False)
email_module_change = models.BooleanField(default=False)
email_cycle_change = models.BooleanField(default=False)
email_reactions = models.BooleanField(default=False)
email_comment = models.BooleanField(default=False)
email_mention = models.BooleanField(default=False)
email_issue_completed = models.BooleanField(default=False)

# in app preference fields
in_app_property_change = models.BooleanField(default=False)
in_app_state_change = models.BooleanField(default=False)
in_app_priority_change = models.BooleanField(default=False)
in_app_assignee_change = models.BooleanField(default=False)
in_app_start_target_date_change = models.BooleanField(default=False)
in_app_module_change = models.BooleanField(default=False)
in_app_cycle_change = models.BooleanField(default=False)
in_app_reactions = models.BooleanField(default=False)
in_app_comment = models.BooleanField(default=False)
in_app_mention = models.BooleanField(default=False)
in_app_issue_completed = models.BooleanField(default=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider refactoring notification preferences for scalability and maintainability

Adding numerous BooleanFields for each notification type leads to a bloated model and can make future extensions cumbersome. As the number of notification types grows, the model will become increasingly complex and harder to manage.

Consider the following refactoring options:

  • Use a JSONField: Store notification preferences in a JSONField, allowing for dynamic and flexible storage. This approach makes it easier to add new notification types without modifying the database schema.
  • Create a separate related model: Introduce a related model (e.g., NotificationPreference) with fields for the preference type and delivery method (email or in-app). Use a foreign key or many-to-many relationship to associate preferences with users.

Implementing one of these approaches can improve scalability, reduce code duplication, and enhance maintainability.

Comment on lines 13 to 22
migrations.RenameField(
model_name="usernotificationpreference",
old_name="property_change",
new_name="email_property_change",
),
migrations.AlterField(
model_name="usernotificationpreference",
name="email_property_change",
field=models.BooleanField(default=False),
),
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Caution: Renaming and altering fields simultaneously may cause issues

Renaming a field and altering its type in the same migration can lead to problems on certain database backends, such as PostgreSQL. This is because the database might not correctly track the field's identity through both operations in a single migration.

Recommendation:

Consider splitting the RenameField and AlterField operations into separate migrations for each field. This ensures that the database correctly handles the field renaming before changing its type.

  1. First Migration: Perform the RenameField operation.
  2. Second Migration: Apply the AlterField to change the field type.

This approach minimizes the risk of data loss or migration errors.

Also applies to: 23-32, 33-42, 43-52, 53-62

@NarayanBavisetti NarayanBavisetti force-pushed the chore/soft-deletion-cascading branch from a5dc0c5 to 1f5cbe9 Compare October 14, 2024 13:56
@NarayanBavisetti NarayanBavisetti force-pushed the chore/soft-deletion-cascading branch from 1f5cbe9 to cf53cdf Compare October 14, 2024 13:59
@sriramveeraghanta sriramveeraghanta deleted the chore/soft-deletion-cascading branch February 10, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant