Skip to content

Commit

Permalink
Incremental removal of FatalIfError()
Browse files Browse the repository at this point in the history
  • Loading branch information
williammoran committed May 6, 2024
1 parent 2ba6c5d commit 7388ed0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
38 changes: 27 additions & 11 deletions lib/format/pgsql8/diff_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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?
Expand All @@ -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},
})
}

Expand All @@ -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},
})
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/format/pgsql8/xml_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7388ed0

Please sign in to comment.