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

fix: raise notice to delete dependent fields after workspace reset #583

Merged

Conversation

JustARatherRidiculouslyLongUsername
Copy link
Contributor

@JustARatherRidiculouslyLongUsername JustARatherRidiculouslyLongUsername commented Dec 10, 2024

Clickup

app.clickup.com

Summary by CodeRabbit

  • New Features
    • Enhanced workspace deletion process with additional cleanup for dependent records.
    • Added user instructions for executing necessary deletion queries in the production database.

Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

Walkthrough

The changes involve modifications to the delete_workspace function in the SQL script located at scripts/sql/functions/delete-workspace.sql. The function now includes additional SQL statements to delete dependent expense field mappings and expense fields of type DEPENDENT_SELECT associated with a workspace. New RAISE NOTICE statements have been added to instruct users to switch to the production database for executing the necessary deletion queries. These changes enhance the cleanup process when a workspace is deleted.

Changes

File Path Change Summary
scripts/sql/functions/delete-workspace.sql Modified delete_workspace function to include new SQL statements for deleting dependent mappings and fields. Added RAISE NOTICE instructions for user actions in production. Wrapped new deletion commands in a transaction block.

Poem

In the garden where workspaces bloom,
We tidy up, making room! 🌼
With fields and mappings, we take our care,
Deleting the old, to make way for fair.
A hop, a skip, to clean up the space,
In our SQL world, we find our place! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0ca5d72 and 0423550.

📒 Files selected for processing (1)
  • scripts/sql/functions/delete-workspace.sql (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • scripts/sql/functions/delete-workspace.sql

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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @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.

@github-actions github-actions bot added the size/XS Extra Small PR label Dec 10, 2024
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)
scripts/sql/functions/delete-workspace.sql (2)

336-338: Improve SQL statement formatting and readability

The SQL statements in the notice can be improved for better readability:

  1. Add proper indentation
  2. Split the long line into multiple lines
  3. Use consistent quote style with the rest of the file
-delete from platform_schema.dependent_expense_field_mappings where expense_field_id in (select id from platform_schema.expense_fields where org_id =''%'' and type=''DEPENDENT_SELECT'');
-
-delete from platform_schema.expense_fields where org_id = ''%'' and type = ''DEPENDENT_SELECT'';\n\n\n\n\n\n\n\n\n\n\n'
+DELETE FROM platform_schema.dependent_expense_field_mappings 
+WHERE expense_field_id IN (
+    SELECT id 
+    FROM platform_schema.expense_fields 
+    WHERE org_id = ''%'' 
+    AND type = ''DEPENDENT_SELECT''
+);
+
+DELETE FROM platform_schema.expense_fields 
+WHERE org_id = ''%'' 
+AND type = ''DEPENDENT_SELECT'';\n\n\n\n\n\n\n\n\n\n\n'

330-338: Consider adding diagnostic counts for dependent field deletions

For consistency with the rest of the function, consider adding RAISE NOTICE statements with deletion counts after executing these queries in the production database.

Add these lines to the notice:

GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % dependent_expense_field_mappings', rcount;

GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % dependent expense_fields', rcount;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e1f7f3c and 0ca5d72.

📒 Files selected for processing (1)
  • scripts/sql/functions/delete-workspace.sql (1 hunks)

Comment on lines 333 to 334
rollback;
begin;
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

Remove unnecessary transaction block

The rollback and begin statements in the notice are unnecessary since these are just example queries to be run later. They should be removed to avoid confusion.

-rollback;
-begin;

Comment on lines 330 to 338
RAISE NOTICE E'\n\n\n\n\n\n\n\n\nSwitch to prod db and run the below queries to delete dependent fields


rollback;
begin;

delete from platform_schema.dependent_expense_field_mappings where expense_field_id in (select id from platform_schema.expense_fields where org_id =''%'' and type=''DEPENDENT_SELECT'');

delete from platform_schema.expense_fields where org_id = ''%'' and type = ''DEPENDENT_SELECT'';\n\n\n\n\n\n\n\n\n\n\n', _org_id, _org_id;
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

The deletion order is correct, but additional cleanup may be needed

The verification reveals:

  1. The mapping_settings table has a foreign key constraint to expense_fields, but it's DEFERRABLE INITIALLY DEFERRED, which means the deletion order won't be affected
  2. The current deletion order (dependent mappings before fields) is correct
  3. However, you should also clean up any related mapping_settings records before deleting the expense fields

Suggested cleanup order:

  • Delete from dependent_expense_field_mappings
  • Delete from mapping_settings where expense_field_id matches
  • Delete from expense_fields
🔗 Analysis chain

Verify the impact of deleting dependent fields

The new cleanup steps for dependent fields should be verified to ensure:

  1. No other tables have foreign key dependencies on these records
  2. The deletion order is correct (dependent mappings before fields)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential foreign key dependencies on dependent fields

# Search for foreign key references to dependent_expense_field_mappings
rg -i "foreign\s+key.*dependent_expense_field_mappings" 

# Search for foreign key references to expense_fields
rg -i "foreign\s+key.*expense_fields"

# Search for any other references to these tables that might indicate dependencies
rg -i "dependent_expense_field_mappings|expense_fields.*type\s*=\s*'DEPENDENT_SELECT'"

Length of output: 653


Script:

#!/bin/bash
# Let's check for any references to mapping_settings table and its relationship with expense_fields
rg -i "mapping_settings.*expense_field" -A 3 -B 3

# Also check if there are any other tables referencing expense_fields
ast-grep --pattern 'CREATE TABLE $_ (
  $$$
  FOREIGN KEY ($$$) REFERENCES expense_fields($$$)
  $$$
)'

# Check if mapping_settings table has any relation to DEPENDENT_SELECT type fields
rg -i "mapping_settings.*type.*DEPENDENT_SELECT" -A 3 -B 3

Length of output: 2782

Copy link

Tests Skipped Failures Errors Time
286 0 💤 0 ❌ 0 🔥 40.806s ⏱️

Copy link

Tests Skipped Failures Errors Time
286 0 💤 0 ❌ 0 🔥 41.828s ⏱️

@JustARatherRidiculouslyLongUsername JustARatherRidiculouslyLongUsername merged commit 609d157 into master Dec 13, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XS Extra Small PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants