Skip to content

Commit

Permalink
Merge pull request #949 from planetscale/fix-replica-dump
Browse files Browse the repository at this point in the history
Fix --replica and --rdonly `dump`
  • Loading branch information
mscoutermarsh authored Dec 11, 2024
2 parents e821ae5 + 4762688 commit 6729396
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 597 deletions.
6 changes: 4 additions & 2 deletions internal/cmd/database/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,13 @@ func dump(ch *cmdutil.Helper, cmd *cobra.Command, flags *dumpFlags, args []strin
}

if flags.replica && flags.shard == "" {
cfg.UseReplica = true
useCmd := "USE @replica;"
cfg.SessionVars = append([]string{useCmd}, cfg.SessionVars...)
}

if flags.rdonly && flags.shard == "" {
cfg.UseRdonly = true
useCmd := "USE @rdonly;"
cfg.SessionVars = append([]string{useCmd}, cfg.SessionVars...)
}

if flags.tables != "" {
Expand Down
17 changes: 3 additions & 14 deletions internal/dumper/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ type Config struct {
Allbytes uint64
Allrows uint64
OverwriteTables bool
UseReplica bool
UseRdonly bool
Wheres map[string]string
Selects map[string]map[string]string
Filters map[string]map[string]string
Expand Down Expand Up @@ -170,7 +168,7 @@ func (d *Dumper) Run(ctx context.Context) error {
zap.Int("thread_conn_id", conn.ID),
)

err := d.dumpTable(ctx, conn, database, table, d.cfg.UseReplica, d.cfg.UseRdonly)
err := d.dumpTable(ctx, conn, database, table)
if err != nil {
d.log.Error("error dumping table", zap.Error(err))
}
Expand Down Expand Up @@ -237,21 +235,12 @@ func (d *Dumper) dumpTableSchema(conn *Connection, database string, table string
}

// Dump a table in "MySQL" (multi-inserts) format
func (d *Dumper) dumpTable(ctx context.Context, conn *Connection, database string, table string, useReplica, useRdonly bool) error {
func (d *Dumper) dumpTable(ctx context.Context, conn *Connection, database string, table string) error {
var allBytes uint64
var allRows uint64
var where string
var selfields []string

databaseHandle := database
if useReplica {
databaseHandle += "@replica"
}

if useRdonly {
databaseHandle += "@rdonly"
}

fields := make([]string, 0)
{
flds, err := d.dumpableFieldNames(conn, table)
Expand Down Expand Up @@ -280,7 +269,7 @@ func (d *Dumper) dumpTable(ctx context.Context, conn *Connection, database strin
where = fmt.Sprintf(" WHERE %v", v)
}

cursor, err := conn.StreamFetch(fmt.Sprintf("SELECT %s FROM `%s`.`%s` %s", strings.Join(selfields, ", "), databaseHandle, table, where))
cursor, err := conn.StreamFetch(fmt.Sprintf("SELECT %s FROM `%s`.`%s` %s", strings.Join(selfields, ", "), database, table, where))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 6729396

Please sign in to comment.