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 5ebc68f commit e5fe1f9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
10 changes: 8 additions & 2 deletions lib/format/pgsql8/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ func (d *diff) updateStructure(stage1 output.OutputFileSegmenter, stage3 output.
dbsteward.FatalIfError(err, "while creating tables")
err = diffTables(d.Logger(), stage1, stage3, oldSchema, newSchema)
dbsteward.FatalIfError(err, "while diffing tables")
diffIndexes(stage1, oldSchema, newSchema)
err = diffIndexes(stage1, oldSchema, newSchema)
if err != nil {
return err
}
diffClusters(stage1, oldSchema, newSchema)
createConstraints(d.Logger(), stage1, oldSchema, newSchema, sql99.ConstraintTypePrimaryKey)
err = diffTriggers(stage1, oldSchema, newSchema)
Expand Down Expand Up @@ -302,7 +305,10 @@ func (d *diff) updateStructure(stage1 output.OutputFileSegmenter, stage3 output.
lib.GlobalDBSteward.FatalIfError(err, "while creating table %s.%s", newSchema.Name, newTable.Name)
err = diffTable(d.Logger(), stage1, stage3, oldSchema, oldTable, newSchema, newTable)
lib.GlobalDBSteward.FatalIfError(err, "while diffing table %s.%s", newSchema.Name, newTable.Name)
diffIndexesTable(stage1, oldSchema, oldTable, newSchema, newTable)
err = diffIndexesTable(stage1, oldSchema, oldTable, newSchema, newTable)
if err != nil {
return err
}
diffClustersTable(stage1, oldTable, newSchema, newTable)
err = createConstraintsTable(d.Logger(), stage1, oldSchema, oldTable, newSchema, newTable, sql99.ConstraintTypePrimaryKey)
if err != nil {
Expand Down
31 changes: 23 additions & 8 deletions lib/format/pgsql8/diff_indexes.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package pgsql8

import (
"fmt"

"github.com/dbsteward/dbsteward/lib"
"github.com/dbsteward/dbsteward/lib/ir"
"github.com/dbsteward/dbsteward/lib/output"
)

func diffIndexes(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, newSchema *ir.Schema) {
func diffIndexes(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, newSchema *ir.Schema) error {
for _, newTable := range newSchema.Tables {
var oldTable *ir.Table
if oldSchema != nil {
// TODO(feat) what about renames?
oldTable = oldSchema.TryGetTableNamed(newTable.Name)
}
diffIndexesTable(ofs, oldSchema, oldTable, newSchema, newTable)
err := diffIndexesTable(ofs, oldSchema, oldTable, newSchema, newTable)
if err != nil {
return err
}
}
return nil
}

func diffIndexesTable(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldTable *ir.Table, newSchema *ir.Schema, newTable *ir.Table) {
for _, oldIndex := range getOldIndexes(oldSchema, oldTable, newSchema, newTable) {
func diffIndexesTable(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldTable *ir.Table, newSchema *ir.Schema, newTable *ir.Table) error {
oldIndexes, err := getOldIndexes(oldSchema, oldTable, newSchema, newTable)
if err != nil {
return err
}
for _, oldIndex := range oldIndexes {
// TODO(go,pgsql) old code used new schema/table instead of old, but I believe that is incorrect. need to verify this behavior change
ofs.WriteSql(getDropIndexSql(oldSchema, oldIndex)...)
}
Expand All @@ -27,9 +37,10 @@ func diffIndexesTable(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldT
for _, newIndex := range getNewIndexes(oldSchema, oldTable, newSchema, newTable) {
ofs.WriteSql(getCreateIndexSql(newSchema, newTable, newIndex)...)
}
return nil
}

func getOldIndexes(oldSchema *ir.Schema, oldTable *ir.Table, newSchema *ir.Schema, newTable *ir.Table) []*ir.Index {
func getOldIndexes(oldSchema *ir.Schema, oldTable *ir.Table, newSchema *ir.Schema, newTable *ir.Table) ([]*ir.Index, error) {
out := []*ir.Index{}

// if new table is nil we don't need to drop those indexes, they'll be dropped implicitly from the DROP TABLE
Expand All @@ -42,17 +53,21 @@ func getOldIndexes(oldSchema *ir.Schema, oldTable *ir.Table, newSchema *ir.Schem
// TODO(go,pgsql) this logic is slightly different than php. need to double check and test
// TODO(go,3) we should move that hallucination to the compositing/expansion phase, and use plain old model getters here
oldIndexes, err := getTableIndexes(oldSchema, oldTable)
lib.GlobalDBSteward.FatalIfError(err, "While finding old indexes")
if err != nil {
return nil, fmt.Errorf("while finding old indexes: %w", err)
}
for _, oldIndex := range oldIndexes {
newIndex, err := tryGetTableIndexNamed(newSchema, newTable, oldIndex.Name)
lib.GlobalDBSteward.FatalIfError(err, "While finding new index corresponding to old")
if err != nil {
return nil, fmt.Errorf("while finding new index corresponding to old: %w", err)
}
if newIndex == nil || !oldIndex.Equals(newIndex, ir.SqlFormatPgsql8) {
out = append(out, oldIndex)
}
}
}

return out
return out, nil
}

func getNewIndexes(oldSchema *ir.Schema, oldTable *ir.Table, newSchema *ir.Schema, newTable *ir.Table) []*ir.Index {
Expand Down
4 changes: 3 additions & 1 deletion lib/format/pgsql8/diff_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,9 @@ func diffClustersTable(ofs output.OutputFileSegmenter, oldTable *ir.Table, newSc
func diffData(l *slog.Logger, ofs output.OutputFileSegmenter, oldSchema, newSchema *ir.Schema) error {
for _, newTable := range newSchema.Tables {
isRenamed, err := lib.GlobalDBSteward.OldDatabase.IsRenamedTable(slog.Default(), newSchema, newTable)
lib.GlobalDBSteward.FatalIfError(err, "while diffing data")
if err != nil {
return fmt.Errorf("while diffing data: %w", err)
}
if isRenamed {
// if the table was renamed, get old definition pointers, diff that
oldSchema := lib.GlobalDBSteward.OldDatabase.GetOldTableSchema(newSchema, newTable)
Expand Down
5 changes: 4 additions & 1 deletion lib/format/pgsql8/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,10 @@ func (ops *Operations) buildSchema(doc *ir.Definition, ofs output.OutputFileSegm
ofs.WriteSql(s...)

// table indexes
diffIndexesTable(ofs, nil, nil, schema, table)
err = diffIndexesTable(ofs, nil, nil, schema, table)
if err != nil {
return err
}

// table grants
for _, grant := range table.Grants {
Expand Down

0 comments on commit e5fe1f9

Please sign in to comment.