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: support valid foreign key cycles #15431

Merged
merged 9 commits into from
Mar 11, 2024

Conversation

shlomi-noach
Copy link
Contributor

@shlomi-noach shlomi-noach commented Mar 10, 2024

Description

Addressing #15430, in this PR schemadiff uses the same algorithm as Query-Serving to distinguish between valid and invalid foreign key cycles. schemadiff now allows table cycles as long as there's no column-reference cycle.

There's a few implications to this solution. Consider this valid foreign key cycle:

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));
  • There is technically no valid ordering to creating these two tables. In MySQL, this could only be achieved by setting FOREIGN_KEY_CHECKS=0. However, schemadiff is declarative. We introduce DiffHints.ForeignKeyCheckStrategy which can be:

    • ForeignKeyCheckStrategyStrict (default value), makes Diff(), OrderedDiffs() etc. behave as if FOREIGN_KEY_CHECKS=1
    • ForeignKeyCheckStrategyIgnore, assume behavior of FOREIGN_KEY_CHECKS=0.
      This means OrderedDiffs() can generate an invalid schema whilst applying changes. We add one final validation to ensure that the grand total result is valid.
  • The ordering of tables in a Schema object is normally by:

    • tables, foreign key dependency order (parents first, children tables)
    • tables, lexicographically
    • views, dependency order
    • views, lexicographically

    (See related discussion in WIP: MoveTables Cancel: sort table list taking into account FK order and drop tables in reverse order. #15252 (comment)).
    However, with cyclic foreign key references the ordering of tables that either participate in a loop, or reference a loop, is undefined. It will always be higher than the order of tables that do not participate in a loop, but no deterministic ordering in between loop-related tables.

  • It is up to the consumer of schemadiff to actually SET FOREIGN_KEY_CHECKS=0 in MySQL. schemadiff does not generate such SQL statement.

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

@shlomi-noach shlomi-noach requested a review from deepthi as a code owner March 10, 2024 10:04
Copy link
Contributor

vitess-bot bot commented Mar 10, 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 Mar 10, 2024
@shlomi-noach shlomi-noach requested a review from a team March 10, 2024 10:04
@github-actions github-actions bot added this to the v20.0.0 milestone Mar 10, 2024
@shlomi-noach shlomi-noach added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Query Serving and 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 Mar 10, 2024
Copy link

codecov bot commented Mar 10, 2024

Codecov Report

Attention: Patch coverage is 95.45455% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 65.66%. Comparing base (6c73053) to head (d3b8c8e).
Report is 7 commits behind head on main.

Files Patch % Lines
go/vt/schemadiff/schema_diff.go 70.00% 3 Missing ⚠️
go/vt/schemadiff/schema.go 96.07% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15431      +/-   ##
==========================================
- Coverage   65.72%   65.66%   -0.07%     
==========================================
  Files        1563     1563              
  Lines      194027   194380     +353     
==========================================
+ Hits       127529   127644     +115     
- Misses      66498    66736     +238     

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

go/vt/graph/graph.go Outdated Show resolved Hide resolved
Signed-off-by: Shlomi Noach <[email protected]>
@@ -294,8 +294,23 @@ type ForeignKeyLoopError struct {
func (e *ForeignKeyLoopError) Error() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

@shlomi-noach Should we change ForeignKeyLoopError to have a different Loop type? So something like this maybe:

type ForeignKeyColumn struct {
	Table  string
	Column string
}

type ForeignKeyLoopError struct {
	Table string
	Loop  []ForeignKeyColumn
}

This because we now better track this at a column level, so this way we have the details of what the loop looks like?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I'll do so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in d3b8c8e

… include column names in error message

Signed-off-by: Shlomi Noach <[email protected]>
@shlomi-noach shlomi-noach merged commit 46975b2 into vitessio:main Mar 11, 2024
102 checks passed
@shlomi-noach shlomi-noach deleted the schemadiff-cyclic-fk branch March 11, 2024 12:55
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.

3 participants