Skip to content

chore: add unique index on organization slug in MongoDB #40198

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 2 commits into from
Apr 9, 2025

Conversation

abhvsn
Copy link
Contributor

@abhvsn abhvsn commented Apr 9, 2025

Description

  • Introduced a new migration to add a unique index on the 'slug' field of the Organization collection.
  • Ensured idempotence by dropping the existing index before creating the new one.
  • Added logging for successful index creation and error handling for potential issues during the process.

🔍 Cypress test results

Caution

If you modify the content in this section, you are likely to disrupt the CI result for your PR.

Communication

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

  • Yes
  • No

Summary by CodeRabbit

  • Refactor
    • Updated how organization identifiers are validated to ensure each remains unique.
  • Chores
    • Introduced a database update that enforces uniqueness for organization identifiers, enhancing data consistency and preventing duplicate entries.

@abhvsn abhvsn requested a review from sharat87 as a code owner April 9, 2025 10:06
Copy link
Contributor

coderabbitai bot commented Apr 9, 2025

Walkthrough

This update modifies how uniqueness is enforced on the slug field in the Organization domain. The @Unique annotation has been removed from the slug field in the Organization class, and a new migration class has been added. The migration, Migration073_AddUniqueIndexOnOrganizationSlug, uses a MongoTemplate to drop any existing index on slug and then creates a new unique index on this field, ensuring idempotence. The migration logs its operations and rethrows exceptions if they occur.

Changes

File(s) Change Summary
app/.../Organization.java Removed the @Unique annotation from the slug field, removing application-level unique constraint enforcement on the field.
app/.../Migration073_AddUniqueIndexOnOrganizationSlug.java Added a new migration class that enforces a unique index on the slug field at the database level, including idempotence, logging, and rollback stub.

Sequence Diagram(s)

sequenceDiagram
    participant Migration as Migration073_AddUniqueIndexOnOrganizationSlug
    participant Template as MongoTemplate
    participant DB as MongoDB

    Migration->>Template: Drop existing index on "slug" (if any)
    Template->>DB: Request index drop
    DB-->>Template: Confirm drop (or no-op)
    Migration->>Template: Create unique index on "slug"
    Template->>DB: Ensure unique index creation
    DB-->>Template: Return index creation result
    Template-->>Migration: Operation completed
    Migration->>Migration: Log success or throw exception on failure
Loading

Poem

In code we danced a subtle feat,
Where annotations bowed out neat.
A migration took the stage at night,
Dropping and forging indexes just right.
Logs sing the tale of change in flight,
A journey of bytes, precise and bright.
Cheers to progress in our code’s great light!

✨ 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 plan to trigger planning for file edits and PR creation.
  • @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 Enhancement New feature or request label Apr 9, 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: 0

🧹 Nitpick comments (1)
app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration073_AddUniqueIndexOnOrganizationSlug.java (1)

16-16: Consider adding an author name.

The author attribute in the @ChangeUnit annotation is empty. Adding the author's name improves traceability and makes it easier to know who to contact if there are questions about this migration in the future.

- @ChangeUnit(order = "073", id = "add-unique-index-on-organization-slug", author = "")
+ @ChangeUnit(order = "073", id = "add-unique-index-on-organization-slug", author = "abhvsn")
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0e6c556 and 8022d3e.

📒 Files selected for processing (2)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Organization.java (1 hunks)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration073_AddUniqueIndexOnOrganizationSlug.java (1 hunks)
🔇 Additional comments (5)
app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Organization.java (1)

22-22: Removal of @unique annotation is appropriate when moving constraint to database level

The removal of the @Unique annotation from the slug field is a good architectural change. This shifts uniqueness enforcement from the application level to the database level via the MongoDB unique index mentioned in the associated migration. Database-level constraints generally provide stronger guarantees and better performance compared to application-level validations.

Make sure the migration class (Migration073_AddUniqueIndexOnOrganizationSlug) properly handles potential duplicate values already in the database to avoid migration failures.

app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration073_AddUniqueIndexOnOrganizationSlug.java (4)

25-28: The empty rollback method looks good.

Having an empty rollback method with a clear comment is appropriate since there's no need to roll back an index creation - dropping the index would be handled in a separate migration if needed.


31-46: Well-structured migration implementation.

The migration properly handles idempotence by first dropping any existing index and then creating a new unique one. The error handling with logging and exception re-throwing is appropriate for a migration task.


38-38: Good use of background indexing.

Setting the index creation to run in the background with .background() is a good practice as it won't block database operations, especially important in production environments.


1-48:

❓ Verification inconclusive

Verify data integrity before deployment.

This migration adds a unique constraint on the Organization slug field. Before deploying to production, ensure there are no duplicate slug values in the Organization collection to prevent migration failures.


🏁 Script executed:

#!/bin/bash
# Check for duplicate organization slugs that would violate the unique constraint

echo "Checking for duplicate organization slugs..."
mongosh --eval '
  db = db.getSiblingDB("appsmith");
  const duplicates = db.organization.aggregate([
    { $group: { _id: "$slug", count: { $sum: 1 } } },
    { $match: { count: { $gt: 1 } } }
  ]).toArray();
  
  if (duplicates.length > 0) {
    print("WARNING: Found duplicate slugs that will violate the unique constraint:");
    duplicates.forEach(d => print(`Slug "${d._id}" appears ${d.count} times`));
  } else {
    print("No duplicate slugs found. Migration should proceed without issues.");
  }
'

Length of output: 706


Action Required: Verify Duplicate Organization Slugs

This migration adds a unique index on the Organization slug field. Before deploying, please verify that your production database does not contain duplicate slugs—which would lead to migration failures. The duplicate-check script attempted to run with mongosh but failed (command not found).

  • Verification Steps:
    • Ensure your environment has the appropriate MongoDB shell (such as mongosh or an equivalent) installed.
    • Manually verify that no duplicate slugs exist in the Organization collection, or update the script to use a available command.

@abhvsn abhvsn requested a review from trishaanand April 9, 2025 10:08
@abhvsn abhvsn changed the title feat: add unique index on organization slug in MongoDB chore: add unique index on organization slug in MongoDB Apr 9, 2025
@github-actions github-actions bot added skip-changelog Adding this label to a PR prevents it from being listed in the changelog and removed Enhancement New feature or request labels Apr 9, 2025
Copy link

github-actions bot commented Apr 9, 2025

Failed server tests

  • com.appsmith.server.services.ce.OrganizationServiceCETest#checkAndExecuteMigrationsForOrganizationFeatureFlags_withPendingMigration_getUpdatedOrganization

@abhvsn abhvsn merged commit e87479f into release Apr 9, 2025
19 checks passed
@abhvsn abhvsn deleted the chore/add-org-slug-unique-index branch April 9, 2025 14:50
github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Apr 12, 2025
…40198)

## Description

- Introduced a new migration to add a unique index on the 'slug' field
of the Organization collection.
- Ensured idempotence by dropping the existing index before creating the
new one.
- Added logging for successful index creation and error handling for
potential issues during the process.

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!CAUTION]  
> If you modify the content in this section, you are likely to disrupt
the CI result for your PR.

<!-- end of auto-generated comment: Cypress test results  -->


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


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

## Summary by CodeRabbit

- **Refactor**
- Updated how organization identifiers are validated to ensure each
remains unique.
- **Chores**
- Introduced a database update that enforces uniqueness for organization
identifiers, enhancing data consistency and preventing duplicate
entries.

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