Skip to content

Commit

Permalink
EntityDiff has a 'Entities() (from Entity, to Entity) function' (#10161)
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
  • Loading branch information
shlomi-noach authored Apr 28, 2022
1 parent 31c9780 commit 02b20f6
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 58 deletions.
99 changes: 63 additions & 36 deletions go/vt/schemadiff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,46 @@ import (

func TestDiffTables(t *testing.T) {
tt := []struct {
name string
from string
to string
diff string
cdiff string
action string
isError bool
name string
from string
to string
diff string
cdiff string
fromName string
toName string
action string
isError bool
}{
{
name: "identical",
from: "create table t(id int primary key)",
to: "create table t(id int primary key)",
},
{
name: "change of columns",
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: "change of columns",
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",
fromName: "t",
toName: "t",
},
{
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",
toName: "t",
},
{
name: "drop",
from: "create table t(id int primary key)",
diff: "drop table t",
cdiff: "DROP TABLE `t`",
action: "drop",
name: "drop",
from: "create table t(id int primary key)",
diff: "drop table t",
cdiff: "DROP TABLE `t`",
action: "drop",
fromName: "t",
},
{
name: "none",
Expand Down Expand Up @@ -116,6 +122,14 @@ func TestDiffTables(t *testing.T) {
// validate we can parse back the statement
_, err = sqlparser.Parse(diff)
assert.NoError(t, err)

eFrom, eTo := d.Entities()
if ts.fromName != "" {
assert.Equal(t, ts.fromName, eFrom.Name())
}
if ts.toName != "" {
assert.Equal(t, ts.toName, eTo.Name())
}
}
{
canonicalDiff := d.CanonicalStatementString()
Expand All @@ -141,40 +155,46 @@ func TestDiffTables(t *testing.T) {

func TestDiffViews(t *testing.T) {
tt := []struct {
name string
from string
to string
diff string
cdiff string
action string
isError bool
name string
from string
to string
diff string
cdiff string
fromName string
toName string
action string
isError bool
}{
{
name: "identical",
from: "create view v1 as select a, b, c from t",
to: "create view v1 as select a, b, c from t",
},
{
name: "change of column list, qualifiers",
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: "change of column list, qualifiers",
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",
fromName: "v1",
toName: "v1",
},
{
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",
toName: "v1",
},
{
name: "drop",
from: "create view v1 as select a, b, c from t",
diff: "drop view v1",
cdiff: "DROP VIEW `v1`",
action: "drop",
name: "drop",
from: "create view v1 as select a, b, c from t",
diff: "drop view v1",
cdiff: "DROP VIEW `v1`",
action: "drop",
fromName: "v1",
},
{
name: "none",
Expand Down Expand Up @@ -231,6 +251,13 @@ func TestDiffViews(t *testing.T) {
_, err = sqlparser.Parse(diff)
assert.NoError(t, err)

eFrom, eTo := d.Entities()
if ts.fromName != "" {
assert.Equal(t, ts.fromName, eFrom.Name())
}
if ts.toName != "" {
assert.Equal(t, ts.toName, eTo.Name())
}
}
{
canonicalDiff := d.CanonicalStatementString()
Expand Down
22 changes: 20 additions & 2 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

//
type AlterTableEntityDiff struct {
from *CreateTableEntity
to *CreateTableEntity
alterTable *sqlparser.AlterTable
}

Expand All @@ -33,6 +35,11 @@ func (d *AlterTableEntityDiff) IsEmpty() bool {
return d.Statement() == nil
}

// IsEmpty implements EntityDiff
func (d *AlterTableEntityDiff) Entities() (from Entity, to Entity) {
return d.from, d.to
}

// Statement implements EntityDiff
func (d *AlterTableEntityDiff) Statement() sqlparser.Statement {
if d == nil {
Expand Down Expand Up @@ -67,6 +74,11 @@ func (d *CreateTableEntityDiff) IsEmpty() bool {
return d.Statement() == nil
}

// IsEmpty implements EntityDiff
func (d *CreateTableEntityDiff) Entities() (from Entity, to Entity) {
return nil, &CreateTableEntity{CreateTable: *d.createTable}
}

// Statement implements EntityDiff
func (d *CreateTableEntityDiff) Statement() sqlparser.Statement {
if d == nil {
Expand All @@ -93,6 +105,7 @@ func (d *CreateTableEntityDiff) CanonicalStatementString() (s string) {

//
type DropTableEntityDiff struct {
from *CreateTableEntity
dropTable *sqlparser.DropTable
}

Expand All @@ -101,6 +114,11 @@ func (d *DropTableEntityDiff) IsEmpty() bool {
return d.Statement() == nil
}

// IsEmpty implements EntityDiff
func (d *DropTableEntityDiff) Entities() (from Entity, to Entity) {
return d.from, nil
}

// Statement implements EntityDiff
func (d *DropTableEntityDiff) Statement() sqlparser.Statement {
if d == nil {
Expand Down Expand Up @@ -234,7 +252,7 @@ func (c *CreateTableEntity) TableDiff(other *CreateTableEntity, hints *DiffHints
// - reordered keys -- we treat that as non-diff
return nil, nil
}
return &AlterTableEntityDiff{alterTable: alterTable}, nil
return &AlterTableEntityDiff{alterTable: alterTable, from: c, to: other}, nil
}

func (c *CreateTableEntity) diffTableCharset(
Expand Down Expand Up @@ -779,5 +797,5 @@ func (c *CreateTableEntity) Drop() EntityDiff {
dropTable := &sqlparser.DropTable{
FromTables: []sqlparser.TableName{c.Table},
}
return &DropTableEntityDiff{dropTable: dropTable}
return &DropTableEntityDiff{from: c, dropTable: dropTable}
}
23 changes: 18 additions & 5 deletions go/vt/schemadiff/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestCreateTableDiff(t *testing.T) {
name string
from string
to string
fromName string
toName string
diff string
cdiff string
isError bool
Expand Down Expand Up @@ -87,11 +89,13 @@ func TestCreateTableDiff(t *testing.T) {
cdiff: "ALTER TABLE `t1` ADD COLUMN `i` int NOT NULL DEFAULT 0",
},
{
name: "dropped column",
from: "create table t1 (id int primary key, `i` int not null default 0)",
to: "create table t2 (`id` int primary key)",
diff: "alter table t1 drop column i",
cdiff: "ALTER TABLE `t1` DROP COLUMN `i`",
name: "dropped column",
from: "create table t1 (id int primary key, `i` int not null default 0)",
to: "create table t2 (`id` int primary key)",
diff: "alter table t1 drop column i",
cdiff: "ALTER TABLE `t1` DROP COLUMN `i`",
fromName: "t1",
toName: "t2",
},
{
name: "modified column",
Expand Down Expand Up @@ -645,13 +649,22 @@ func TestCreateTableDiff(t *testing.T) {
// validate we can parse back the statement
_, err := sqlparser.Parse(diff)
assert.NoError(t, err)

eFrom, eTo := alter.Entities()
if ts.fromName != "" {
assert.Equal(t, ts.fromName, eFrom.Name())
}
if ts.toName != "" {
assert.Equal(t, ts.toName, eTo.Name())
}
}
{
cdiff := alter.CanonicalStatementString()
assert.Equal(t, ts.cdiff, cdiff)
_, err := sqlparser.Parse(cdiff)
assert.NoError(t, err)
}

}
})
}
Expand Down
2 changes: 2 additions & 0 deletions go/vt/schemadiff/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Entity interface {
type EntityDiff interface {
// IsEmpty returns true when the two entities are considered identical
IsEmpty() bool
// Entities returns the two diffed entitied, aka "from" and "to"
Entities() (from Entity, to Entity)
// Statement returns a valid SQL statement that applies the diff, e.g. an ALTER TABLE ...
// It returns nil if the diff is empty
Statement() sqlparser.Statement
Expand Down
22 changes: 20 additions & 2 deletions go/vt/schemadiff/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import "vitess.io/vitess/go/vt/sqlparser"

//
type AlterViewEntityDiff struct {
from *CreateViewEntity
to *CreateViewEntity
alterView *sqlparser.AlterView
}

Expand All @@ -28,6 +30,11 @@ func (d *AlterViewEntityDiff) IsEmpty() bool {
return d.Statement() == nil
}

// IsEmpty implements EntityDiff
func (d *AlterViewEntityDiff) Entities() (from Entity, to Entity) {
return d.from, d.to
}

// Statement implements EntityDiff
func (d *AlterViewEntityDiff) Statement() sqlparser.Statement {
if d == nil {
Expand Down Expand Up @@ -62,6 +69,11 @@ func (d *CreateViewEntityDiff) IsEmpty() bool {
return d.Statement() == nil
}

// IsEmpty implements EntityDiff
func (d *CreateViewEntityDiff) Entities() (from Entity, to Entity) {
return nil, &CreateViewEntity{CreateView: *d.createView}
}

// Statement implements EntityDiff
func (d *CreateViewEntityDiff) Statement() sqlparser.Statement {
if d == nil {
Expand All @@ -88,6 +100,7 @@ func (d *CreateViewEntityDiff) CanonicalStatementString() (s string) {

//
type DropViewEntityDiff struct {
from *CreateViewEntity
dropView *sqlparser.DropView
}

Expand All @@ -96,6 +109,11 @@ func (d *DropViewEntityDiff) IsEmpty() bool {
return d.Statement() == nil
}

// IsEmpty implements EntityDiff
func (d *DropViewEntityDiff) Entities() (from Entity, to Entity) {
return d.from, nil
}

// Statement implements EntityDiff
func (d *DropViewEntityDiff) Statement() sqlparser.Statement {
if d == nil {
Expand Down Expand Up @@ -173,7 +191,7 @@ func (c *CreateViewEntity) ViewDiff(other *CreateViewEntity, hints *DiffHints) (
Select: otherStmt.Select,
CheckOption: otherStmt.CheckOption,
}
return &AlterViewEntityDiff{alterView: alterView}, nil
return &AlterViewEntityDiff{alterView: alterView, from: c, to: other}, nil
}

// Create implements Entity interface
Expand All @@ -186,5 +204,5 @@ func (c *CreateViewEntity) Drop() EntityDiff {
dropView := &sqlparser.DropView{
FromTables: []sqlparser.TableName{c.ViewName},
}
return &DropViewEntityDiff{dropView: dropView}
return &DropViewEntityDiff{from: c, dropView: dropView}
}
Loading

0 comments on commit 02b20f6

Please sign in to comment.