-
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
Introducing schemadiff, a declarative diff for table/view CREATE statements #9719
Conversation
…eclarative schemas using go/vt/sqlparser and without need for a backend database Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Ahhh, there we go. I was testing with |
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Woot, solved the failing test scenario. |
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Good to review. |
@@ -0,0 +1,427 @@ | |||
/* | |||
Copyright 2021 The Vitess Authors. |
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.
nit: 2022
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.
fixed
}) | ||
} | ||
|
||
// func TestRandomSchemaChanges(t *testing.T) { |
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.
did you intentionally leave this around?
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.
Actually, yes. So I had this idea that on top of the existing 80 - 90 tests, I could also do a Cartesian product of all tests, a crossover between them all, to test any-schema-to-any-schema conversion. That's the commented code. The reason it's commented is that this leads to some 5K tests, which run well within 7 minutes -- a reasonable time -- but I'm afraid that if I add a bit more test, this will explode (runtime is n^2).
…face function that was never used Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
…lumnDefinition field Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Did a bit more refactoring/cleanup -- nothing substantial to affect the review. Good to go. |
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.
This looks great to me. It's very little code for a lot of functionality. I think we still have pending the abstraction of this package, and the sqlparser
, so it can be reused more easily, but that shouldn't block this PR.
👍 ❤️ |
Tracking issue: #10203 |
Description
go/vt/schemadiff
is a new library whose functionality is to diff twoCREATE TABLE
statements, or twoCREATE VIEW
statements, and produce the SQL diff between them, which could be either:CREATE TABLE
/DROP TABLE
/ALTER TABLE
CREATE VIEW
/DROP VIEW
/ALTER VIEW
The most interesting use case is getting a valid
ALTER TABLE
statement that will convert a table from one state to the other.schemadiff
relies on Vitess'sgo/vt/sqlparser
functionality, and specifically:The main entry points for this functionality are:
As example:
The returned
diff
object can benil
, if no diff is found (ie two identical tables, or two identical views, or twonil
statements). Otherwise, thediff
can be queries forStatement() sqlparser.Statement
In the above example, we can expect
sqlparser.String(diff.Statement()) == "alter table t add column c char(5)"
It is imperative that the two input queries, or the two
Create(Table|View)
statements are canonical. Ideally, the input should be a strictSHOW CREATE TABLE
output (thus, made canonical by MySQL). Otherwise there are some scenarios where the parser can return a diff for semantically-identical statements,Table diffing
Table diffing is the most complex of all diffs.
schemadiff
supports:schemadiff
is impartial to ordering of keys and does not generate a diff when keys are ordered differently between tables. there's an unimplementedhint
flag to control that in the future.sqlparser
's limitations -- right now subpartitioning unsupported)schemadiff
rejects statements thatsqlparser
cannot fully parse.Testing
There are two forms of testing to
schemadiff
:go/vt/schemadiff
. Those are self explanatory I believe.endtoend
testing. What we did was to piggyride theonlineddl/vrepl_suite
tests:vrepl_suite
is the main suite that validatedvitess
schema migration functionality, by presenting trivial, complex or nicheALTER
cases with various data types, conversions and constraints. It is what builds our trust invitess
migrations.schemadiff
uses the same suite as follows:vitess
Online DDL here) thealter
statement in the testSHOW CREATE TABLE
in MySQL -- this is the main validationI was able to moreover do a N*N comparison of all tables in the suite; this leads to some 5000 tests, they're all good. This part is committed but commented out. It takes 7 minutes to run, which is fine, but I'm afraid it will grow fast and too much, should we add more tests.
Using
At this time, the library is added but not used by any code path. In a followup PR, we will make Online DDL's
-declarative
migrations to use this library (technically this is already known to work, I just want to have separate PRs).Checklist
Many thanks to @GuptaManan100 for fleshing out parsing issues found while working on this PR.