Skip to content

schemastore: fix rename tables when tidb version <= v8.1.x (#4388)#4410

Merged
lidezhu merged 2 commits intopingcap:release-8.5from
ti-chi-bot:cherry-pick-4388-to-release-8.5
Mar 10, 2026
Merged

schemastore: fix rename tables when tidb version <= v8.1.x (#4388)#4410
lidezhu merged 2 commits intopingcap:release-8.5from
ti-chi-bot:cherry-pick-4388-to-release-8.5

Conversation

@ti-chi-bot
Copy link
Copy Markdown
Member

This is an automated cherry-pick of #4388

What problem does this PR solve?

Issue Number: close #4392

What is changed and how it works?

This pull request significantly improves the robustness and correctness of DDL event handling within the schema store, particularly for RENAME TABLES operations. It addresses a critical issue where older TiDB versions might provide incomplete old table information, by introducing a fallback mechanism that parses this data directly from the DDL query. Furthermore, it fortifies the system against potential SQL injection by ensuring all generated DDL queries properly quote schema and table identifiers. These changes enhance the reliability of schema synchronization and DDL application.

Highlights

  • Robust RENAME TABLES Handling: Implemented a fallback mechanism for RENAME TABLES DDL events to derive missing old table names from the original SQL query, specifically addressing issues in TiDB versions <= v8.1.x where this information might be incomplete.
  • Enhanced Identifier Quoting: Refactored DDL query generation across various DDL types (DROP VIEW, DROP TABLE, RENAME TABLE, EXCHANGE PARTITION) to consistently use common.QuoteSchema and common.QuoteName for proper identifier escaping, mitigating potential SQL injection vulnerabilities.
  • Case-Insensitive Name Lookups: Updated schema and table name lookup functions (findSchemaIDByName, findTableIDByName) to perform case-insensitive comparisons, improving flexibility and correctness.
  • Improved Error Handling for Multi-Table Renames: Changed a warning to a panic for length mismatches in RENAME TABLES operations, ensuring that multi-table DDLs are applied atomically and preventing partial schema updates.
  • Comprehensive Testing: Added extensive unit tests covering the new fallback logic for RENAME TABLES, cyclic rename scenarios, identifier escaping, and case-insensitive name matching.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Questions

Will it cause performance regression or break compatibility?
Do you need to update user documentation, design documentation or monitoring documentation?

Release note

Please refer to [Release Notes Language Style Guide](https://pingcap.github.io/tidb-dev-guide/contribute-to-tidb/release-notes-style-guide.html) to write a quality release note.

If you don't think this PR needs a release note then fill it with `None`.

Summary by CodeRabbit

  • Bug Fixes

    • Improved persisted RENAME TABLE handling: safely derive missing old identifiers from SQL and stored metadata as fallbacks, prefer store metadata on conflicts (e.g., cycles), truncate mismatched inputs to a safe length, emit warnings when fallbacks/mismatches occur, and ensure emitted RENAME statements use the resolved identifiers.
  • Tests

    • Added tests covering fallback recovery from parsed queries, metadata-driven reconciliation, and cyclic/temporary-table rename scenarios.

@ti-chi-bot ti-chi-bot added lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. type/cherry-pick-for-release-8.5 This PR is cherry-picked to release-8.5 from a source PR. labels Mar 10, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Mar 10, 2026

This cherry pick PR is for a release branch and has not yet been approved by triage owners.
Adding the do-not-merge/cherry-pick-not-approved label.

To merge this cherry pick:

  1. It must be LGTMed and approved by the reviewers firstly.
  2. For pull requests to TiDB-x branches, it must have no failed tests.
  3. AFTER it has lgtm and approved labels, please wait for the cherry-pick merging approval from triage owners.
Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the schema store's handling of DDL events, particularly for RENAME TABLES operations. It introduces a robust fallback mechanism to correctly identify old table names from the DDL query, addressing a limitation in older TiDB versions. Additionally, it strengthens security and reliability by ensuring proper quoting of schema and table identifiers in generated DDL queries across several DDL types. These changes collectively improve the accuracy and resilience of schema synchronization.

Highlights

  • Robust RENAME TABLES Handling: Implemented a fallback mechanism for RENAME TABLES DDL events to derive missing old table names from the original SQL query, specifically addressing issues in TiDB versions <= v8.1.x where this information might be incomplete.
  • Enhanced Identifier Quoting: Refactored DDL query generation across various DDL types (DROP VIEW, DROP TABLE, RENAME TABLE, EXCHANGE PARTITION) to consistently use common.QuoteSchema and common.QuoteName for proper identifier escaping, mitigating potential SQL injection vulnerabilities.
  • Case-Insensitive Name Lookups: Updated schema and table name lookup functions (findSchemaIDByName, findTableIDByName) to perform case-insensitive comparisons, improving flexibility and correctness.
  • Improved Error Handling for Multi-Table Renames: Changed a warning to a panic for length mismatches in RENAME TABLES operations, ensuring that multi-table DDLs are applied atomically and preventing partial schema updates.
  • Comprehensive Testing: Added extensive unit tests covering the new fallback logic for RENAME TABLES, cyclic rename scenarios, identifier escaping, and case-insensitive name matching.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • logservice/schemastore/persist_storage_ddl_handlers.go
    • Updated schema and table name lookup functions to use case-insensitive comparison.
    • Refactored DDL query generation for DROP VIEW, DROP TABLE, RENAME TABLE, and EXCHANGE PARTITION to use common quoting utilities.
    • Introduced a new utility function parseRenameTablesQueryInfos to parse RENAME TABLES queries.
    • Enhanced buildPersistedDDLEventForRenameTables to prioritize names from the original query, provide a fallback for missing old table names in older TiDB versions, and enforce consistent identifier quoting.
    • Added panic conditions for inconsistent lengths between rename arguments and parsed query information in multi-table rename operations.
  • logservice/schemastore/persist_storage_test.go
    • Added new test cases for buildPersistedDDLEventForRenameTables to cover fallback logic for missing old table names, cyclic renames, and preference for query-parsed names.
    • Included tests to verify panic behavior when RENAME TABLES query info lengths are inconsistent.
    • Added comprehensive tests for identifier escaping across various DDL types, including RENAME TABLES, RENAME TABLE, DROP TABLE, DROP VIEW, and EXCHANGE PARTITION.
    • Introduced new test cases for parseRenameTablesQueryInfos to validate its parsing capabilities.
    • Expanded TestBuildPersistedDDLEventForCreateTableLikeSetsReferTableID with case-insensitive lookup scenarios.
  • tests/integration_tests/_utils/test_prepare
    • Added shell functions (get_tidb_release_version, normalize_tidb_semver_triplet, tidb_version_less_than, skip_if_tidb_version_less_than) to facilitate TiDB version checks in integration tests.
  • tests/integration_tests/vector/run.sh
    • Integrated skip_if_tidb_version_less_than to conditionally skip tests based on the TiDB version, specifically for versions older than v8.5.0.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 10, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c505baac-a637-41a3-9490-8fa97e218321

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant improvements to DDL event handling, particularly for RENAME TABLES operations, by adding a fallback to parse the original SQL query. This addresses issues with incomplete information from older TiDB versions. The changes also enhance security by consistently quoting identifiers in generated DDL queries, mitigating potential SQL injection risks. Additionally, lookups for schema and table names are now case-insensitive, and error handling for multi-table renames has been made more robust. The new logic is well-supported by extensive unit tests. My review found one minor issue with unquoting partition names that could lead to incorrect DDL generation in some edge cases.

@@ -787,8 +792,10 @@ func buildPersistedDDLEventForExchangePartition(args buildPersistedDDLEventFuncA
// Note that partition name should be parsed from original query, not the upperQuery.
partName := strings.TrimSpace(event.Query[idx1:idx2])
partName = strings.Replace(partName, "`", "", -1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The use of strings.Replace(partName, "", "", -1)` to unquote the partition name is incorrect as it removes all backticks, which can corrupt partition names that legitimately contain a backtick. A proper unquoting logic should be used that handles surrounding backticks and escaped backticks.

if len(partName) > 1 && partName[0] == '`' && partName[len(partName)-1] == '`' {
	partName = strings.ReplaceAll(partName[1:len(partName)-1], "``", "`")
}

@lidezhu
Copy link
Copy Markdown
Collaborator

lidezhu commented Mar 10, 2026

/test all

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Mar 10, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from lidezhu. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@lidezhu
Copy link
Copy Markdown
Collaborator

lidezhu commented Mar 10, 2026

/test all

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Mar 10, 2026

@ti-chi-bot: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-cdc-mysql-integration-light 4e8293e link true /test pull-cdc-mysql-integration-light

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@lidezhu lidezhu merged commit 5306544 into pingcap:release-8.5 Mar 10, 2026
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/cherry-pick-not-approved lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. type/cherry-pick-for-release-8.5 This PR is cherry-picked to release-8.5 from a source PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants