From 94a21b2420f7f51c042a146ee4bef077e4434078 Mon Sep 17 00:00:00 2001 From: Jojoo Imbeah Date: Thu, 15 Mar 2018 10:54:38 +0100 Subject: [PATCH 1/2] Improve error message when rollback fails Fix ineffassign for err --- pkg/dumper/postgres/dumper.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/dumper/postgres/dumper.go b/pkg/dumper/postgres/dumper.go index 9e3e273..75a6360 100644 --- a/pkg/dumper/postgres/dumper.go +++ b/pkg/dumper/postgres/dumper.go @@ -49,10 +49,12 @@ func (d *pgDumper) DumpTable(tableName string, rowChan <-chan database.Row) erro insertedRows, err := d.insertIntoTable(txn, tableName, rowChan) if err != nil { err = errors.Wrap(err, "failed to insert rows") - if err := txn.Rollback(); err != nil { - return errors.Wrap(err, "failed to rollback transaction") + if rErr := txn.Rollback(); rErr != nil { + return errors.Wrap(rErr, "failed to insert rows, failed to rollback inserts") } + return err } + log.WithFields(log.Fields{ "table": tableName, "inserted": insertedRows, From 8e70a9628b33c23e7c5dbc8a971e273be3f775ab Mon Sep 17 00:00:00 2001 From: Jojoo Imbeah Date: Thu, 15 Mar 2018 11:06:48 +0100 Subject: [PATCH 2/2] Make small improvements By fixing minor typo. Also, improve error handling when inserting into tables or when we roll back inserts --- pkg/dumper/dumper.go | 2 +- pkg/dumper/mysql/dumper.go | 8 +++++--- pkg/dumper/postgres/dumper.go | 8 +++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/dumper/dumper.go b/pkg/dumper/dumper.go index 75a27f8..c9168ae 100644 --- a/pkg/dumper/dumper.go +++ b/pkg/dumper/dumper.go @@ -18,7 +18,7 @@ type ( NewConnection(ConnOpts, reader.Reader) (Dumper, error) } - // A Dumper writes a database's stucture to the provided stream. + // A Dumper writes a database's structure to the provided stream. Dumper interface { // Dump executes the dump process. Dump(chan<- struct{}, *config.Spec, int) error diff --git a/pkg/dumper/mysql/dumper.go b/pkg/dumper/mysql/dumper.go index de24837..9b4b733 100644 --- a/pkg/dumper/mysql/dumper.go +++ b/pkg/dumper/mysql/dumper.go @@ -54,10 +54,12 @@ func (d *myDumper) DumpTable(tableName string, rowChan <-chan database.Row) erro insertedRows, err := d.insertIntoTable(txn, tableName, rowChan) if err != nil { + defer func() { + if err := txn.Rollback(); err != nil { + log.WithError(err).Error("failed to rollback") + } + }() err = errors.Wrap(err, "failed to insert rows") - if err := txn.Rollback(); err != nil { - return errors.Wrap(err, "failed to rollback transaction") - } return err } diff --git a/pkg/dumper/postgres/dumper.go b/pkg/dumper/postgres/dumper.go index 75a6360..ea11411 100644 --- a/pkg/dumper/postgres/dumper.go +++ b/pkg/dumper/postgres/dumper.go @@ -48,10 +48,12 @@ func (d *pgDumper) DumpTable(tableName string, rowChan <-chan database.Row) erro insertedRows, err := d.insertIntoTable(txn, tableName, rowChan) if err != nil { + defer func() { + if err := txn.Rollback(); err != nil { + log.WithError(err).Error("failed to rollback") + } + }() err = errors.Wrap(err, "failed to insert rows") - if rErr := txn.Rollback(); rErr != nil { - return errors.Wrap(rErr, "failed to insert rows, failed to rollback inserts") - } return err }