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

sql/internal/sqlx: add mviews to standard differ #2114

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions sql/internal/sqlx/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ func (d *Diff) schemaDiff(from, to *schema.Schema, opts *schema.DiffOptions) ([]
}
// Drop or modify views.
for _, v1 := range from.Views {
v2, ok := to.View(v1.Name)
v2, ok := findView(to, v1)
if !ok {
// Changing a view to materialized (and vice versa)
// generates a drop and add.
changes = opts.AddOrSkip(changes, &schema.DropView{V: v1})
continue
}
Expand All @@ -196,7 +198,7 @@ func (d *Diff) schemaDiff(from, to *schema.Schema, opts *schema.DiffOptions) ([]
}
// Add views.
for _, v1 := range to.Views {
if _, ok := from.View(v1.Name); !ok {
if _, ok := findView(from, v1); !ok {
changes = opts.AddOrSkip(changes, &schema.AddView{V: v1})
}
}
Expand Down Expand Up @@ -679,3 +681,11 @@ func SingleQuote(s string) (string, error) {
func TrimViewExtra(s string) string {
return strings.Trim(s, " \n\t;")
}

// findView finds the view by its name and its type.
func findView(s *schema.Schema, v1 *schema.View) (*schema.View, bool) {
if v1.Materialized() {
return s.Materialized(v1.Name)
}
return s.View(v1.Name)
}