Skip to content

Commit

Permalink
Refactor global DBSteward variable to passed variable
Browse files Browse the repository at this point in the history
  • Loading branch information
williammoran committed May 10, 2024
1 parent 2998703 commit 7a85471
Show file tree
Hide file tree
Showing 40 changed files with 511 additions and 558 deletions.
60 changes: 44 additions & 16 deletions lib/dbsteward.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,56 @@ import (

"github.com/dbsteward/dbsteward/lib/config"
"github.com/dbsteward/dbsteward/lib/encoding/xml"
"github.com/dbsteward/dbsteward/lib/format"
"github.com/dbsteward/dbsteward/lib/ir"
"github.com/dbsteward/dbsteward/lib/output"
"github.com/dbsteward/dbsteward/lib/util"
"github.com/hashicorp/go-multierror"

"github.com/alexflint/go-arg"
"github.com/rs/zerolog"
)

type LookupMap map[ir.SqlFormat]*Lookup

type Lookup struct {
Schema Schema
OperationsConstructor func(*DBSteward) Operations
}

type Operations interface {
Build(outputPrefix string, dbDoc *ir.Definition) error
BuildUpgrade(
oldOutputPrefix, oldCompositeFile string, oldDbDoc *ir.Definition, oldFiles []string,
newOutputPrefix, newCompositeFile string, newDbDoc *ir.Definition, newFiles []string,
) error
ExtractSchema(host string, port uint, name, user, pass string) (*ir.Definition, error)
CompareDbData(dbDoc *ir.Definition, host string, port uint, name, user, pass string) (*ir.Definition, error)
SqlDiff(old, new []string, outputFile string)

GetQuoter() output.Quoter
//SetConfig(*config.Args)
}

type Schema interface {
GetCreationSql(*DBSteward, *ir.Schema) ([]output.ToSql, error)
GetDropSql(*ir.Schema) []output.ToSql
}

type SlonyOperations interface {
SlonyCompare(file string)
SlonyDiff(oldFile, newFile string)
}

// NOTE: 2.0.0 is the intended golang release. 3.0.0 is the intended refactor/modernization
var Version = "2.0.0"
const Version = "2.0.0"

// NOTE: we're attempting to maintain "api" compat with legacy dbsteward for now
var ApiVersion = "1.4"

// TODO(go,3) no globals
var GlobalDBSteward *DBSteward
const ApiVersion = "1.4"

