From ae512a24e22f1d0a632a6847d460d47bd0dcd9ed Mon Sep 17 00:00:00 2001 From: Bill Moran Date: Thu, 25 Apr 2024 07:23:44 -0400 Subject: [PATCH] Remove GlobalFunctions, refactor it into standalone functions, and some general cleanup of that code --- lib/format/pgsql8/diff.go | 2 +- lib/format/pgsql8/diff_functions.go | 8 +++---- lib/format/pgsql8/diff_types.go | 4 ++-- lib/format/pgsql8/function.go | 33 +++++++++++++---------------- lib/format/pgsql8/operations.go | 6 +++--- lib/format/pgsql8/pgsql8.go | 1 - lib/format/pgsql8/schema.go | 2 +- 7 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/format/pgsql8/diff.go b/lib/format/pgsql8/diff.go index b3846ea..ec58b11 100644 --- a/lib/format/pgsql8/diff.go +++ b/lib/format/pgsql8/diff.go @@ -324,7 +324,7 @@ func (self *Diff) updatePermissions(stage1 output.OutputFileSegmenter, stage3 ou oldFunc := oldSchema.TryGetFunctionMatching(newFunc) for _, newGrant := range newFunc.Grants { if oldFunc == nil || !ir.HasPermissionsOf(oldFunc, newGrant, ir.SqlFormatPgsql8) { - stage1.WriteSql(GlobalFunction.GetGrantSql(newDoc, newSchema, newFunc, newGrant)...) + stage1.WriteSql(getFunctionGrantSql(newSchema, newFunc, newGrant)...) } } } diff --git a/lib/format/pgsql8/diff_functions.go b/lib/format/pgsql8/diff_functions.go index 8e84712..b506979 100644 --- a/lib/format/pgsql8/diff_functions.go +++ b/lib/format/pgsql8/diff_functions.go @@ -18,7 +18,7 @@ func (self *DiffFunctions) DiffFunctions(stage1 output.OutputFileSegmenter, stag if oldSchema != nil { for _, oldFunction := range oldSchema.Functions { if newSchema.TryGetFunctionMatching(oldFunction) == nil { - stage3.WriteSql(GlobalFunction.GetDropSql(oldSchema, oldFunction)...) + stage3.WriteSql(getFunctionDropSql(oldSchema, oldFunction)...) } } } @@ -27,16 +27,16 @@ func (self *DiffFunctions) DiffFunctions(stage1 output.OutputFileSegmenter, stag for _, newFunction := range newSchema.Functions { oldFunction := oldSchema.TryGetFunctionMatching(newFunction) if oldFunction == nil || !oldFunction.Equals(newFunction, ir.SqlFormatPgsql8) { - stage1.WriteSql(GlobalFunction.GetCreationSql(newSchema, newFunction)...) + stage1.WriteSql(getFunctionCreationSql(newSchema, newFunction)...) } else if newFunction.ForceRedefine { stage1.WriteSql(sql.NewComment("Function %s.%s has forceRedefine set to true", newSchema.Name, newFunction.Name)) - stage1.WriteSql(GlobalFunction.GetCreationSql(newSchema, newFunction)...) + stage1.WriteSql(getFunctionCreationSql(newSchema, newFunction)...) } else { oldReturnType := oldSchema.TryGetTypeNamed(newFunction.Returns) newReturnType := newSchema.TryGetTypeNamed(newFunction.Returns) if oldReturnType != nil && newReturnType != nil && !oldReturnType.Equals(newReturnType) { stage1.WriteSql(sql.NewComment("Function %s.%s return type %s has changed", newSchema.Name, newFunction.Name, newReturnType.Name)) - stage1.WriteSql(GlobalFunction.GetCreationSql(newSchema, newFunction)...) + stage1.WriteSql(getFunctionCreationSql(newSchema, newFunction)...) } } } diff --git a/lib/format/pgsql8/diff_types.go b/lib/format/pgsql8/diff_types.go index 95c645a..230cd9f 100644 --- a/lib/format/pgsql8/diff_types.go +++ b/lib/format/pgsql8/diff_types.go @@ -42,7 +42,7 @@ func (self *DiffTypes) DiffTypes(ofs output.OutputFileSegmenter, oldSchema *ir.S "Type migration of %s.%s requires recreating dependent function %s.%s", newSchema.Name, newType.Name, oldSchema.Name, oldFunc.Name, )) - ofs.WriteSql(GlobalFunction.GetDropSql(oldSchema, oldFunc)...) + ofs.WriteSql(getFunctionDropSql(oldSchema, oldFunc)...) } columns, sql := GlobalDataType.AlterColumnTypePlaceholder(oldSchema, oldType) @@ -59,7 +59,7 @@ func (self *DiffTypes) DiffTypes(ofs output.OutputFileSegmenter, oldSchema *ir.S // functions are only recreated if they changed elsewise, so need to create them here for _, newFunc := range GlobalSchema.GetFunctionsDependingOnType(newSchema, newType) { - ofs.WriteSql(GlobalFunction.GetCreationSql(newSchema, newFunc)...) + ofs.WriteSql(getFunctionCreationSql(newSchema, newFunc)...) } ofs.WriteSql(GlobalDataType.AlterColumnTypeRestore(columns, newSchema, newType)...) diff --git a/lib/format/pgsql8/function.go b/lib/format/pgsql8/function.go index 4340528..c54c502 100644 --- a/lib/format/pgsql8/function.go +++ b/lib/format/pgsql8/function.go @@ -11,15 +11,7 @@ import ( "github.com/dbsteward/dbsteward/lib/util" ) -type Function struct { - IncludeColumnDefaultNextvalInCreateSql bool -} - -func NewFunction() *Function { - return &Function{} -} - -func (self *Function) DefinitionReferencesTable(definition *ir.FunctionDefinition) *lib.QualifiedTable { +func functionDefinitionReferencesTable(definition *ir.FunctionDefinition) *lib.QualifiedTable { // TODO(feat) a function could reference many tables, but this only returns the first; make it understand many tables // TODO(feat) this won't detect quoted table names // TODO(go,pgsql) test this @@ -42,8 +34,8 @@ func (self *Function) DefinitionReferencesTable(definition *ir.FunctionDefinitio return &parsed } -func (self *Function) GetCreationSql(schema *ir.Schema, function *ir.Function) []output.ToSql { - ref := sql.FunctionRef{schema.Name, function.Name, function.ParamSigs()} +func getFunctionCreationSql(schema *ir.Schema, function *ir.Function) []output.ToSql { + ref := sql.FunctionRef{Schema: schema.Name, Function: function.Name, Params: function.ParamSigs()} def := function.TryGetDefinition(ir.SqlFormatPgsql8) out := []output.ToSql{ &sql.FunctionCreate{ @@ -72,27 +64,32 @@ func (self *Function) GetCreationSql(schema *ir.Schema, function *ir.Function) [ return out } -func (self *Function) GetDropSql(schema *ir.Schema, function *ir.Function) []output.ToSql { +func getFunctionDropSql(schema *ir.Schema, function *ir.Function) []output.ToSql { types := function.ParamTypes() for i, paramType := range types { // TODO(feat) there's evidence in get_drop_sql that postgres only recognizes the normalized typenames here. // we should look for other cases and validate behavior - types[i] = self.normalizeParameterType(paramType) + types[i] = normalizeFunctionParameterType(paramType) } return []output.ToSql{ - &sql.FunctionDrop{sql.FunctionRef{schema.Name, function.Name, types}}, + &sql.FunctionDrop{ + Function: sql.FunctionRef{ + Schema: schema.Name, + Function: function.Name, + Params: types, + }}, } } -func (self *Function) normalizeParameterType(paramType string) string { +func normalizeFunctionParameterType(paramType string) string { if strings.EqualFold(paramType, "character varying") || strings.EqualFold(paramType, "varying") { return "varchar" } return paramType } -func (self *Function) GetGrantSql(doc *ir.Definition, schema *ir.Schema, fn *ir.Function, grant *ir.Grant) []output.ToSql { +func getFunctionGrantSql(schema *ir.Schema, fn *ir.Function, grant *ir.Grant) []output.ToSql { roles := make([]string, len(grant.Roles)) for i, role := range grant.Roles { roles[i] = lib.GlobalXmlParser.RoleEnum(lib.GlobalDBSteward.NewDatabase, role) @@ -109,7 +106,7 @@ func (self *Function) GetGrantSql(doc *ir.Definition, schema *ir.Schema, fn *ir. ddl := []output.ToSql{ &sql.FunctionGrant{ - Function: sql.FunctionRef{schema.Name, fn.Name, fn.ParamTypes()}, + Function: sql.FunctionRef{Schema: schema.Name, Function: fn.Name, Params: fn.ParamTypes()}, Perms: []string(grant.Permissions), Roles: roles, CanGrant: grant.CanGrant(), @@ -122,7 +119,7 @@ func (self *Function) GetGrantSql(doc *ir.Definition, schema *ir.Schema, fn *ir. } // TODO(go,3) move this to model -func (self *Function) FunctionDependsOnType(fn *ir.Function, typeSchema *ir.Schema, datatype *ir.DataType) bool { +func functionDependsOnType(fn *ir.Function, typeSchema *ir.Schema, datatype *ir.DataType) bool { // TODO(feat) what about composite/domain types that are also dependent on the type? further refinement needed qualifiedName := typeSchema.Name + "." + datatype.Name returns := strings.TrimRight(fn.Returns, "[] ") // allow for arrays diff --git a/lib/format/pgsql8/operations.go b/lib/format/pgsql8/operations.go index e67e4f7..a736dda 100644 --- a/lib/format/pgsql8/operations.go +++ b/lib/format/pgsql8/operations.go @@ -100,7 +100,7 @@ outer: for _, function := range schema.Functions { if definition := function.TryGetDefinition(ir.SqlFormatPgsql8); definition != nil { if strings.EqualFold(definition.Language, "sql") { - referenced := GlobalFunction.DefinitionReferencesTable(definition) + referenced := functionDefinitionReferencesTable(definition) if referenced == nil { continue } @@ -918,12 +918,12 @@ func buildSchema(doc *ir.Definition, ofs output.OutputFileSegmenter, tableDep [] for _, schema := range doc.Schemas { for _, function := range schema.Functions { if function.HasDefinition(ir.SqlFormatPgsql8) { - ofs.WriteSql(GlobalFunction.GetCreationSql(schema, function)...) + ofs.WriteSql(getFunctionCreationSql(schema, function)...) // when pg:build_schema() is doing its thing for straight builds, include function permissions // they are not included in pg_function::get_creation_sql() for _, grant := range function.Grants { - ofs.WriteSql(GlobalFunction.GetGrantSql(doc, schema, function, grant)...) + ofs.WriteSql(getFunctionGrantSql(schema, function, grant)...) } } } diff --git a/lib/format/pgsql8/pgsql8.go b/lib/format/pgsql8/pgsql8.go index 70f6911..a843af5 100644 --- a/lib/format/pgsql8/pgsql8.go +++ b/lib/format/pgsql8/pgsql8.go @@ -3,7 +3,6 @@ package pgsql8 import "github.com/dbsteward/dbsteward/lib/format" var GlobalOperations = NewOperations() -var GlobalFunction = NewFunction() var GlobalIndex = NewIndex() var GlobalLanguage = NewLanguage() var GlobalPermission = NewPermission() diff --git a/lib/format/pgsql8/schema.go b/lib/format/pgsql8/schema.go index d8a7d20..8e738b1 100644 --- a/lib/format/pgsql8/schema.go +++ b/lib/format/pgsql8/schema.go @@ -89,7 +89,7 @@ func (self *Schema) GetGrantSql(doc *ir.Definition, schema *ir.Schema, grant *ir func (self *Schema) GetFunctionsDependingOnType(schema *ir.Schema, datatype *ir.DataType) []*ir.Function { out := []*ir.Function{} for _, fn := range schema.Functions { - if GlobalFunction.FunctionDependsOnType(fn, schema, datatype) { + if functionDependsOnType(fn, schema, datatype) { out = append(out, fn) } }