load

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyGTIDPurged

func ApplyGTIDPurged(ctx context.Context, db *sql.DB, dumpGTIDSet string, force bool) error

ApplyGTIDPurged seeds the target server's GTID history from the dump's captured gtid_set so the target can replicate from the dump source with SOURCE_AUTO_POSITION=1.

Safety gates, in order:

  • the dump GTID set must be well-formed (it is embedded in SQL);
  • gtid_mode must be ON on the target;
  • a running replication channel always aborts; a configured-but-stopped channel requires force;
  • if the target's gtid_executed is non-empty, it must be a subset of the dump's set (anything extra means transactions the dump source never had — errant; use go-gtids to inspect). A subset still requires force, because clearing it takes RESET MASTER / RESET BINARY LOGS AND GTIDS, which destroys the target's binlog history.

The cheap path — empty gtid_executed, e.g. a fresh instance loaded with --skip-binlog — needs no reset and no force.

func BuildReplicationSQL added in v0.3.0

func BuildReplicationSQL(info ReplicationInfo) (string, error)

BuildReplicationSQL renders ready-to-run replication setup statements from the dump's recorded coordinates. Nothing is executed — the output is for the operator to review and run manually (or pipe into the mysql client).

When the dump carries a GTID set, the GTID AUTO_POSITION variant is emitted as the runnable block and file/position as a commented alternative; without a GTID set, file/position is the runnable block. The MySQL 5.7 CHANGE MASTER syntax is always included as a commented block.

func StartReplication added in v0.3.0

func StartReplication(ctx context.Context, db *sql.DB, info ReplicationInfo, opts StartOptions) error

StartReplication configures the target as a replica of the dump source and starts it, then waits for both threads to come up healthy.

Safety gates, matching ApplyGTIDPurged:

  • a RUNNING replication channel always aborts;
  • a configured-but-stopped channel requires force;
  • in GTID mode, the target's gtid_executed must equal the dump's GTID set (seed it with --set-gtid-purged) — anything less re-fetches rows the load already inserted, anything more is errant.

Types

type FileLoad

type FileLoad struct {
	Path     string
	IsSchema bool
	IsPost   bool // trigger/routine/event file — loaded serially after all data
}

FileLoad represents a single SQL file to load.

type Importer

type Importer struct {
	// contains filtered or unexported fields
}

Importer loads SQL files produced by go-dump into a MySQL server.

func New

func New(db *sql.DB, workers int, skipSchema bool) *Importer

New creates an Importer. Call Close() when done.

func (*Importer) Close

func (imp *Importer) Close() error

Close releases the underlying database connection pool.

func (*Importer) ImportDirectory

func (imp *Importer) ImportDirectory(ctx context.Context, dir, pattern string, ls *LoadState) error

ImportDirectory loads all SQL files in dir matching pattern. Schema (definition) files are loaded serially first; data files run in parallel. Pass a non-nil LoadState to enable resume (skip already-loaded files).

func (*Importer) ImportFile

func (imp *Importer) ImportFile(ctx context.Context, path string) error

ImportFile loads a single SQL file.

func (*Importer) SetSkipBinlog

func (imp *Importer) SetSkipBinlog(v bool)

SetSkipBinlog makes every load connection run SET SQL_LOG_BIN=0, so the restore is written to the target only — not to its binlog, its replicas, or its GTID history. Requires SUPER or SYSTEM_VARIABLES_ADMIN on the target.

type LoadState

type LoadState struct {
	StartTime      time.Time        `json:"start_time"`
	CompletedFiles []string         `json:"completed_files"`
	FileProgress   map[string]int64 `json:"file_progress,omitempty"`
	// contains filtered or unexported fields
}

LoadState persists which files have been successfully loaded, enabling --resume. Written atomically to <directory>/load-state.json after each successful file load. FileProgress tracks partially-loaded data files by the number of statements committed so far, so an interrupted load resumes mid-file instead of replaying rows that are already in the target.

func NewLoadState

func NewLoadState(dir string) (*LoadState, error)

NewLoadState reads an existing load-state.json from dir, or returns a fresh state.

func (*LoadState) HasFile

func (ls *LoadState) HasFile(name string) bool

HasFile reports whether name was already successfully loaded.

func (*LoadState) Len

func (ls *LoadState) Len() int

Len returns the number of files recorded as completed.

func (*LoadState) Mark

func (ls *LoadState) Mark(name string) error

Mark records name as complete and flushes the state file atomically. Any partial progress entry for name is dropped — completed files are tracked by CompletedFiles alone.

func (*LoadState) Progress added in v0.2.0

func (ls *LoadState) Progress(name string) int64

Progress returns the number of statements already committed for a partially-loaded file, or 0 when the file has not been started.

func (*LoadState) SetProgress added in v0.2.0

func (ls *LoadState) SetProgress(name string, n int64) error

SetProgress records that n statements of name have been committed and flushes the state file atomically.

type ReplicationInfo added in v0.3.0

type ReplicationInfo struct {
	SourceHost   string
	SourcePort   int
	ReplUser     string // "<repl_user>" placeholder when empty
	ReplPassword string // "<repl_password>" placeholder when empty
	BinlogFile   string
	BinlogPos    int
	GTIDSet      string // raw from metadata.json; sanitised and validated here
}

ReplicationInfo carries everything needed to point a replica seeded from a dump at the dump's source server. Coordinates come from the dump's metadata.json; host, port, user, and password may be overridden by flags when the replica reaches the source by a different address or account.

type StartOptions added in v0.3.0

type StartOptions struct {
	Mode               string // "auto", "gtid", or "file-pos"
	SourceSSL          bool   // add SOURCE_SSL = 1 (encrypt the replication connection)
	GetSourcePublicKey bool   // add GET_SOURCE_PUBLIC_KEY = 1 (caching_sha2_password without TLS)
	Force              bool   // allow acting on a configured-but-stopped channel
}

StartOptions controls StartReplication.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL