From 1c495a89e09403cd59bd535716fc198156966765 Mon Sep 17 00:00:00 2001 From: Bill Moran Date: Thu, 25 Apr 2024 11:03:27 -0400 Subject: [PATCH] Remove GlobalDiffViews, refactor it into standalone functions, and some general cleanup of that code --- lib/format/pgsql8/diff.go | 4 ++-- lib/format/pgsql8/diff_views.go | 33 +++++++++++++-------------------- lib/format/pgsql8/operations.go | 2 +- lib/format/pgsql8/pgsql8.go | 1 - 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/format/pgsql8/diff.go b/lib/format/pgsql8/diff.go index 5e2bd1f..9a5e510 100644 --- a/lib/format/pgsql8/diff.go +++ b/lib/format/pgsql8/diff.go @@ -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 { @@ -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) { diff --git a/lib/format/pgsql8/diff_views.go b/lib/format/pgsql8/diff_views.go index 1e50e63..599800c 100644 --- a/lib/format/pgsql8/diff_views.go +++ b/lib/format/pgsql8/diff_views.go @@ -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 @@ -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) } diff --git a/lib/format/pgsql8/operations.go b/lib/format/pgsql8/operations.go index d2333b5..004f548 100644 --- a/lib/format/pgsql8/operations.go +++ b/lib/format/pgsql8/operations.go @@ -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 { diff --git a/lib/format/pgsql8/pgsql8.go b/lib/format/pgsql8/pgsql8.go index 8cb150d..3af3d34 100644 --- a/lib/format/pgsql8/pgsql8.go +++ b/lib/format/pgsql8/pgsql8.go @@ -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()