Skip to content

Commit

Permalink
disentangle library dependencies by moving to a feature registration …
Browse files Browse the repository at this point in the history
…design
  • Loading branch information
williammoran committed May 10, 2024
1 parent f5e4477 commit 7058e81
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 122 deletions.
59 changes: 23 additions & 36 deletions lib/dbsteward.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,6 @@ import (
"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
Expand All @@ -59,7 +38,6 @@ const ApiVersion = "1.4"
type DBSteward struct {
logger zerolog.Logger
slogLogger *slog.Logger
lookupMap LookupMap

SqlFormat ir.SqlFormat

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

func NewDBSteward(lookupMap 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 @@ -136,10 +113,6 @@ func NewDBSteward(lookupMap LookupMap) *DBSteward {
return dbsteward
}

func (dbsteward *DBSteward) Lookup() *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 @@ -582,7 +555,9 @@ func (dbsteward *DBSteward) doBuild(files []string, dataFiles []string, addendum
dbsteward.fatalIfError(err, "saving file")
}

err = dbsteward.Lookup().OperationsConstructor(dbsteward).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 @@ -613,14 +588,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(dbsteward).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(dbsteward).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 @@ -649,13 +628,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(dbsteward).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(dbsteward).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 @@ -668,8 +651,12 @@ func (dbsteward *DBSteward) doSlonikConvert(file string, outputFile string) {
}
}
func (dbsteward *DBSteward) doSlonyCompare(file string) {
dbsteward.lookupMap[ir.SqlFormatPgsql8].OperationsConstructor(dbsteward).(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(dbsteward).(SlonyOperations).SlonyDiff(oldFile, newFile)
ops, err := Format(DefaultSqlFormat)
dbsteward.fatalIfError(err, "loading default format")
ops(dbsteward).(SlonyOperations).SlonyDiff(oldFile, newFile)
}
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
}
8 changes: 2 additions & 6 deletions lib/format/pgsql8/diff_constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ func TestDiffConstraints_DropCreate_ChangePrimaryKeyNameAndTable(t *testing.T) {
newDoc := &ir.Definition{
Schemas: []*ir.Schema{newSchema},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ofs := output.NewSegmenter(defaultQuoter(dbs))
differ := newDiff(NewOperations(dbs).(*Operations), defaultQuoter(dbs))
setOldNewDocs(dbs, differ, oldDoc, newDoc)
Expand Down Expand Up @@ -384,9 +382,7 @@ func diffConstraintsTableCommon(t *testing.T, oldSchema, newSchema *ir.Schema, c
newDoc := &ir.Definition{
Schemas: []*ir.Schema{newSchema},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ofs := output.NewSegmenter(defaultQuoter(dbs))
differ := newDiff(NewOperations(dbs).(*Operations), defaultQuoter(dbs))
setOldNewDocs(dbs, differ, oldDoc, newDoc)
Expand Down
4 changes: 1 addition & 3 deletions lib/format/pgsql8/diff_tables_escape_char_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ func TestDiffTables_GetDataSql_EscapeCharacters(t *testing.T) {
},
},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
dbs.NewDatabase = &ir.Definition{
Schemas: []*ir.Schema{schema},
}
Expand Down
28 changes: 7 additions & 21 deletions lib/format/pgsql8/diff_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func TestDiffTables_DiffTables_ColumnCaseChange(t *testing.T) {
},
}

dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
dbs.IgnoreOldNames = false
ops := NewOperations(dbs).(*Operations)

Expand Down Expand Up @@ -98,9 +96,7 @@ func TestDiffTables_DiffTables_TableOptions_NoChange(t *testing.T) {
},
},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ops := NewOperations(dbs).(*Operations)
ddl1, ddl3 := diffTablesCommon(t, ops, schema, schema)
assert.Empty(t, ddl1)
Expand Down Expand Up @@ -133,9 +129,7 @@ func TestDiffTables_DiffTables_TableOptions_AddWith(t *testing.T) {
},
},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ops := NewOperations(dbs).(*Operations)
ddl1, ddl3 := diffTablesCommon(t, ops, oldSchema, newSchema)
assert.Equal(t, []output.ToSql{
Expand Down Expand Up @@ -184,9 +178,7 @@ func TestDiffTables_DiffTables_TableOptions_AlterWith(t *testing.T) {
},
},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ops := NewOperations(dbs).(*Operations)
ddl1, ddl3 := diffTablesCommon(t, ops, oldSchema, newSchema)
assert.Equal(t, []output.ToSql{
Expand Down Expand Up @@ -240,9 +232,7 @@ func TestDiffTables_DiffTables_TableOptions_AddTablespaceAlterWith(t *testing.T)
},
},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ops := NewOperations(dbs).(*Operations)
ddl1, ddl3 := diffTablesCommon(t, ops, oldSchema, newSchema)
assert.Equal(t, []output.ToSql{
Expand Down Expand Up @@ -301,9 +291,7 @@ func TestDiffTables_DiffTables_TableOptions_DropTablespace(t *testing.T) {
},
},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ops := NewOperations(dbs).(*Operations)
ddl1, ddl3 := diffTablesCommon(t, ops, oldSchema, newSchema)
assert.Equal(t, []output.ToSql{
Expand Down Expand Up @@ -356,9 +344,7 @@ func TestDiffTables_GetDeleteCreateDataSql_AddSerialColumn(t *testing.T) {
},
},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ops := NewOperations(dbs).(*Operations)
delddl, err := getDeleteDataSql(ops, oldSchema, oldSchema.Tables[0], newSchema, newSchema.Tables[0])
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions lib/format/pgsql8/diff_types_domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,7 @@ func diffTypesForTest(t *testing.T, oldSchema, newSchema *ir.Schema) []output.To
newDoc := &ir.Definition{
Schemas: []*ir.Schema{newSchema},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ops := NewOperations(dbs).(*Operations)
differ := newDiff(ops, defaultQuoter(dbs))
setOldNewDocs(dbs, differ, oldDoc, newDoc)
Expand Down
4 changes: 1 addition & 3 deletions lib/format/pgsql8/diff_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ func TestDiffTypes_DiffTypes_RecreateDependentFunctions(t *testing.T) {
},
}

dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
ops := NewOperations(dbs).(*Operations)
ofs := output.NewAnnotationStrippingSegmenter(defaultQuoter(dbs))
err := diffTypes(dbs, newDiff(ops, defaultQuoter(dbs)), ofs, oldSchema, newSchema)
Expand Down
4 changes: 1 addition & 3 deletions lib/format/pgsql8/diff_views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ func newSingleView() *ir.Definition {
}

func TestCreateViewsOrdered(t *testing.T) {
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
q := defaultQuoter(dbs)
ofs := output.NewAnnotationStrippingSegmenter(q)
err := createViewsOrdered(dbs, ofs, oldSingleView(), newSingleView())
Expand Down
4 changes: 1 addition & 3 deletions lib/format/pgsql8/oneeighty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ func TestOneEighty(t *testing.T) {
}
defer Teardowndb(t, c, "pg")
role := os.Getenv("DB_USER")
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
dbs.SqlFormat = ir.SqlFormatPgsql8
ops := NewOperations(dbs).(*Operations)
statements, err := ops.CreateStatements(ir.FullFeatureSchema(role))
Expand Down
4 changes: 1 addition & 3 deletions lib/format/pgsql8/operations_column_value_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ func getColumnValueDefault(def *ir.Column, data *ir.DataCol) (string, error) {
},
},
}
dbs := lib.NewDBSteward(lib.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
})
dbs := lib.NewDBSteward()
dbs.NewDatabase = doc
schema := doc.Schemas[0]
table := schema.Tables[0]
Expand Down
Loading

0 comments on commit 7058e81

Please sign in to comment.