Skip to content

Commit

Permalink
sql/sqlcheck/condrop: skip constraint modifications (#3174)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Oct 3, 2024
1 parent 741579a commit fb15bfb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sql/sqlcheck/condrop/condrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (a *Analyzer) Analyze(_ context.Context, p *sqlcheck.Pass) error {
}
for i := range c.Changes {
d, ok := c.Changes[i].(*schema.DropForeignKey)
if !ok {
if !ok || p.File.ForeignKeySpan(c.T, d.F)&sqlcheck.SpanAdded != 0 {
continue
}
dropC := func() bool {
Expand Down
16 changes: 15 additions & 1 deletion sql/sqlcheck/sqlcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,16 @@ func (f *File) ColumnSpan(t *schema.Table, c *schema.Column) ResourceSpan {
return f.tableSpan(t).columns[c.Name]
}

// IndexSpan returns the span information for the span.
// IndexSpan returns the span information for the index.
func (f *File) IndexSpan(t *schema.Table, i *schema.Index) ResourceSpan {
return f.tableSpan(t).indexes[i.Name]
}

// ForeignKeySpan returns the span information for the foreign-key constraint.
func (f *File) ForeignKeySpan(t *schema.Table, fk *schema.ForeignKey) ResourceSpan {
return f.tableSpan(t).forkeys[fk.Symbol]
}

type (
// schemaSpan holds the span structure of a schema.
schemaSpan struct {
Expand All @@ -209,6 +214,7 @@ type (
state ResourceSpan
columns map[string]ResourceSpan
indexes map[string]ResourceSpan
forkeys map[string]ResourceSpan
}
)

Expand All @@ -230,6 +236,9 @@ func (f *File) loadSpans() {
for _, idx := range c.T.Indexes {
span.indexes[idx.Name] = SpanAdded
}
for _, fk := range c.T.ForeignKeys {
span.forkeys[fk.Symbol] = SpanAdded
}
case *schema.DropTable:
f.tableSpan(c.T).state |= SpanDropped
case *schema.ModifyTable:
Expand All @@ -244,6 +253,10 @@ func (f *File) loadSpans() {
span.indexes[c1.I.Name] = SpanAdded
case *schema.DropIndex:
span.indexes[c1.I.Name] |= SpanDropped
case *schema.AddForeignKey:
span.forkeys[c1.F.Symbol] = SpanAdded
case *schema.DropForeignKey:
span.forkeys[c1.F.Symbol] |= SpanDropped
}
}
}
Expand All @@ -267,6 +280,7 @@ func (f *File) tableSpan(t *schema.Table) *tableSpan {
span.tables[t.Name] = &tableSpan{
columns: make(map[string]ResourceSpan),
indexes: make(map[string]ResourceSpan),
forkeys: make(map[string]ResourceSpan),
}
}
return f.spans[t.Schema.Name].tables[t.Name]
Expand Down

0 comments on commit fb15bfb

Please sign in to comment.