Skip to content

Conversation

@awais786
Copy link
Contributor

@awais786 awais786 commented Nov 20, 2025

This pull request adds complete support for renaming Meilisearch indexes using both the Index.update() method and Client.swap_indexes().

Key Changes

Index Renaming via Index.update()Added a new optional parameter new_uid to the update() method. This allows renaming an index using the standard index update endpoint.

Example payload

{
  "newUid": "new_index_name"
}

Support via Client.swap_indexes()

No code changes required.

The rename operation can be passed directly in the swap payload using rename:True or False

Added Tests

Summary by CodeRabbit

  • New Features

    • Index renaming supported via the index update operation; update can accept a rename target and optionally update primary key in one call.
  • Bug Fixes

    • Update now validates inputs and raises an error if neither primary key nor rename target is provided.
  • Documentation

    • Clarified swap/rename semantics and examples; minor docs formatting tweaks.
  • Tests

    • Added tests covering renames, swap behavior with non-existent targets, and update validation.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 20, 2025

Walkthrough

Index.update() gained an optional new_uid parameter to support renaming; payload now includes "uid" when renaming and requires at least one of primary_key or new_uid. swap_indexes docstring and docs formatting were tweaked. Tests added for rename and swap behaviors.

Changes

Cohort / File(s) Summary
Configuration & Docs Formatting
docs/conf.py
Minor formatting: switched html_title quotes and normalized spacing in html_js_files.
Client docstring
meilisearch/client.py
Expanded swap_indexes() docstring with examples and explicit default rename behavior (no runtime change).
Index API (core change)
meilisearch/index.py
Index.update() signature changed to def update(self, primary_key: Optional[str] = None, new_uid: Optional[str] = None) -> TaskInfo; payload now conditionally includes "primaryKey" and/or "uid"; raises ValueError if both params are None.
Tests: swap behavior
tests/client/test_client_swap_meilisearch.py
Added tests for swapping when a target does not exist with rename=False (expects index not found) and rename=True (validates renamed UID creation).
Tests: index update & rename
tests/index/test_index.py
Added tests: test_rename_index, test_index_update_and_rename, and a test asserting update() without params raises ValueError mentioning primary_key and new_uid.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Index
    participant API

    Caller->>Index: update(primary_key?, new_uid?)
    rect rgb(245,250,240)
    Note over Index: Build payload conditionally
    Index->>Index: payload = {}
    alt primary_key provided
        Index->>Index: payload["primaryKey"] = primary_key
    end
    alt new_uid provided
        Index->>Index: payload["uid"] = new_uid
    end
    alt neither provided
        Index-->>Caller: raise ValueError
    end
    end
    Index->>API: PATCH /indexes/{uid} with payload
    API-->>Index: TaskInfo
    Index-->>Caller: TaskInfo
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review meilisearch/index.py update logic and the ValueError condition.
  • Verify tests in tests/index/test_index.py and tests/client/test_client_swap_meilisearch.py cover rename/no-rename branches and UID existence assertions.

Poem

🐰
I hopped to patch a tiny name,
new_uid in tow — a gentle game.
Tests keep watch as UIDs shift,
payloads patch, the changes lift.
A quiet tweak, a joyful claim.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title "Rename index" directly and clearly summarizes the main purpose of the pull request, which is to add renaming functionality to Meilisearch indexes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 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 1f6ee1a and e914130.

