Skip to content

chore: Adding migration to make user email unique in an organization#39567

Merged
trishaanand merged 1 commit intoreleasefrom
chore/making-user-unique-in-org
Mar 6, 2025
Merged

chore: Adding migration to make user email unique in an organization#39567
trishaanand merged 1 commit intoreleasefrom
chore/making-user-unique-in-org

Conversation

@trishaanand
Copy link
Contributor

@trishaanand trishaanand commented Mar 5, 2025

Description

Chore migration to make emails unique only in an organization instead of entire instance.

Fixes #Issue Number
or
Fixes Issue URL

Warning

If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.

Automation

/test all

🔍 Cypress test results

Caution

🔴 🔴 🔴 Some tests have failed.
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/13673657485
Commit: a9c44d6
Cypress dashboard.
Tags: @tag.All
Spec:
The following are new failures, please fix them before merging the PR:

  1. cypress/e2e/Regression/ServerSide/GenerateCRUD/Postgres2_Spec.ts
List of identified flaky tests.
Wed, 05 Mar 2025 11:24:33 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • Chores
    • Enhanced backend processing of user email data associated with organizational information, leading to improved query performance and data consistency.

@trishaanand trishaanand added the ok-to-test Required label for CI label Mar 5, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 5, 2025

Walkthrough

A new migration class, Migration067_UpdateUserEmailIndex, has been added to handle database changes. The migration logs its operations, attempts to drop an existing email index, and creates a new unique compound index on both email and organizationId. The rollback method is present but currently contains no logic. Exceptions during execution are logged and rethrown.

Changes

File(s) Change Summary
app/.../Migration067_UpdateUserEmailIndex.java Added a migration class with two methods: execute(MongoTemplate) which logs the start, drops an existing email index (if it exists), and creates a unique compound index on email and organizationId; and rollbackExecution(), currently empty.

Sequence Diagram(s)

sequenceDiagram
    participant M as Migration067_UpdateUserEmailIndex
    participant DB as MongoTemplate
    participant L as Logger

    M->>L: Log start of migration
    M->>DB: dropIndexIfExists (index on email)
    DB-->>M: Index dropped/confirmation
    M->>DB: ensureIndexes (create unique compound index on email & organizationId)
    DB-->>M: Index created/confirmation
    alt Exception occurs
        M->>L: Log exception
        M->>M: Rethrow exception
    end
Loading

Suggested labels

Bug, Low, Production, Needs Triaging, Platform Administration Pod, RBAC Product, Workflows Pod, Workflows Product

Suggested reviewers

  • AnaghHegde
  • abhvsn
  • sharat87

Poem

A new migration takes its flight,
Index adjustments shining bright.
Email and org in a compound dance,
Logs and errors join the prance.
Cheers to progress, code takes flight!
🚀✨

✨ 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.

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

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 skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Mar 5, 2025
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 (1)
app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration067_UpdateUserEmailIndex.java (1)

39-42: Consider more specific error handling.

