Skip to content

Commit

Permalink
Add --data-only to copy data without structure (#140)
Browse files Browse the repository at this point in the history
Allow data-only transfers to be run to extract multiple subsets from the same table.
  • Loading branch information
reidab authored Sep 23, 2022
1 parent 0fd6dd1 commit 17d2104
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
4 changes: 3 additions & 1 deletion cmd/steal.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type (
concurrency int
readOpts connOpts
writeOpts connOpts
dataOnly bool
}
connOpts struct {
timeout time.Duration
Expand Down Expand Up @@ -76,6 +77,7 @@ func NewStealCmd() *cobra.Command {
persistentFlags.DurationVar(&opts.writeOpts.maxConnLifetime, "write-conn-lifetime", 0, "Sets the maximum amount of time a connection may be reused on the write database")
persistentFlags.IntVar(&opts.writeOpts.maxConns, "write-max-conns", 5, "Sets the maximum number of open connections to the write database")
persistentFlags.IntVar(&opts.writeOpts.maxIdleConns, "write-max-idle-conns", 0, "Sets the maximum number of connections in the idle connection pool for the write database")
persistentFlags.BoolVar(&opts.dataOnly, "data-only", false, "Only steal data; requires that the target database structure already exists")

return cmd
}
Expand Down Expand Up @@ -122,7 +124,7 @@ func RunSteal(opts *StealOptions) (err error) {
defer close(done)

start := time.Now()
if err := target.Dump(done, opts.cfgTables, opts.concurrency); err != nil {
if err := target.Dump(done, opts.cfgTables, opts.concurrency, opts.dataOnly); err != nil {
return fmt.Errorf("error while dumping: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion features/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *MysqlTestSuite) TestExample() {

done := make(chan struct{})
defer close(done)
s.Require().NoError(dmp.Dump(done, config.Tables{}, 4), "Failed to dump")
s.Require().NoError(dmp.Dump(done, config.Tables{}, 4, false), "Failed to dump")

<-done

Expand Down
2 changes: 1 addition & 1 deletion features/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *PostgresTestSuite) TestExample() {

done := make(chan struct{})
defer close(done)
s.Require().NoError(dmp.Dump(done, config.Tables{}, 4), "Failed to dump")
s.Require().NoError(dmp.Dump(done, config.Tables{}, 4, false), "Failed to dump")

<-done

Expand Down
2 changes: 1 addition & 1 deletion pkg/dumper/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type (
// A Dumper writes a database's structure to the provided stream.
Dumper interface {
// Dump executes the dump process.
Dump(chan<- struct{}, config.Tables, int) error
Dump(chan<- struct{}, config.Tables, int, bool) error
// Close closes the dumper resources and releases them.
Close() error
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/dumper/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ func New(rdr reader.Reader, dumper Dumper) dumper.Dumper {
}

// Dump executes the dump process.
func (e *Engine) Dump(done chan<- struct{}, cfgTables config.Tables, concurrency int) error {
if err := e.readAndDumpStructure(); err != nil {
return err
func (e *Engine) Dump(done chan<- struct{}, cfgTables config.Tables, concurrency int, dataOnly bool) error {
if !dataOnly {
if err := e.readAndDumpStructure(); err != nil {
return err
}
}

return e.readAndDumpTables(done, cfgTables, concurrency)
Expand Down
16 changes: 9 additions & 7 deletions pkg/dumper/query/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ func NewDumper(output io.Writer, rdr reader.Reader) dumper.Dumper {
}

// Dump executes the dump stream process.
func (d *textDumper) Dump(done chan<- struct{}, cfgTables config.Tables, concurrency int) error {
func (d *textDumper) Dump(done chan<- struct{}, cfgTables config.Tables, concurrency int, dataOnly bool) error {
tables, err := d.reader.GetTables()
if err != nil {
return fmt.Errorf("failed to get tables: %w", err)
}

structure, err := d.reader.GetStructure()
if err != nil {
return fmt.Errorf("could not get database structure: %w", err)
}
if _, err := io.WriteString(d.output, structure); err != nil {
return fmt.Errorf("could not write structure to output: %w", err)
if !dataOnly {
structure, err := d.reader.GetStructure()
if err != nil {
return fmt.Errorf("could not get database structure: %w", err)
}
if _, err := io.WriteString(d.output, structure); err != nil {
return fmt.Errorf("could not write structure to output: %w", err)
}
}

var wg sync.WaitGroup
Expand Down

0 comments on commit 17d2104

Please sign in to comment.