📒 Files selected for processing (1)
  • meilisearch/index.py (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
meilisearch/index.py (1)
meilisearch/models/task.py (1)
  • TaskInfo (77-106)
🪛 Ruff (0.14.5)
meilisearch/index.py

138-140: Avoid specifying long messages outside the exception class

(TRY003)

🔇 Additional comments (4)
meilisearch/index.py (4)

107-107: LGTM: Method signature properly supports both primary key updates and renames.

The signature change correctly makes both parameters optional, allowing the method to handle updating the primary key, renaming, or both simultaneously. The validation logic inside ensures at least one parameter is provided.


114-124: Excellent documentation addressing the stale object concern.

The docstring clearly warns users about the stale self.uid issue after rename and provides actionable guidance to obtain a fresh Index instance. This properly addresses the concern raised in previous reviews.


137-140: LGTM: Input validation prevents empty payload errors.

The validation correctly ensures at least one parameter is provided, addressing the concern from previous reviews about empty payload API errors. The error message is clear and helpful.

Note: The static analysis hint (TRY003) about message length is a minor style suggestion. For this simple validation case, the inline error message is perfectly appropriate.


142-147: LGTM: Payload construction correctly supports all update scenarios.

The conditional logic properly constructs the payload for:

  • Primary key updates only (primary_key provided)
  • Rename only (new_uid provided)
  • Both operations simultaneously (both parameters provided)

The implementation aligns with the Meilisearch API requirements.

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

indexes:
List of indexes to swap (ex: [{"indexes": ["indexA", "indexB"]}).
List of indexes to swap ex:
[{"indexes": ["indexA", "indexB"]}) # default rename to false
Copy link
Contributor

Choose a reason for hiding this comment

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

not valid json
maybe
[{"indexes": ["indexA", "indexB"]},
{"indexes": ["indexA", "indexB"], "rename": false},
{"indexes": ["indexA", "indexB"], "rename": true}]

@awais786 awais786 closed this Nov 20, 2025
@awais786 awais786 reopened this Nov 20, 2025
@awais786 awais786 marked this pull request as ready for review November 20, 2025 12:24
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)
tests/index/test_index.py (1)

255-255: Clean up redundant comment marker.

Line 255 has an extra # creating a redundant comment marker.

Apply this diff:

-    # # Verify the old UID no longer exists
+    # Verify the old UID no longer exists
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 87cf84b and fb58d6d.

📒 Files selected for processing (5)
  • docs/conf.py (1 hunks)
  • meilisearch/client.py (1 hunks)
  • meilisearch/index.py (2 hunks)
  • tests/client/test_client_swap_meilisearch.py (2 hunks)
  • tests/index/test_index.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
tests/index/test_index.py (5)
tests/conftest.py (1)
  • client (15-16)
meilisearch/client.py (2)
  • index (226-242)
  • wait_for_task (639-666)
meilisearch/index.py (4)
  • update (107-137)
  • wait_for_task (241-268)
  • fetch_info (139-151)
  • get_primary_key (153-161)
meilisearch/task.py (1)
  • wait_for_task (174-212)
meilisearch/errors.py (1)
  • MeilisearchApiError (28-51)
tests/client/test_client_swap_meilisearch.py (3)
tests/conftest.py (2)
  • client (15-16)
  • empty_index (109-117)
meilisearch/client.py (3)
  • index (226-242)
  • swap_indexes (534-556)
  • wait_for_task (639-666)
meilisearch/index.py (2)
  • wait_for_task (241-268)
  • fetch_info (139-151)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Seer Code Review
🔇 Additional comments (7)
docs/conf.py (1)

61-65: LGTM! Minor formatting improvements.

The quote style standardization and spacing adjustments improve code consistency without affecting functionality.

meilisearch/client.py (1)

540-543: LGTM! Clear documentation examples.

The expanded examples effectively illustrate the three usage patterns for the rename parameter in swap operations.

tests/index/test_index.py (2)

239-257: LGTM! Well-structured rename test.

The test properly validates:

  • Successful rename operation via update(new_uid=...)
  • New UID is accessible with correct info
  • Old UID becomes inaccessible (raises MeilisearchApiError)

260-275: LGTM! Excellent test for combined update.

This test validates the important use case of updating both primary key and UID in a single operation, ensuring atomicity and correct behavior.

meilisearch/index.py (1)

107-137: LGTM! Clean and flexible implementation.

The conditional payload construction is well-designed, allowing:

  • Primary key update only
  • Rename only
  • Combined update and rename
  • Handles optional parameters correctly

The implementation properly avoids sending unnecessary fields in the payload.

tests/client/test_client_swap_meilisearch.py (2)

49-60: LGTM! Validates error handling with rename=False.

This test correctly verifies that when rename=False (or default), swapping with a non-existent index produces an index_not_found error as expected.


76-91: LGTM! Validates successful rename via swap.

This test effectively validates that rename=True enables renaming an index by "swapping" to a non-existent target name, and confirms the renamed index is accessible.

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
meilisearch/index.py (1)

107-127: Update docstring to reflect rename capability.

The docstring on Line 108 states "Update the index primary-key" but the method now also supports renaming via the new_uid parameter. Update the summary to reflect both capabilities.

Apply this diff:

-    def update(self, primary_key: Optional[str] = None, new_uid: Optional[str] = None) -> TaskInfo:
-        """Update the index primary-key.
+    def update(self, primary_key: Optional[str] = None, new_uid: Optional[str] = None) -> TaskInfo:
+        """Update the index primary key or rename the index.
🧹 Nitpick comments (2)
tests/index/test_index.py (1)

278-285: Consider stricter assertion.

The assertion on Line 285 uses or, which passes if either string appears in the error message. Consider using and to ensure both parameter names are mentioned, providing a more robust validation of the error message.

Apply this diff:

-    assert "primary_key" in str(exc.value) or "new_uid" in str(exc.value)
+    assert "primary_key" in str(exc.value) and "new_uid" in str(exc.value)
meilisearch/index.py (1)

128-131: Validation correctly prevents empty payload.

The validation logic properly addresses the concern from the previous review about allowing an empty payload, ensuring at least one parameter is provided before making the API call.

Optional: The static analysis tool suggests avoiding long error messages inline (TRY003), but this is a style preference and not a functional issue.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fb58d6d and 1f6ee1a.

📒 Files selected for processing (2)
  • meilisearch/index.py (2 hunks)
  • tests/index/test_index.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
meilisearch/index.py (1)
meilisearch/models/task.py (1)
  • TaskInfo (77-106)
tests/index/test_index.py (5)
tests/conftest.py (1)
  • client (15-16)
meilisearch/client.py (2)
  • index (226-242)
  • wait_for_task (639-666)
meilisearch/index.py (4)
  • update (107-142)
  • wait_for_task (246-273)
  • fetch_info (144-156)
  • get_primary_key (158-166)
meilisearch/task.py (1)
  • wait_for_task (174-212)
meilisearch/errors.py (1)
  • MeilisearchApiError (28-51)
🪛 Ruff (0.14.5)
meilisearch/index.py

129-131: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: integration-tests (3.12)
🔇 Additional comments (2)
tests/index/test_index.py (2)

239-258: LGTM! Thorough rename test.

The test correctly verifies the rename functionality and properly creates a new index reference after renaming rather than reusing the original Index object, which becomes stale after the UID changes.


260-276: LGTM! Combined update test is well-structured.

The test properly validates that both primary_key and new_uid can be updated in a single call, and correctly verifies both changes took effect.

@Strift Strift added the enhancement New feature or request label Nov 24, 2025
@Strift
Copy link
Contributor

Strift commented Nov 25, 2025

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 25, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@Strift Strift left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution 🙌

@Strift Strift added this pull request to the merge queue Nov 25, 2025
Merged via the queue into meilisearch:main with commit 23debbd Nov 25, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants