Skip to content

Commit

Permalink
Remove GlobalDiffIndexes, refactor it into standalone functions, and …
Browse files Browse the repository at this point in the history
…some general cleanup of that code
  • Loading branch information
williammoran committed Apr 25, 2024
1 parent 3d00aa3 commit 11c427e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/format/pgsql8/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (self *Diff) updateStructure(stage1 output.OutputFileSegmenter, stage3 outp
lib.GlobalDBSteward.FatalIfError(err, "while creating tables")
err = GlobalDiffTables.DiffTables(stage1, stage3, oldSchema, newSchema)
lib.GlobalDBSteward.FatalIfError(err, "while diffing tables")
GlobalDiffIndexes.DiffIndexes(stage1, oldSchema, newSchema)
diffIndexes(stage1, oldSchema, newSchema)
GlobalDiffTables.DiffClusters(stage1, oldSchema, newSchema)
createConstraints(stage1, oldSchema, newSchema, sql99.ConstraintTypePrimaryKey)
GlobalDiffTriggers.DiffTriggers(stage1, oldSchema, newSchema)
Expand Down Expand Up @@ -256,7 +256,7 @@ func (self *Diff) updateStructure(stage1 output.OutputFileSegmenter, stage3 outp
lib.GlobalDBSteward.FatalIfError(err, "while creating table %s.%s", newSchema.Name, newTable.Name)
err = GlobalDiffTables.DiffTable(stage1, stage3, oldSchema, oldTable, newSchema, newTable)
lib.GlobalDBSteward.FatalIfError(err, "while diffing table %s.%s", newSchema.Name, newTable.Name)
GlobalDiffIndexes.DiffIndexesTable(stage1, oldSchema, oldTable, newSchema, newTable)
diffIndexesTable(stage1, oldSchema, oldTable, newSchema, newTable)
GlobalDiffTables.DiffClustersTable(stage1, oldSchema, oldTable, newSchema, newTable)
createConstraintsTable(stage1, oldSchema, oldTable, newSchema, newTable, sql99.ConstraintTypePrimaryKey)
GlobalDiffTriggers.DiffTriggersTable(stage1, oldSchema, oldTable, newSchema, newTable)
Expand Down
21 changes: 7 additions & 14 deletions lib/format/pgsql8/diff_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,30 @@ import (
"github.com/dbsteward/dbsteward/lib/output"
)

type DiffIndexes struct {
}

func NewDiffIndexes() *DiffIndexes {
return &DiffIndexes{}
}

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

func (self *DiffIndexes) DiffIndexesTable(ofs output.OutputFileSegmenter, oldSchema *ir.Schema, oldTable *ir.Table, newSchema *ir.Schema, newTable *ir.Table) {
for _, oldIndex := range self.getOldIndexes(oldSchema, oldTable, newSchema, newTable) {
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) {
// 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)...)
}

// TODO(go,pgsql) old code used a different codepath if oldSchema = nil; need to verify this behavior change
for _, newIndex := range self.getNewIndexes(oldSchema, oldTable, newSchema, newTable) {
for _, newIndex := range getNewIndexes(oldSchema, oldTable, newSchema, newTable) {
ofs.WriteSql(getCreateIndexSql(newSchema, newTable, newIndex)...)
}
}

func (self *DiffIndexes) 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 {
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 @@ -62,7 +55,7 @@ func (self *DiffIndexes) getOldIndexes(oldSchema *ir.Schema, oldTable *ir.Table,
return out
}

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

// if new table is nil, there _are_ no indexes to create
Expand Down
2 changes: 1 addition & 1 deletion lib/format/pgsql8/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ func buildSchema(doc *ir.Definition, ofs output.OutputFileSegmenter, tableDep []
ofs.WriteSql(getCreateTableSql(schema, table)...)

// table indexes
GlobalDiffIndexes.DiffIndexesTable(ofs, nil, nil, schema, table)
diffIndexesTable(ofs, nil, nil, schema, table)

// table grants
for _, grant := range table.Grants {
Expand Down
1 change: 0 additions & 1 deletion lib/format/pgsql8/pgsql8.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "github.com/dbsteward/dbsteward/lib/format"

var GlobalOperations = NewOperations()
var GlobalSchema = NewSchema()
var GlobalDiffIndexes = NewDiffIndexes()
var GlobalDiffLanguages = NewDiffLanguages()
var GlobalDiffSequences = NewDiffSequences()
var GlobalDiffTables = NewDiffTables()
Expand Down

0 comments on commit 11c427e

Please sign in to comment.