Skip to content

Commit

Permalink
Remove GlobalDiffViews, refactor it into standalone functions, and so…
Browse files Browse the repository at this point in the history
…me general cleanup of that code
  • Loading branch information
williammoran committed Apr 25, 2024
1 parent 1867978 commit 1c495a8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/format/pgsql8/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (self *Diff) updateStructure(stage1 output.OutputFileSegmenter, stage3 outp

// drop all views in all schemas, regardless whether dependency order is known or not
// TODO(go,4) would be so cool if we could parse the view def and only recreate what's required
GlobalDiffViews.DropViewsOrdered(stage1, dbsteward.OldDatabase, dbsteward.NewDatabase)
dropViewsOrdered(stage1, dbsteward.OldDatabase, dbsteward.NewDatabase)

// TODO(go,3) should we just always use table deps?
if len(self.NewTableDependency) == 0 {
Expand Down Expand Up @@ -277,7 +277,7 @@ func (self *Diff) updateStructure(stage1 output.OutputFileSegmenter, stage3 outp
}
}

GlobalDiffViews.CreateViewsOrdered(stage3, dbsteward.OldDatabase, dbsteward.NewDatabase)
createViewsOrdered(stage3, dbsteward.OldDatabase, dbsteward.NewDatabase)
}

func (self *Diff) updatePermissions(stage1 output.OutputFileSegmenter, stage3 output.OutputFileSegmenter) {
Expand Down
33 changes: 13 additions & 20 deletions lib/format/pgsql8/diff_views.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,43 @@ import (
"github.com/dbsteward/dbsteward/lib/output"
)

type DiffViews struct {
}

func NewDiffViews() *DiffViews {
return &DiffViews{}
}

// TODO(go,core) lift some of these to sql99

func (self *DiffViews) CreateViewsOrdered(ofs output.OutputFileSegmenter, oldDoc *ir.Definition, newDoc *ir.Definition) {
self.forEachViewInDepOrder(newDoc, func(newRef ir.ViewRef) {
func createViewsOrdered(ofs output.OutputFileSegmenter, oldDoc *ir.Definition, newDoc *ir.Definition) {
forEachViewInDepOrder(newDoc, func(newRef ir.ViewRef) {
oldSchema := oldDoc.TryGetSchemaNamed(newRef.Schema.Name)
var oldView *ir.View
if oldSchema != nil {
// TODO(go,nth) allow nil receivers in TryGet methods to alleviate branching
oldView = oldSchema.TryGetViewNamed(newRef.View.Name)
}
if self.shouldCreateView(oldView, newRef.View) {
if shouldCreateView(oldView, newRef.View) {
ofs.WriteSql(getCreateViewSql(newRef.Schema, newRef.View)...)
}
})
}

func (self *DiffViews) shouldCreateView(oldView, newView *ir.View) bool {
func shouldCreateView(oldView, newView *ir.View) bool {
return oldView == nil || lib.GlobalDBSteward.AlwaysRecreateViews || !oldView.Equals(newView, ir.SqlFormatPgsql8)
}

func (self *DiffViews) DropViewsOrdered(ofs output.OutputFileSegmenter, oldDoc *ir.Definition, newDoc *ir.Definition) {
self.forEachViewInDepOrder(oldDoc, func(oldViewRef ir.ViewRef) {
func dropViewsOrdered(ofs output.OutputFileSegmenter, oldDoc *ir.Definition, newDoc *ir.Definition) {
forEachViewInDepOrder(oldDoc, func(oldViewRef ir.ViewRef) {
newSchema := newDoc.TryGetSchemaNamed(oldViewRef.Schema.Name)
newView := newSchema.TryGetViewNamed(oldViewRef.View.Name)
if self.shouldDropView(oldViewRef.View, newSchema, newView) {
if shouldDropView(oldViewRef.View, newSchema, newView) {
ofs.WriteSql(getDropViewSql(oldViewRef.Schema, oldViewRef.View)...)
}
})
}

func (self *DiffViews) shouldDropView(oldView *ir.View, newSchema *ir.Schema, newView *ir.View) bool {
func shouldDropView(oldView *ir.View, newSchema *ir.Schema, newView *ir.View) bool {
// don't drop the view if new_schema is null - we've already dropped the view by this point
// otherwise, drop if it changed or no longer exists
return newSchema != nil && !oldView.Equals(newView, ir.SqlFormatPgsql8)
}

func (self *DiffViews) forEachViewInDepOrder(doc *ir.Definition, callback func(ir.ViewRef)) {
func forEachViewInDepOrder(doc *ir.Definition, callback func(ir.ViewRef)) {
// TODO(go,3) unify this with XmlParser.TableDepOrder?
if doc == nil {
return
Expand All @@ -59,23 +52,23 @@ func (self *DiffViews) forEachViewInDepOrder(doc *ir.Definition, callback func(i

for _, rootSchema := range doc.Schemas {
for _, rootView := range rootSchema.Views {
ref := ir.ViewRef{rootSchema, rootView}
ref := ir.ViewRef{Schema: rootSchema, View: rootView}
if _, ok := visited[ref]; ok {
continue
}
self.dfsViewDeps(doc, ref, visited, callback)
dfsViewDeps(doc, ref, visited, callback)
}
}
}

func (self *DiffViews) dfsViewDeps(doc *ir.Definition, ref ir.ViewRef, visited map[ir.ViewRef]bool, callback func(ir.ViewRef)) {
func dfsViewDeps(doc *ir.Definition, ref ir.ViewRef, visited map[ir.ViewRef]bool, callback func(ir.ViewRef)) {
if _, ok := visited[ref]; ok {
return
}
visited[ref] = true

for _, dep := range getViewDependencies(doc, ref.Schema, ref.View) {
self.dfsViewDeps(doc, dep, visited, callback)
dfsViewDeps(doc, dep, visited, callback)
}
callback(ref)
}
2 changes: 1 addition & 1 deletion lib/format/pgsql8/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ func buildSchema(doc *ir.Definition, ofs output.OutputFileSegmenter, tableDep []
}
}

GlobalDiffViews.CreateViewsOrdered(ofs, nil, doc)
createViewsOrdered(ofs, nil, doc)

// view permission grants
for _, schema := range doc.Schemas {
Expand Down
1 change: 0 additions & 1 deletion lib/format/pgsql8/pgsql8.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "github.com/dbsteward/dbsteward/lib/format"

var GlobalOperations = NewOperations()
var GlobalSchema = NewSchema()
var GlobalDiffViews = NewDiffViews()
var GlobalDiff = NewDiff()
var GlobalXmlParser = NewXmlParser()

Expand Down

0 comments on commit 1c495a8

Please sign in to comment.