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: CanonicalStatementString(), utilize sqlparser.CanonicalString() #10142

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func validateDiff(t *testing.T, fromCreateTable string, toCreateTable string, hi
// The diff can be empty or there can be an actual ALTER TABLE statement
diffedAlterQuery := ""
if diff != nil && !diff.IsEmpty() {
diffedAlterQuery = sqlparser.String(diff.Statement())
diffedAlterQuery = diff.CanonicalStatementString()
}

// Validate the diff! The way we do it is:
Expand Down
129 changes: 115 additions & 14 deletions go/vt/schemadiff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestDiffTables(t *testing.T) {
from string
to string
diff string
cdiff string
action string
isError bool
}{
Expand All @@ -44,18 +45,21 @@ func TestDiffTables(t *testing.T) {
from: "create table t(id int primary key)",
to: "create table t(id int primary key, i int)",
diff: "alter table t add column i int",
cdiff: "ALTER TABLE `t` ADD COLUMN `i` int",
action: "alter",
},
{
name: "create",
to: "create table t(id int primary key)",
diff: "create table t (\n\tid int primary key\n)",
cdiff: "CREATE TABLE `t` (\n\t`id` int PRIMARY KEY\n)",
action: "create",
},
{
name: "drop",
from: "create table t(id int primary key)",
diff: "drop table t",
cdiff: "DROP TABLE `t`",
action: "drop",
},
{
Expand Down Expand Up @@ -102,17 +106,33 @@ func TestDiffTables(t *testing.T) {
assert.NoError(t, err)
require.NotNil(t, d)
require.False(t, d.IsEmpty())
diff := d.StatementString()
assert.Equal(t, ts.diff, diff)
action, err := DDLActionStr(d)
assert.NoError(t, err)
assert.Equal(t, ts.action, action)
{
diff := d.StatementString()
assert.Equal(t, ts.diff, diff)
action, err := DDLActionStr(d)
assert.NoError(t, err)
assert.Equal(t, ts.action, action)

// validate we can parse back the statement
_, err = sqlparser.Parse(diff)
assert.NoError(t, err)
}
{
canonicalDiff := d.CanonicalStatementString()
assert.Equal(t, ts.cdiff, canonicalDiff)
action, err := DDLActionStr(d)
assert.NoError(t, err)
assert.Equal(t, ts.action, action)

// validate we can parse back the statement
_, err = sqlparser.Parse(canonicalDiff)
assert.NoError(t, err)
}
// let's also check dq, and also validate that dq's statement is identical to d's
assert.NoError(t, dqerr)
require.NotNil(t, dq)
require.False(t, dq.IsEmpty())
diff = dq.StatementString()
diff := dq.StatementString()
assert.Equal(t, ts.diff, diff)
}
})
Expand All @@ -125,6 +145,7 @@ func TestDiffViews(t *testing.T) {
from string
to string
diff string
cdiff string
action string
isError bool
}{
Expand All @@ -138,18 +159,21 @@ func TestDiffViews(t *testing.T) {
from: "create view v1 (col1, `col2`, `col3`) as select `a`, `b`, c from t",
to: "create view v1 (`col1`, col2, colother) as select a, b, `c` from t",
diff: "alter view v1(col1, col2, colother) as select a, b, c from t",
cdiff: "ALTER VIEW `v1`(`col1`, `col2`, `colother`) AS SELECT `a`, `b`, `c` FROM `t`",
action: "alter",
},
{
name: "create",
to: "create view v1 as select a, b, c from t",
diff: "create view v1 as select a, b, c from t",
cdiff: "CREATE VIEW `v1` AS SELECT `a`, `b`, `c` FROM `t`",
action: "create",
},
{
name: "drop",
from: "create view v1 as select a, b, c from t",
diff: "drop view v1",
cdiff: "DROP VIEW `v1`",
action: "drop",
},
{
Expand Down Expand Up @@ -196,17 +220,35 @@ func TestDiffViews(t *testing.T) {
assert.NoError(t, err)
require.NotNil(t, d)
require.False(t, d.IsEmpty())
diff := d.StatementString()
assert.Equal(t, ts.diff, diff)
action, err := DDLActionStr(d)
assert.NoError(t, err)
assert.Equal(t, ts.action, action)
{
diff := d.StatementString()
assert.Equal(t, ts.diff, diff)
action, err := DDLActionStr(d)
assert.NoError(t, err)
assert.Equal(t, ts.action, action)

// validate we can parse back the statement
_, err = sqlparser.Parse(diff)
assert.NoError(t, err)

}
{
canonicalDiff := d.CanonicalStatementString()
assert.Equal(t, ts.cdiff, canonicalDiff)
action, err := DDLActionStr(d)
assert.NoError(t, err)
assert.Equal(t, ts.action, action)

// validate we can parse back the statement
_, err = sqlparser.Parse(canonicalDiff)
assert.NoError(t, err)
}

// let's also check dq, and also validate that dq's statement is identical to d's
assert.NoError(t, dqerr)
require.NotNil(t, dq)
require.False(t, dq.IsEmpty())
diff = dq.StatementString()
diff := dq.StatementString()
assert.Equal(t, ts.diff, diff)
}
})
Expand All @@ -219,6 +261,7 @@ func TestDiffSchemas(t *testing.T) {
from string
to string
diffs []string
cdiffs []string
expectError string
}{
{
Expand All @@ -233,13 +276,19 @@ func TestDiffSchemas(t *testing.T) {
diffs: []string{
"alter table t add column i int",
},
cdiffs: []string{
"ALTER TABLE `t` ADD COLUMN `i` int",
},
},
{
name: "create table",
to: "create table t(id int primary key)",
diffs: []string{
"create table t (\n\tid int primary key\n)",
},
cdiffs: []string{
"CREATE TABLE `t` (\n\t`id` int PRIMARY KEY\n)",
},
},
{
name: "create table (2)",
Expand All @@ -248,13 +297,19 @@ func TestDiffSchemas(t *testing.T) {
diffs: []string{
"create table t (\n\tid int primary key\n)",
},
cdiffs: []string{
"CREATE TABLE `t` (\n\t`id` int PRIMARY KEY\n)",
},
},
{
name: "drop table",
from: "create table t(id int primary key)",
diffs: []string{
"drop table t",
},
cdiffs: []string{
"DROP TABLE `t`",
},
},
{
name: "create, alter, drop tables",
Expand All @@ -265,6 +320,11 @@ func TestDiffSchemas(t *testing.T) {
"alter table t2 modify column id bigint primary key",
"create table t4 (\n\tid int primary key\n)",
},
cdiffs: []string{
"DROP TABLE `t1`",
"ALTER TABLE `t2` MODIFY COLUMN `id` bigint PRIMARY KEY",
"CREATE TABLE `t4` (\n\t`id` int PRIMARY KEY\n)",
},
},
{
name: "identical views",
Expand All @@ -278,6 +338,9 @@ func TestDiffSchemas(t *testing.T) {
diffs: []string{
"alter view v1 as select id from t",
},
cdiffs: []string{
"ALTER VIEW `v1` AS SELECT `id` FROM `t`",
},
},
{
name: "drop view",
Expand All @@ -286,6 +349,9 @@ func TestDiffSchemas(t *testing.T) {
diffs: []string{
"drop view v1",
},
cdiffs: []string{
"DROP VIEW `v1`",
},
},
{
name: "create view",
Expand All @@ -294,6 +360,9 @@ func TestDiffSchemas(t *testing.T) {
diffs: []string{
"create view v1 as select id from t",
},
cdiffs: []string{
"CREATE VIEW `v1` AS SELECT `id` FROM `t`",
},
},
{
name: "create view: unresolved dependencies",
Expand All @@ -309,6 +378,10 @@ func TestDiffSchemas(t *testing.T) {
"drop table v1",
"create view v1 as select * from t",
},
cdiffs: []string{
"DROP TABLE `v1`",
"CREATE VIEW `v1` AS SELECT * FROM `t`",
},
},
{
name: "convert view to table",
Expand All @@ -318,6 +391,10 @@ func TestDiffSchemas(t *testing.T) {
"drop view v1",
"create table v1 (\n\tid int\n)",
},
cdiffs: []string{
"DROP VIEW `v1`",
"CREATE TABLE `v1` (\n\t`id` int\n)",
},
},
{
name: "unsupported statement",
Expand All @@ -337,6 +414,14 @@ func TestDiffSchemas(t *testing.T) {
"alter view v2 as select id from t2",
"create view v0 as select * from v2, t2",
},
cdiffs: []string{
"DROP TABLE `t1`",
"DROP VIEW `v1`",
"ALTER TABLE `t2` MODIFY COLUMN `id` bigint PRIMARY KEY",
"CREATE TABLE `t4` (\n\t`id` int PRIMARY KEY\n)",
"ALTER VIEW `v2` AS SELECT `id` FROM `t2`",
"CREATE VIEW `v0` AS SELECT * FROM `v2`, `t2`",
},
},
}
hints := &DiffHints{}
Expand All @@ -348,15 +433,31 @@ func TestDiffSchemas(t *testing.T) {
assert.Contains(t, err.Error(), ts.expectError)
} else {
assert.NoError(t, err)

statements := []string{}
cstatements := []string{}
for _, d := range diffs {
statement := sqlparser.String(d.Statement())
statements = append(statements, statement)
statements = append(statements, d.StatementString())
cstatements = append(cstatements, d.CanonicalStatementString())
}
if ts.diffs == nil {
ts.diffs = []string{}
}
assert.Equal(t, ts.diffs, statements)
if ts.cdiffs == nil {
ts.cdiffs = []string{}
}
assert.Equal(t, ts.cdiffs, cstatements)

// validate we can parse back the diff statements
for _, s := range statements {
_, err := sqlparser.Parse(s)
assert.NoError(t, err)
}
for _, s := range cstatements {
_, err := sqlparser.Parse(s)
assert.NoError(t, err)
}
}
})
}
Expand Down
24 changes: 24 additions & 0 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func (d *AlterTableEntityDiff) StatementString() (s string) {
return s
}

// CanonicalStatementString implements EntityDiff
func (d *AlterTableEntityDiff) CanonicalStatementString() (s string) {
if stmt := d.Statement(); stmt != nil {
s = sqlparser.CanonicalString(stmt)
}
return s
}

//
type CreateTableEntityDiff struct {
createTable *sqlparser.CreateTable
Expand All @@ -75,6 +83,14 @@ func (d *CreateTableEntityDiff) StatementString() (s string) {
return s
}

// CanonicalStatementString implements EntityDiff
func (d *CreateTableEntityDiff) CanonicalStatementString() (s string) {
if stmt := d.Statement(); stmt != nil {
s = sqlparser.CanonicalString(stmt)
}
return s
}

//
type DropTableEntityDiff struct {
dropTable *sqlparser.DropTable
Expand All @@ -101,6 +117,14 @@ func (d *DropTableEntityDiff) StatementString() (s string) {
return s
}

// CanonicalStatementString implements EntityDiff
func (d *DropTableEntityDiff) CanonicalStatementString() (s string) {
if stmt := d.Statement(); stmt != nil {
s = sqlparser.CanonicalString(stmt)
}
return s
}

// CreateTableEntity stands for a TABLE construct. It contains the table's CREATE statement.
type CreateTableEntity struct {
sqlparser.CreateTable
Expand Down
Loading