Skip to content

Commit

Permalink
Extracted reader options to builder from private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarvardt committed Apr 2, 2020
1 parent f81f7a5 commit d913626
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 49 deletions.
35 changes: 8 additions & 27 deletions pkg/dumper/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package engine
import (
"sync"

wErrors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/hellofresh/klepto/pkg/config"
"github.com/hellofresh/klepto/pkg/database"
"github.com/hellofresh/klepto/pkg/dumper"
"github.com/hellofresh/klepto/pkg/reader"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

type (
Expand Down Expand Up @@ -58,11 +59,11 @@ func (e *Engine) readAndDumpStructure() error {
log.Debug("dumping structure...")
sql, err := e.reader.GetStructure()
if err != nil {
return errors.Wrap(err, "failed to get structure")
return wErrors.Wrap(err, "failed to get structure")
}

if err := e.DumpStructure(sql); err != nil {
return errors.Wrap(err, "failed to dump structure")
return wErrors.Wrap(err, "failed to dump structure")
}

log.Debug("structure was dumped")
Expand All @@ -72,13 +73,13 @@ func (e *Engine) readAndDumpStructure() error {
func (e *Engine) readAndDumpTables(done chan<- struct{}, spec *config.Spec, concurrency int) error {
tables, err := e.reader.GetTables()
if err != nil {
return errors.Wrap(err, "failed to read and dump tables")
return wErrors.Wrap(err, "failed to read and dump tables")
}

// Trigger pre dump tables
if adv, ok := e.Dumper.(Hooker); ok {
if err := adv.PreDumpTables(tables); err != nil {
return errors.Wrap(err, "failed to execute pre dump tables")
return wErrors.Wrap(err, "failed to execute pre dump tables")
}
}

Expand All @@ -98,12 +99,7 @@ func (e *Engine) readAndDumpTables(done chan<- struct{}, spec *config.Spec, conc
continue
}

opts = reader.ReadTableOpt{
Match: tableConfig.Filter.Match,
Sorts: tableConfig.Filter.Sorts,
Limit: tableConfig.Filter.Limit,
Relationships: e.relationshipConfigToOptions(tableConfig.Relationships),
}
opts = reader.NewReadTableOpt(tableConfig)
}

// Create read/write chanel
Expand Down Expand Up @@ -144,18 +140,3 @@ func (e *Engine) readAndDumpTables(done chan<- struct{}, spec *config.Spec, conc

return nil
}

func (e *Engine) relationshipConfigToOptions(relationshipsConfig []*config.Relationship) []*reader.RelationshipOpt {
var opts []*reader.RelationshipOpt

for _, r := range relationshipsConfig {
opts = append(opts, &reader.RelationshipOpt{
Table: r.Table,
ReferencedTable: r.ReferencedTable,
ReferencedKey: r.ReferencedKey,
ForeignKey: r.ForeignKey,
})
}

return opts
}
25 changes: 4 additions & 21 deletions pkg/dumper/query/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ func (d *textDumper) Dump(done chan<- struct{}, spec *config.Spec, concurrency i
for _, tbl := range tables {
var opts reader.ReadTableOpt

table := spec.Tables.FindByName(tbl)
if table == nil {
tableConfig := spec.Tables.FindByName(tbl)
if tableConfig == nil {
log.WithField("table", tbl).Debug("no configuration found for table")
}

if table != nil {
opts = reader.ReadTableOpt{
Limit: table.Filter.Limit,
Relationships: d.relationshipConfigToOptions(table.Relationships),
}
if tableConfig != nil {
opts = reader.NewReadTableOpt(tableConfig)
}

// Create read/write chanel
Expand Down Expand Up @@ -158,17 +155,3 @@ func (d *textDumper) toSQLStringValue(src interface{}) (string, error) {

return "", nil
}

func (d *textDumper) relationshipConfigToOptions(relationshipsConfig []*config.Relationship) []*reader.RelationshipOpt {
var opts []*reader.RelationshipOpt

for _, r := range relationshipsConfig {
opts = append(opts, &reader.RelationshipOpt{
ReferencedTable: r.ReferencedTable,
ReferencedKey: r.ReferencedKey,
ForeignKey: r.ForeignKey,
})
}

return opts
}
23 changes: 22 additions & 1 deletion pkg/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,28 @@ type (
}
)

// Connect acts as fectory method that returns a reader from a DSN
// NewReadTableOpt builds read table options from table config
func NewReadTableOpt(tableCfg *config.Table) ReadTableOpt {
var rOpts []*RelationshipOpt

for _, r := range tableCfg.Relationships {
rOpts = append(rOpts, &RelationshipOpt{
Table: r.Table,
ReferencedTable: r.ReferencedTable,
ReferencedKey: r.ReferencedKey,
ForeignKey: r.ForeignKey,
})
}

return ReadTableOpt{
Match: tableCfg.Filter.Match,
Sorts: tableCfg.Filter.Sorts,
Limit: tableCfg.Filter.Limit,
Relationships: rOpts,
}
}

// Connect acts as factory method that returns a reader from a DSN
func Connect(opts ConnOpts) (reader Reader, err error) {
drivers.Range(func(key, value interface{}) bool {
driver, ok := value.(Driver)
Expand Down
47 changes: 47 additions & 0 deletions pkg/reader/reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package reader

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hellofresh/klepto/pkg/config"
)

func TestNewReadTableOpt(t *testing.T) {
tableCfg := &config.Table{
Filter: config.Filter{
Match: "foo-match",
Limit: 123,
Sorts: map[string]string{"foo": "asc", "bar": "desc"},
},
Relationships: []*config.Relationship{
{
Table: "r1-table",
ForeignKey: "r1-fk",
ReferencedTable: "r1-reference-table",
ReferencedKey: "r1-reference-key",
}, {
Table: "r2-table",
ForeignKey: "r2-fk",
ReferencedTable: "r2-reference-table",
ReferencedKey: "r2-reference-key",
},
},
}

tableOpt := NewReadTableOpt(tableCfg)

assert.Equal(t, tableCfg.Filter.Match, tableOpt.Match)
assert.Equal(t, tableCfg.Filter.Limit, tableOpt.Limit)
assert.Equal(t, tableCfg.Filter.Sorts, tableOpt.Sorts)

require.Equal(t, len(tableCfg.Relationships), len(tableOpt.Relationships))
for i := range tableCfg.Relationships {
assert.Equal(t, tableCfg.Relationships[i].Table, tableOpt.Relationships[i].Table)
assert.Equal(t, tableCfg.Relationships[i].ForeignKey, tableOpt.Relationships[i].ForeignKey)
assert.Equal(t, tableCfg.Relationships[i].ReferencedTable, tableOpt.Relationships[i].ReferencedTable)
assert.Equal(t, tableCfg.Relationships[i].ReferencedKey, tableOpt.Relationships[i].ReferencedKey)
}
}

0 comments on commit d913626

Please sign in to comment.