-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Conversation
Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
…the loop or just references a loop Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
Codecov ReportAttention: Patch coverage is
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. |
Signed-off-by: Shlomi Noach <[email protected]>
@@ -294,8 +294,23 @@ type ForeignKeyLoopError struct { | |||
func (e *ForeignKeyLoopError) Error() string { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
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:
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 introduceDiffHints.ForeignKeyCheckStrategy
which can be:ForeignKeyCheckStrategyStrict
(default value), makesDiff()
,OrderedDiffs()
etc. behave as ifFOREIGN_KEY_CHECKS=1
ForeignKeyCheckStrategyIgnore
, assume behavior ofFOREIGN_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:(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 actuallySET FOREIGN_KEY_CHECKS=0
in MySQL.schemadiff
does not generate such SQL statement.Related Issue(s)
schemadiff
: analyze and report foreign key loops/cycles #15062Checklist
Deployment Notes