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: dependent field queries not printing the org id #231

Conversation

JustARatherRidiculouslyLongUsername
Copy link
Contributor

@JustARatherRidiculouslyLongUsername JustARatherRidiculouslyLongUsername commented Dec 12, 2024

app.clickup.com

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Improved data cleanup process for workspace deletions by adding new deletion operations.
    • Enhanced context in deletion notices for better clarity.
  • Style

    • Adjusted formatting for improved readability in output messages.

Copy link

coderabbitai bot commented Dec 12, 2024

Walkthrough

The changes primarily involve modifications to the delete_workspace function within the scripts/sql/reset_db.sql file. A new deletion operation has been added to remove records from the expense_attributes_deletion_cache table based on the provided _workspace_id. The assignment of the _org_id variable has been updated to retrieve the org_id from the workspaces table instead of the previously used fyle_org_id. The function continues to execute multiple DELETE statements and reports the number of affected rows using RAISE NOTICE, with minor formatting adjustments for clarity.

Changes

File Change Summary
scripts/sql/reset_db.sql Modified delete_workspace function to introduce _org_id variable; updated assignment to retrieve org_id from workspaces; added deletion from expense_attributes_deletion_cache; adjusted spacing around RAISE NOTICE statements.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Database
    User->>Database: Call delete_workspace(_workspace_id)
    Database->>Database: Retrieve org_id from workspaces
    Database->>Database: Execute DELETE statements
    Database->>Database: GET DIAGNOSTICS for affected rows
    Database->>User: RAISE NOTICE with affected rows
    Database->>User: RETURN
Loading

Possibly related PRs

Suggested reviewers

  • ashwin1111

Poem

🐇 In the garden where data grows,
A workspace now gently goes.
With _org_id shining bright,
We tidy up, all feels right.
RAISE NOTICE, hear the cheer,
Clean and clear, the path is near! 🌼


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7ccc6e0 and 1911088.

📒 Files selected for processing (1)
  • scripts/sql/reset_db.sql (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • scripts/sql/reset_db.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.

Copy link

PR description must contain a link to a ClickUp (case-insensitive)

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

🧹 Outside diff range and nitpick comments (2)
scripts/sql/reset_db.sql (2)

206-207: Improve clarity and readability of the notice messages.

While the org_id is now correctly included, there are several improvements we can make to these notice messages:

  1. Reduce the excessive newlines that make logs harder to read
  2. Add more context about why these queries need to be run in prod
  3. Format the SQL queries for better readability

Consider this improvement:

-  RAISE NOTICE E'\n\n\n\n\n\n\n\n\nSwitch to prod db and run the below queries to delete dependent fields';
-  RAISE NOTICE E'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;
+  RAISE NOTICE E'\n=== Manual Steps Required ===';
+  RAISE NOTICE 'The following queries must be executed in the PRODUCTION database to clean up dependent fields for org_id: %', _org_id;
+  RAISE NOTICE E'BEGIN;
+    -- Delete dependent field mappings
+    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 dependent fields
+    DELETE FROM platform_schema.expense_fields 
+    WHERE org_id = ''%'' AND type = ''DEPENDENT_SELECT'';
+COMMIT;', _org_id, _org_id;

This format:

  • Clearly indicates these are manual steps
  • Shows these are production database operations
  • Formats the SQL for better readability
  • Adds comments explaining each operation
  • Uses a single newline for separation

Line range hint 1-210: Consider adding transaction management and error handling.

While the specific changes look good, there are some architectural improvements that could make this function more robust:

  1. Add explicit transaction management to ensure atomic operations
  2. Validate workspace existence before proceeding
  3. Add error handling for failed deletions

Consider wrapping the operations in a transaction and adding validation:

CREATE OR REPLACE FUNCTION delete_workspace(IN _workspace_id integer) RETURNS void AS $$
DECLARE
  rcount integer;
  _org_id varchar(255);
BEGIN
  -- Validate workspace exists
  IF NOT EXISTS (SELECT 1 FROM workspaces WHERE id = _workspace_id) THEN
    RAISE EXCEPTION 'Workspace % does not exist', _workspace_id;
  END IF;

  -- Start transaction
  BEGIN
    -- Existing delete operations...

    -- Commit if all operations succeed
    COMMIT;
  EXCEPTION WHEN OTHERS THEN
    -- Rollback on any error
    ROLLBACK;
    RAISE EXCEPTION 'Failed to delete workspace %: %', _workspace_id, SQLERRM;
  END;
END
$$ LANGUAGE plpgsql;

This would:

  • Ensure all operations succeed or none do
  • Prevent attempts to delete non-existent workspaces
  • Provide clear error messages on failure
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ba48fed and 7ccc6e0.

📒 Files selected for processing (1)
  • scripts/sql/reset_db.sql (1 hunks)
🔇 Additional comments (1)
scripts/sql/reset_db.sql (1)

198-199: LGTM! Strategic placement of org_id retrieval.

The _org_id is now correctly retrieved from the workspaces table before the workspace deletion. This fixes the issue where dependent field queries weren't printing the org id.

Copy link

PR description must contain a link to a ClickUp (case-insensitive)

Copy link

Tests Skipped Failures Errors Time
157 0 💤 0 ❌ 0 🔥 12.130s ⏱️

Copy link

Tests Skipped Failures Errors Time
157 0 💤 0 ❌ 0 🔥 12.050s ⏱️

@JustARatherRidiculouslyLongUsername JustARatherRidiculouslyLongUsername merged commit def3e25 into dependent-field-notice-after-reset Dec 13, 2024
3 checks passed
JustARatherRidiculouslyLongUsername added a commit that referenced this pull request Dec 13, 2024
)

* fix: raise notice to delete dependent fields after workspace reset

* refactor: squish query into one-liner

* fix: dependent field queries not printing the org id (#231)

* fix: dependent field queries not printing the org id

* fix: delete `expense_attributes_deletion_cache` while workspace reset (#232)
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.

3 participants