From 7388ed0dd352c20e57396dc2677e3a87ec72295d Mon Sep 17 00:00:00 2001 From: Bill Moran Date: Mon, 6 May 2024 10:16:59 -0400 Subject: [PATCH] Incremental removal of FatalIfError() --- lib/format/pgsql8/diff_types.go | 38 +++++++++++++++++++++++---------- lib/format/pgsql8/xml_parser.go | 8 +++++-- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/format/pgsql8/diff_types.go b/lib/format/pgsql8/diff_types.go index a135166..7ecb592 100644 --- a/lib/format/pgsql8/diff_types.go +++ b/lib/format/pgsql8/diff_types.go @@ -13,7 +13,10 @@ import ( func diffTypes(l *slog.Logger, differ *diff, ofs output.OutputFileSegmenter, oldSchema *ir.Schema, newSchema *ir.Schema) error { dropTypes(ofs, oldSchema, newSchema) - createTypes(ofs, oldSchema, newSchema) + err := createTypes(ofs, oldSchema, newSchema) + if err != nil { + return err + } // there is no alter for types // find types that still exist that are different @@ -50,7 +53,9 @@ func diffTypes(l *slog.Logger, differ *diff, ofs output.OutputFileSegmenter, old } else { ofs.WriteSql(getDropTypeSql(oldSchema, oldType)...) sql, err := getCreateTypeSql(newSchema, newType) - lib.GlobalDBSteward.FatalIfError(err, "Could not get data type creation sql for type alter") + if err != nil { + return fmt.Errorf("could not get data type creation sql for type alter: %w", err) + } ofs.WriteSql(sql...) } @@ -79,14 +84,17 @@ func dropTypes(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, newSchema * } } -func createTypes(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, newSchema *ir.Schema) { +func createTypes(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, newSchema *ir.Schema) error { for _, newType := range newSchema.Types { if oldSchema.TryGetTypeNamed(newType.Name) == nil { sql, err := getCreateTypeSql(newSchema, newType) - lib.GlobalDBSteward.FatalIfError(err, "Could not get data type creation sql for type diff") + if err != nil { + return fmt.Errorf("could not get data type creation sql for type diff: %w", err) + } ofs.WriteSql(sql...) } } + return nil } func diffDomain(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldType *ir.TypeDef, newSchema *ir.Schema, newType *ir.TypeDef) { @@ -103,12 +111,12 @@ func diffDomain(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldType *i ofs.WriteSql(sql...) } - ref := sql.TypeRef{newSchema.Name, newType.Name} + ref := sql.TypeRef{Schema: newSchema.Name, Type: newType.Name} if oldInfo.Default != "" && newInfo.Default == "" { ofs.WriteSql(&sql.Annotated{ Annotation: "domain default dropped", - Wrapped: &sql.TypeDomainAlterDropDefault{ref}, + Wrapped: &sql.TypeDomainAlterDropDefault{Type: ref}, }) } else if oldInfo.Default != newInfo.Default { // TODO(feat) what about recursively resolving this in the case that the base type is another user defined type? @@ -128,7 +136,7 @@ func diffDomain(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldType *i if oldInfo.Nullable != newInfo.Nullable { ofs.WriteSql(&sql.Annotated{ Annotation: "domain nullability changed", - Wrapped: &sql.TypeDomainAlterSetNullable{ref, newInfo.Nullable}, + Wrapped: &sql.TypeDomainAlterSetNullable{Type: ref, Nullable: newInfo.Nullable}, }) } @@ -137,19 +145,27 @@ func diffDomain(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldType *i if oldConstraint != nil { if !oldConstraint.Equals(newConstraint) { ofs.WriteSql(sql.NewComment("domain constraint %s changed from %s", oldConstraint.Name, oldConstraint.Check)) - ofs.WriteSql(&sql.TypeDomainAlterDropConstraint{ref, oldConstraint.Name}) - ofs.WriteSql(&sql.TypeDomainAlterAddConstraint{ref, newConstraint.Name, sql.RawSql(newConstraint.GetNormalizedCheck())}) + ofs.WriteSql(&sql.TypeDomainAlterDropConstraint{Type: ref, Constraint: oldConstraint.Name}) + ofs.WriteSql(&sql.TypeDomainAlterAddConstraint{ + Type: ref, + Constraint: newConstraint.Name, + Check: sql.RawSql(newConstraint.GetNormalizedCheck())}, + ) } } else { ofs.WriteSql(sql.NewComment("domain constraint %s added", newConstraint.Name)) - ofs.WriteSql(&sql.TypeDomainAlterAddConstraint{ref, newConstraint.Name, sql.RawSql(newConstraint.GetNormalizedCheck())}) + ofs.WriteSql(&sql.TypeDomainAlterAddConstraint{ + Type: ref, + Constraint: newConstraint.Name, + Check: sql.RawSql(newConstraint.GetNormalizedCheck())}, + ) } } for _, oldConstraint := range oldType.DomainConstraints { if newType.TryGetDomainConstraintNamed(oldConstraint.Name) == nil { ofs.WriteSql(&sql.Annotated{ Annotation: fmt.Sprintf("domain constraint %s removed", oldConstraint.Name), - Wrapped: &sql.TypeDomainAlterDropConstraint{ref, oldConstraint.Name}, + Wrapped: &sql.TypeDomainAlterDropConstraint{Type: ref, Constraint: oldConstraint.Name}, }) } } diff --git a/lib/format/pgsql8/xml_parser.go b/lib/format/pgsql8/xml_parser.go index 45f1c55..3a6ca72 100644 --- a/lib/format/pgsql8/xml_parser.go +++ b/lib/format/pgsql8/xml_parser.go @@ -29,12 +29,16 @@ func tryNewSlonyRange(firstStr, lastStr string, parts int) (*slonyRange, error) } first, err := strconv.Atoi(firstStr) - lib.GlobalDBSteward.FatalIfError(err, "tablePartitionOption 'firstSlonyId' must be a number") + if err != nil { + return nil, fmt.Errorf("tablePartitionOption 'firstSlonyId' must be a number: %w", err) + } last := first + parts - 1 if lastStr != "" { lastTmp, err := strconv.Atoi(lastStr) - lib.GlobalDBSteward.FatalIfError(err, "tablePartitionOption 'lastSlonyId' must be a number") + if err != nil { + return nil, fmt.Errorf("tablePartitionOption 'lastSlonyId' must be a number: %w", err) + } allocated := lastTmp - first + 1 if allocated != parts { return nil, fmt.Errorf("requested %d partitions but provided %d slony IDs", parts, allocated)