Skip to content

Commit

Permalink
Remove all global variables and disentangle library dependendies by u…
Browse files Browse the repository at this point in the history
…sing a registration pattern
  • Loading branch information
williammoran committed May 10, 2024
1 parent 2998703 commit df1df48
Show file tree
Hide file tree
Showing 42 changed files with 505 additions and 579 deletions.
51 changes: 30 additions & 21 deletions lib/dbsteward.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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/util"
"github.com/hashicorp/go-multierror"
Expand All @@ -19,19 +18,20 @@ import (
"github.com/rs/zerolog"
)

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

SqlFormat ir.SqlFormat

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

func NewDBSteward(lookupMap format.LookupMap) *DBSteward {
func NewDBSteward() *DBSteward {
dbsteward := &DBSteward{
logger: zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger(),
lookupMap: lookupMap,
logger: zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger(),

SqlFormat: ir.SqlFormatUnknown,

Expand Down Expand Up @@ -108,10 +107,6 @@ func NewDBSteward(lookupMap format.LookupMap) *DBSteward {
return dbsteward
}

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

// correlates to dbsteward->arg_parse()
func (dbsteward *DBSteward) ArgParse() {
// TODO(go,nth): deck this out with better go-arg config
Expand Down Expand Up @@ -554,7 +549,9 @@ func (dbsteward *DBSteward) doBuild(files []string, dataFiles []string, addendum
dbsteward.fatalIfError(err, "saving file")
}

err = dbsteward.Lookup().OperationsConstructor().Build(outputPrefix, dbDoc)
ops, err := Format(DefaultSqlFormat)
dbsteward.fatalIfError(err, "loading default format")
err = ops(dbsteward).Build(outputPrefix, dbDoc)
dbsteward.fatalIfError(err, "building")
}
func (dbsteward *DBSteward) doDiff(oldFiles []string, newFiles []string, dataFiles []string) {
Expand Down Expand Up @@ -585,14 +582,18 @@ 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(
ops, err := Format(DefaultSqlFormat)
dbsteward.fatalIfError(err, "loading default format")
err = ops(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)
ops, err := Format(DefaultSqlFormat)
dbsteward.fatalIfError(err, "loading default format")
output, err := ops(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 +622,17 @@ 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)
ops, err := Format(DefaultSqlFormat)
dbsteward.fatalIfError(err, "loading default format")
output, err := ops(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)
ops, err := Format(DefaultSqlFormat)
dbsteward.fatalIfError(err, "loading default format")
ops(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 +645,12 @@ func (dbsteward *DBSteward) doSlonikConvert(file string, outputFile string) {
}
}
func (dbsteward *DBSteward) doSlonyCompare(file string) {
dbsteward.lookupMap[ir.SqlFormatPgsql8].OperationsConstructor().(format.SlonyOperations).SlonyCompare(file)
ops, err := Format(DefaultSqlFormat)
dbsteward.fatalIfError(err, "loading default format")
ops(dbsteward).(SlonyOperations).SlonyCompare(file)
}
func (dbsteward *DBSteward) doSlonyDiff(oldFile string, newFile string) {
dbsteward.lookupMap[ir.SqlFormatPgsql8].OperationsConstructor().(format.SlonyOperations).SlonyDiff(oldFile, newFile)
ops, err := Format(DefaultSqlFormat)
dbsteward.fatalIfError(err, "loading default format")
ops(dbsteward).(SlonyOperations).SlonyDiff(oldFile, newFile)
}
24 changes: 0 additions & 24 deletions lib/dbsteward_main_test.go

This file was deleted.

45 changes: 45 additions & 0 deletions lib/extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lib

import (
"fmt"
"sync"

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

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
}

type Encoding interface {
}

var formats = make(map[ir.SqlFormat]func(*DBSteward) Operations)

var formatMutex sync.Mutex

func RegisterFormat(id ir.SqlFormat, constructor func(*DBSteward) Operations) {
formatMutex.Lock()
defer formatMutex.Unlock()
formats[id] = constructor
}

func Format(id ir.SqlFormat) (func(*DBSteward) Operations, error) {
formatMutex.Lock()
defer formatMutex.Unlock()
constructor, exists := formats[id]
if !exists {
return nil, fmt.Errorf("no such format as %s", id)
}
return constructor, nil
}
1 change: 0 additions & 1 deletion lib/format/constants.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 df1df48

Please sign in to comment.