type DBSteward struct {
logger zerolog.Logger
slogLogger *slog.Logger
lookupMap format.LookupMap
lookupMap LookupMap

SqlFormat ir.SqlFormat

Expand Down Expand Up @@ -67,7 +95,7 @@ type DBSteward struct {
NewDatabase *ir.Definition
}

func NewDBSteward(lookupMap format.LookupMap) *DBSteward {
func NewDBSteward(lookupMap LookupMap) *DBSteward {
dbsteward := &DBSteward{
logger: zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger(),
lookupMap: lookupMap,
Expand Down Expand Up @@ -108,7 +136,7 @@ func NewDBSteward(lookupMap format.LookupMap) *DBSteward {
return dbsteward
}

func (dbsteward *DBSteward) Lookup() *format.Lookup {
func (dbsteward *DBSteward) Lookup() *Lookup {
return dbsteward.lookupMap[dbsteward.SqlFormat]
}

Expand Down Expand Up @@ -554,7 +582,7 @@ func (dbsteward *DBSteward) doBuild(files []string, dataFiles []string, addendum
dbsteward.fatalIfError(err, "saving file")
}

err = dbsteward.Lookup().OperationsConstructor().Build(outputPrefix, dbDoc)
err = dbsteward.Lookup().OperationsConstructor(dbsteward).Build(outputPrefix, dbDoc)
dbsteward.fatalIfError(err, "building")
}
func (dbsteward *DBSteward) doDiff(oldFiles []string, newFiles []string, dataFiles []string) {
Expand Down Expand Up @@ -585,14 +613,14 @@ func (dbsteward *DBSteward) doDiff(oldFiles []string, newFiles []string, dataFil
err = xml.SaveDefinition(dbsteward.Logger(), newCompositeFile, newDbDoc)
dbsteward.fatalIfError(err, "saving file")

err = dbsteward.Lookup().OperationsConstructor().BuildUpgrade(
err = dbsteward.Lookup().OperationsConstructor(dbsteward).BuildUpgrade(
oldOutputPrefix, oldCompositeFile, oldDbDoc, oldFiles,
newOutputPrefix, newCompositeFile, newDbDoc, newFiles,
)
dbsteward.fatalIfError(err, "building upgrade")
}
func (dbsteward *DBSteward) doExtract(dbHost string, dbPort uint, dbName, dbUser, dbPass string, outputFile string) {
output, err := dbsteward.Lookup().OperationsConstructor().ExtractSchema(dbHost, dbPort, dbName, dbUser, dbPass)
output, err := dbsteward.Lookup().OperationsConstructor(dbsteward).ExtractSchema(dbHost, dbPort, dbName, dbUser, dbPass)
dbsteward.fatalIfError(err, "extracting")
dbsteward.Info("Saving extracted database schema to %s", outputFile)
err = xml.SaveDefinition(dbsteward.Logger(), outputFile, output)
Expand Down Expand Up @@ -621,13 +649,13 @@ func (dbsteward *DBSteward) doDbDataDiff(files []string, dataFiles []string, add
err = xml.SaveDefinition(dbsteward.Logger(), compositeFile, dbDoc)
dbsteward.fatalIfError(err, "saving file")

output, err := dbsteward.Lookup().OperationsConstructor().CompareDbData(dbDoc, dbHost, dbPort, dbName, dbUser, dbPass)
output, err := dbsteward.Lookup().OperationsConstructor(dbsteward).CompareDbData(dbDoc, dbHost, dbPort, dbName, dbUser, dbPass)
dbsteward.fatalIfError(err, "comparing data")
err = xml.SaveDefinition(dbsteward.Logger(), compositeFile, output)
dbsteward.fatalIfError(err, "saving file")
}
func (dbsteward *DBSteward) doSqlDiff(oldSql, newSql []string, outputFile string) {
dbsteward.Lookup().OperationsConstructor().SqlDiff(oldSql, newSql, outputFile)
dbsteward.Lookup().OperationsConstructor(dbsteward).SqlDiff(oldSql, newSql, outputFile)
}
func (dbsteward *DBSteward) doSlonikConvert(file string, outputFile string) {
// TODO(go,nth) is there a nicer way to handle this output idiom?
Expand All @@ -640,8 +668,8 @@ func (dbsteward *DBSteward) doSlonikConvert(file string, outputFile string) {
}
}
func (dbsteward *DBSteward) doSlonyCompare(file string) {
dbsteward.lookupMap[ir.SqlFormatPgsql8].OperationsConstructor().(format.SlonyOperations).SlonyCompare(file)
dbsteward.lookupMap[ir.SqlFormatPgsql8].OperationsConstructor(dbsteward).(SlonyOperations).SlonyCompare(file)
}
func (dbsteward *DBSteward) doSlonyDiff(oldFile string, newFile string) {
dbsteward.lookupMap[ir.SqlFormatPgsql8].OperationsConstructor().(format.SlonyOperations).SlonyDiff(oldFile, newFile)
dbsteward.lookupMap[ir.SqlFormatPgsql8].OperationsConstructor(dbsteward).(SlonyOperations).SlonyDiff(oldFile, newFile)
}
24 changes: 0 additions & 24 deletions lib/dbsteward_main_test.go

This file was deleted.

44 changes: 0 additions & 44 deletions lib/format/interface.go

This file was deleted.

10 changes: 0 additions & 10 deletions lib/format/lookup.go

This file was deleted.

10 changes: 5 additions & 5 deletions lib/format/pgsql8/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ func getTableContraintCreationSql(constraint *sql99.TableConstraint) []output.To
return nil
}

func constraintDependsOnRenamedTable(l *slog.Logger, doc *ir.Definition, constraint *sql99.TableConstraint) (bool, error) {
if lib.GlobalDBSteward.IgnoreOldNames {
func constraintDependsOnRenamedTable(dbs *lib.DBSteward, doc *ir.Definition, constraint *sql99.TableConstraint) (bool, error) {
if dbs.IgnoreOldNames {
return false, nil
}

Expand All @@ -294,16 +294,16 @@ func constraintDependsOnRenamedTable(l *slog.Logger, doc *ir.Definition, constra
if refTable == nil {
return false, nil
}
isRenamed := lib.GlobalDBSteward.IgnoreOldNames
isRenamed := dbs.IgnoreOldNames
if !isRenamed {
var err error
isRenamed, err = lib.GlobalDBSteward.OldDatabase.IsRenamedTable(slog.Default(), refSchema, refTable)
isRenamed, err = dbs.OldDatabase.IsRenamedTable(slog.Default(), refSchema, refTable)
if err != nil {
return false, fmt.Errorf("while checking if constraint depends on renamed table: %w", err)
}
}
if isRenamed {
l.Info(fmt.Sprintf("Constraint %s.%s.%s references renamed table %s.%s", constraint.Schema.Name, constraint.Table.Name, constraint.Name, refSchema.Name, refTable.Name))
dbs.Logger().Info(fmt.Sprintf("Constraint %s.%s.%s references renamed table %s.%s", constraint.Schema.Name, constraint.Table.Name, constraint.Name, refSchema.Name, refTable.Name))
return true, nil
}
return false, nil
Expand Down
Loading

0 comments on commit 7a85471

Please sign in to comment.