Skip to content
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

schemadiff: analyze and report foreign key loops/cycles #15062

Merged
merged 5 commits into from
Feb 5, 2024

Conversation

shlomi-noach
Copy link
Contributor

@shlomi-noach shlomi-noach commented Jan 28, 2024

Description

See #15061 for details.

schemadiff does not support foreign key cycles (with the exception of self referencing tables). In this PR, schemadiff identifies cycles and reports those cycles as part of the schema validation process. Taking the same example from #15061:

create table t11 (id int primary key, i int, constraint f11 foreign key (i) references t12 (id));
create table t12 (id int primary key, i int, constraint f12 foreign key (i) references t11 (id));

As of this PR, schemadiff will now report:

table `t11` participates in foreign key loop: `t11`, `t12`, `t11`
table `t12` participates in foreign key loop: `t11`, `t12`, `t11`

schemadiff will further identify tables that reference foreign key loops, i.e. their immediate parent or on of their indirect parents participates in a loop, and will report something like:

table `t13` references foreign key loop: `t11`, `t12`, `t11`

Computing foreign key loops uses DFS, and for every loop found it caches the tables participating in that loop, so as to never recompute the same table twice. This limits the number of loops found per table to 1, which is sufficient, as we already only report one foreign key error per table anyway (otherwise this can blow up with excessive error messages).

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Signed-off-by: Shlomi Noach <[email protected]>
…possibility as this is already covered by ForeignKeyLoopError

Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
@shlomi-noach shlomi-noach added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Query Serving labels Jan 28, 2024
@shlomi-noach shlomi-noach requested a review from a team January 28, 2024 09:27
@shlomi-noach shlomi-noach requested a review from deepthi as a code owner January 28, 2024 09:27
Copy link
Contributor

vitess-bot bot commented Jan 28, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jan 28, 2024
@github-actions github-actions bot added this to the v19.0.0 milestone Jan 28, 2024
Copy link

codecov bot commented Jan 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f751c83) 47.49% compared to head (1a4ed10) 40.88%.
Report is 41 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main   #15062       +/-   ##
===========================================
- Coverage   47.49%   40.88%    -6.61%     
===========================================
  Files        1149     1454      +305     
  Lines      239475   368246   +128771     
===========================================
+ Hits       113730   150567    +36837     
- Misses     117138   201488    +84350     
- Partials     8607    16191     +7584     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

sqlescape.EscapeID(e.Table))
}

type ForeignKeyLoopError struct {
Table string
Loop []string
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it useful to have for each step in the loop also the column name? To more exactly identify the specific loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm afraid of clutter. The foreign key might reference multiple columns. It is indeed possible that same table child will reference table parent with two different foreign keys, one of which has a cycle, the other does not. In such case it would indeed be beneficial to identify the referenced columns both on parent and child; but again this is an amount of information that is likely to create much background noise. Alternatively, we could use the foreign key name participating in the loop.
Either solution would complicate the loop detection logic only slightly; question is how useful vs. confusing would the information be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just took a look at the code. The current approach (only report table name) is consistent with other schemadiff error reports, that suffice with table name or column name without drilling into specific details. I think this should be fine as it is for now.

@shlomi-noach shlomi-noach removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jan 28, 2024
@shlomi-noach shlomi-noach requested review from a team and removed request for a team January 30, 2024 10:42
Comment on lines 297 to 299
escaped := make([]string, 0, len(e.Loop))
for _, t := range e.Loop {
escaped = append(escaped, sqlescape.EscapeID(t))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitty, but I think this is slightly more performant:

	escaped := make([]string, len(e.Loop))
	for i, t := range e.Loop {
		escaped[i] = sqlescape.EscapeID(t)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied.

}
}
if tableIsInsideLoop {
return fmt.Sprintf("table %s participates in foreign key loop: %s",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think "infinite loop" or "circular foreign key references" might be more descriptive for user facing error messages and docs, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

}
if seenTable == tableName {
// This table alreay appears in `seen`.
// We only return the loop portion of `seen` that starts and ends with this table.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this comment is a little off, no? Maybe ~ // We only return the tail end of seen which begins with this table.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reworded as:

// We only return the suffix of `seen` that starts (and now ends) with this table.

Signed-off-by: Shlomi Noach <[email protected]>
@shlomi-noach shlomi-noach merged commit 961f70f into vitessio:main Feb 5, 2024
101 of 102 checks passed
@shlomi-noach shlomi-noach deleted the schemadiff-fk-loops branch February 5, 2024 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Query Serving Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: foreign key loop analysis in schemadiff
3 participants