The current error handling simply logs and rethrows any exception. Consider handling specific MongoDB exceptions separately to provide more helpful error messages and recovery options.

} catch (Exception e) {
    log.error("Error updating user email index", e);
+   if (e instanceof org.springframework.dao.DuplicateKeyException) {
+       log.error("Duplicate keys found. This might indicate existing users with the same email in the same organization.");
+   } else if (e instanceof org.springframework.dao.DataAccessResourceFailureException) {
+       log.error("Database connection issue during migration. Please verify MongoDB connection.");
+   }
    throw e;
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 92fa95f and a9c44d6.

📒 Files selected for processing (1)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration067_UpdateUserEmailIndex.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: perform-test / rts-build / build
  • GitHub Check: perform-test / client-build / client-build
  • GitHub Check: perform-test / server-build / server-unit-tests
  • GitHub Check: server-unit-tests / server-unit-tests
  • GitHub Check: server-spotless / spotless-check
🔇 Additional comments (3)
app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration067_UpdateUserEmailIndex.java (3)

30-36: LGTM on compound index creation.

The compound index creation with uniqueness constraint on both email and organizationId is properly implemented, which will ensure emails are unique within an organization rather than globally.


1-45:

Details

❓ Verification inconclusive

Verify data integrity after migration.

Since this migration changes from a global email uniqueness to org-scoped uniqueness, verify that there are no unexpected data integrity issues that could arise during the migration if duplicate emails already exist across organizations.


🏁 Script executed:

#!/bin/bash
# Script to check for potential duplicate emails across organizations
echo "Checking for potential user email conflicts across organizations..."

# Sample MongoDB query to find any users with same email but different organizations
# In a real environment, you would use appropriate MongoDB connection details
cat <<'EOF' > check_duplicates.js
db.getCollection("users").aggregate([
    { $group: { 
        _id: "$email", 
        count: { $sum: 1 }, 
        organizations: { $addToSet: "$organizationId" }
    }},
    { $match: { count: { $gt: 1 } } }
])
EOF

echo "In a production environment, you would execute the above MongoDB query to identify potential conflicts."
echo "If conflicts exist, they should be resolved before running this migration."

Length of output: 526


Data Integrity Verification Reminder

Please ensure that the migration's shift from a global email uniqueness constraint to an organization-scoped uniqueness constraint does not lead to data integrity issues. Specifically, before deploying this change, verify that there are no duplicate emails across different organizations. The sample MongoDB query provided below illustrates how one might identify potential conflicts:

  • Location: app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration067_UpdateUserEmailIndex.java
  • Action Items:
    • Run the provided aggregation query in your production environment to check for any email conflicts.
    • Resolve any detected duplicates prior to executing the migration.

If the verification script finds potential conflicts, address them to prevent runtime issues post-migration.


26-27:

Details

❓ Verification inconclusive

Verify existing index before dropping.

Consider adding a check to verify the existing index structure before dropping it, to ensure data consistency.


🏁 Script executed:

#!/bin/bash
# Check for current index structure in the codebase or related migrations
rg -A 5 "new Index\(\).+on\(\"email\"" --type java

Length of output: 50


Action on Email Index Verification:
The current migration code drops the "email" index without first verifying its existing structure. Our initial search did not reveal any sign of a pre-drop check (e.g., no evidence of a corresponding new Index(...) configuration), which raises a potential risk for inconsistent state if the index's structure has changed unexpectedly.

Recommendations:

  • Review the dropIndexIfExists Method: Check if it already performs any implicit validation of the existing index structure.
  • Consider Index Structure Verification: If no internal checks are present, add a manual verification step (or MongoDB query) to confirm that the existing index matches the expected configuration before dropping it.

Please verify these points manually as the automated search was inconclusive.

Comment on lines +19 to +20
@RollbackExecution
public void rollbackExecution() {}
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

Empty rollback method needs implementation.

The rollback method is currently empty. Implement a proper rollback strategy that would restore the original email index if the migration needs to be reversed.

@RollbackExecution
public void rollbackExecution() {}
+public void rollbackExecution(MongoTemplate mongoTemplate) {
+    log.info("Rolling back migration for user email index");
+    try {
+        // Drop the compound index
+        dropIndexIfExists(mongoTemplate, User.class, "email_organizationId_compound_index");
+        
+        // Recreate the original email index
+        Index emailIndex = new Index()
+                .on("email", ASC)
+                .unique()
+                .named("email");
+                
+        ensureIndexes(mongoTemplate, User.class, emailIndex);
+        
+        log.info("Completed rollback of user email index migration");
+    } catch (Exception e) {
+        log.error("Error rolling back user email index migration", e);
+        throw e;
+    }
+}

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

@trishaanand trishaanand merged commit eca0a66 into release Mar 6, 2025
85 of 87 checks passed
@trishaanand trishaanand deleted the chore/making-user-unique-in-org branch March 6, 2025 18:51
github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Apr 12, 2025
…ppsmithorg#39567)

## Description
Chore migration to make emails unique only in an organization instead of
entire instance.


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/test all
### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!CAUTION]
> 🔴 🔴 🔴 Some tests have failed.
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/13673657485>
> Commit: a9c44d6
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=13673657485&attempt=1&selectiontype=test&testsstatus=failed&specsstatus=fail"
target="_blank">Cypress dashboard</a>.
> Tags: @tag.All
> Spec: 
> The following are new failures, please fix them before merging the PR:
<ol>
>
<li>cypress/e2e/Regression/ServerSide/GenerateCRUD/Postgres2_Spec.ts</ol>
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/identified-flaky-tests-65890b3c81d7400d08fa9ee3?branch=master"
target="_blank">List of identified flaky tests</a>.
> <hr>Wed, 05 Mar 2025 11:24:33 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Chores**
- Enhanced backend processing of user email data associated with
organizational information, leading to improved query performance and
data consistency.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test Required label for CI skip-changelog Adding this label to a PR prevents it from being listed in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants