DM: remove redundant DELETE and force non-cascade REPLACE in safe mode to support Foreign Key constraint with non-key updates (#12351)#12541
Conversation
1. Remove redundant Delete statement from safe mode rewrite of Update when row identity(pk and uks) are not changed during the Update. 2. Force replace into from safe mode to be executed without triggering cascade behaviors. 3. Add integration test to verify safe mode under insert and non-key updates with FK and on cascade delete enabled.
… and better batching.
… when determining non-key udpates.
…l be triggered and the test case will function as intended.
…efactor defer function in executeBatchJobs for better adaption to errors from genSQLs.
…s runs." This reverts commit 8371f46.
…abled." because the original check without FKinfo would be too strict and an overkill. This reverts commit 9ae52f1.
…er be triggered under the current setup, even after after the attempt to make the test modify PK value as well (will result in len(queries) == len(jobs) in two queues). Therefore DM_DML_EXECUTE_ERROR is commented out for now.
Summary of ChangesHello, 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 enhances the Data Migration (DM) tool's handling of DML operations in safe mode, particularly in scenarios involving Foreign Key constraints. The changes streamline the update process by intelligently omitting redundant DELETE operations when only non-key columns are modified. Furthermore, it introduces mechanisms to prevent unintended cascade actions during REPLACE operations, ensuring data consistency and integrity. These improvements are validated through a new, dedicated integration test case. Highlights
🧠 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
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces an optimization for safe mode by avoiding redundant DELETE statements for non-key updates, and correctly handles foreign key constraints by temporarily disabling foreign_key_checks. The changes are well-structured and include comprehensive unit and integration tests. I've found a logic issue in the new IsPrimaryOrUniqueKeyUpdated function that could be improved for correctness and clarity. Overall, a great improvement.
Note: Security Review did not run due to the size of the PR.
| func (r *RowChange) IsPrimaryOrUniqueKeyUpdated() bool { | ||
| if r.tp != RowChangeUpdate { | ||
| return false | ||
| } | ||
|
|
||
| r.lazyInitWhereHandle() | ||
| identityUpdated := func(pre, post []interface{}) bool { | ||
| if len(pre) != len(post) { | ||
| // should not happen | ||
| return true | ||
| } | ||
| for i := range pre { | ||
| if pre[i] != post[i] { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| if idx := r.whereHandle.UniqueNotNullIdx; idx != nil { | ||
| pre, post := r.identityValuesByIndex(idx) | ||
| if identityUpdated(pre, post) { | ||
| return true | ||
| } | ||
| } else { | ||
| if identityUpdated(r.preValues, r.postValues) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| for _, idx := range r.whereHandle.UniqueIdxs { | ||
| if idx == nil || idx == r.whereHandle.UniqueNotNullIdx { | ||
| continue | ||
| } | ||
| pre, post := r.identityValuesByIndex(idx) | ||
| if identityUpdated(pre, post) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } |
There was a problem hiding this comment.
The logic in IsPrimaryOrUniqueKeyUpdated can be simplified and made more correct. The current implementation has a flaw: if a table only has nullable unique keys (UniqueNotNullIdx is nil), the else block at line 127 will treat any update as a key update by checking all columns. This prevents the optimization for such tables, as the loop at line 133 is never reached.
The logic should be to iterate through all available unique keys. If any key is updated, return true. If no unique keys exist, then any update is considered a key update.
I suggest simplifying the function as follows:
func (r *RowChange) IsPrimaryOrUniqueKeyUpdated() bool {
if r.tp != RowChangeUpdate {
return false
}
r.lazyInitWhereHandle()
identityUpdated := func(pre, post []interface{}) bool {
if len(pre) != len(post) {
// should not happen
return true
}
for i := range pre {
if pre[i] != post[i] {
return true
}
}
return false
}
if len(r.whereHandle.UniqueIdxs) == 0 {
return identityUpdated(r.preValues, r.postValues)
}
for _, idx := range r.whereHandle.UniqueIdxs {
if idx == nil {
continue
}
pre, post := r.identityValuesByIndex(idx)
if identityUpdated(pre, post) {
return true
}
}
return false
}
Codecov Report❌ Patch coverage is Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more. @@ Coverage Diff @@
## release-8.5 #12541 +/- ##
================================================
Coverage ? 53.4111%
================================================
Files ? 1026
Lines ? 137507
Branches ? 0
================================================
Hits ? 73444
Misses ? 58593
Partials ? 5470 🚀 New features to boost your workflow:
|
|
/retest |
|
@OliverS929: adding LGTM is restricted to approvers and reviewers in OWNERS files. DetailsIn response to this: 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. |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: GMHDBJD, OliverS929 The full list of commands accepted by this bot can be found here. The pull request process is described here |
|
/retest |
|
/test pull-cdc-integration-storage-test |
|
/test pull-dm-compatibility-test |
|
/test pull-dm-integration-test |
|
/retest |
3 similar comments
|
/retest |
|
/retest |
|
/retest |
|
/retest |
1 similar comment
|
/retest |
This is an automated cherry-pick of #12351
What problem does this PR solve?
Issue Number: ref #12350
What is changed and how it works?
Check List
Tests
Questions
Will it cause performance regression or break compatibility?
Do you need to update user documentation, design documentation or monitoring documentation?
Release note
Remove redundant DELETE and force non-cascade REPLACE in safe mode to support Foreign Key constraint with non-key updates.