Skip to content

Commit

Permalink
Alter vschema events (#5491)
Browse files Browse the repository at this point in the history
* Send schema change events for alter vschema

Signed-off-by: Rafer Hazen <rafer@ralua.com>

* Don't send schema changes for DropDatabase either

Signed-off-by: Rafer Hazen <rafer@ralua.com>

---------

Signed-off-by: Rafer Hazen <rafer@ralua.com>
  • Loading branch information
rafer authored Jun 27, 2024
1 parent 0bfc61f commit 0fee57d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ require (
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
github.com/planetscale/kmsbackup v0.4.0
github.com/planetscale/psevents v0.0.0-20240516215623-132f292ad9c2
github.com/planetscale/psevents v0.0.0-20240626195140-2ae695cbaa65
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
github.com/segmentio/fasthash v1.0.3
github.com/segmentio/kafka-go v0.4.47
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ github.com/planetscale/kmsbackup v0.4.0 h1:K2fiZaONJ7ThNeTi7FvsPVq1UJR228uot/R+F
github.com/planetscale/kmsbackup v0.4.0/go.mod h1:MQ7CT5k5xfpf1UXKQunLkxNFlvoZKXvZceIlI7vjKLw=
github.com/planetscale/pargzip v0.0.0-20201116224723-90c7fc03ea8a h1:y0OpQ4+5tKxeh9+H+2cVgASl9yMZYV9CILinKOiKafA=
github.com/planetscale/pargzip v0.0.0-20201116224723-90c7fc03ea8a/go.mod h1:GJFUzQuXIoB2Kjn1ZfDhJr/42D5nWOqRcIQVgCxTuIE=
github.com/planetscale/psevents v0.0.0-20240516215623-132f292ad9c2 h1:XTfDOozw2lLjs/X3o9gep+Oa+76SXQEa+h/Ef4KUVjU=
github.com/planetscale/psevents v0.0.0-20240516215623-132f292ad9c2/go.mod h1:2yqEdV/4PFyzfv/d4aA/0SNKfQ3e1zKZodDyNHZXLeA=
github.com/planetscale/psevents v0.0.0-20240626195140-2ae695cbaa65 h1:WhLnyw9LEVvsXy5Ui5bjrFz5a1Sp169YTqfc8RmoqZM=
github.com/planetscale/psevents v0.0.0-20240626195140-2ae695cbaa65/go.mod h1:2yqEdV/4PFyzfv/d4aA/0SNKfQ3e1zKZodDyNHZXLeA=
github.com/planetscale/vtprotobuf v0.5.0 h1:l8PXm6Colok5z6qQLNhAj2Jq5BfoMTIHxLER5a6nDqM=
github.com/planetscale/vtprotobuf v0.5.0/go.mod h1:wm1N3qk9G/4+VM1WhpkLbvY/d8+0PbwYYpP5P5VhTks=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
30 changes: 18 additions & 12 deletions go/vt/vtgate/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,21 +541,13 @@ func (ii *Insights) makeSchemaChangeMessage(ls *logstats.LogStats) ([]byte, erro
return nil, err
}

ddlStmt, ok := stmt.(sqlparser.DDLStatement)
if !ok {
return nil, fmt.Errorf("expected a DDLStatement but got a %T", stmt)
}

operation := pbvtgate.SchemaChange_UNKNOWN

switch ddlStmt.(type) {
switch stmt.(type) {
case *sqlparser.CreateTable:
operation = pbvtgate.SchemaChange_CREATE_TABLE
case *sqlparser.AlterTable:
operation = pbvtgate.SchemaChange_ALTER_TABLE
case *sqlparser.TruncateTable:
// Truncate table isn't really a schema change
return nil, nil
case *sqlparser.DropTable:
operation = pbvtgate.SchemaChange_DROP_TABLE
case *sqlparser.RenameTable:
Expand All @@ -566,14 +558,28 @@ func (ii *Insights) makeSchemaChangeMessage(ls *logstats.LogStats) ([]byte, erro
operation = pbvtgate.SchemaChange_CREATE_VIEW
case *sqlparser.DropView:
operation = pbvtgate.SchemaChange_DROP_VIEW
case *sqlparser.AlterVschema:
operation = pbvtgate.SchemaChange_ALTER_VSCHEMA
case *sqlparser.DropDatabase:
return nil, nil
case *sqlparser.CreateDatabase:
return nil, nil
case *sqlparser.TruncateTable:
// Truncate table isn't really a schema change
return nil, nil
}

// Sometimes DDL statements aren't fully parsed (but are still executed).
fullyParsed := true
// Sometimes DDLStatements aren't fully parsed (but are still executed).
// If that happens we want to send along the original query as the DDL.
fullyParsed := ddlStmt.IsFullyParsed()
ddlStmt, ok := stmt.(sqlparser.DDLStatement)
if ok {
fullyParsed = ddlStmt.IsFullyParsed()
}

var ddl string
if fullyParsed {
ddl = sqlparser.CanonicalString(ddlStmt)
ddl = sqlparser.CanonicalString(stmt)
} else {
ddl = ls.SQL
}
Expand Down
4 changes: 4 additions & 0 deletions go/vt/vtgate/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ func TestInsightsSchemaChanges(t *testing.T) {
{sql: "alter view foo as select b,c from bar"},
{sql: "create view foo(x,y,z) as select a,b from bar"},
{sql: "drop view foo"},
{sql: "alter vschema add table foo"},
},
[]insightsKafkaExpectation{
expect(queryStatsBundleTopic, "create table foo"),
Expand All @@ -499,6 +500,9 @@ func TestInsightsSchemaChanges(t *testing.T) {

expect(queryStatsBundleTopic, "drop view foo"),
expect(schemaChangeTopic, "DROP VIEW `foo`", "operation:DROP_VIEW", "normalized:true"),

expect(queryStatsBundleTopic, "alter vschema add table foo"),
expect(schemaChangeTopic, "ALTER VSCHEMA", "normalized:true"),
})
}

Expand Down

0 comments on commit 0fee57d

Please sign in to comment.