io

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const CSVExtension = ".csv"

CSVExtension is the suffix appended to an export path that has none.

View Source
const DefaultBatchSize = 20000

DefaultBatchSize is the number of rows read or written per batch.

It is deliberately much larger than the historical value of 500. Export used to slice the table with LIMIT/OFFSET, where a small batch meant thousands of round trips each rescanning the table from the start; that pattern is gone, but a small batch is still costly because every batch is a separate query. Measured on 5,000,000 rows: batch 500 took 15.3s, batch 20000 took 7.5s, with a server-side COPY at 5.3s. 20000 rows is a few megabytes of CSV, so memory stays flat while the per-batch overhead shrinks to something negligible.

Variables

This section is empty.

Functions

func CSVOutputPath added in v1.2.1

func CSVOutputPath(path string) string

CSVOutputPath returns the path an export will actually write.

Export appends ".csv" when the caller omits an extension. Callers that check whether they are about to overwrite a file must resolve the path the same way, otherwise "pgtransfer export csv prod users users" checks for a file called "users" and silently writes "users.csv" over an existing one.

func CountCSVDataRows

func CountCSVDataRows(path string, hasHeader bool) int

CountCSVDataRows best-effort counts records in a CSV file, optionally excluding the header row.

This counts records rather than lines. A CSV record can span several lines when a quoted field contains a newline, so counting newline bytes overstates the row count for any table with multiline text in it.

func DumpDatabase

func DumpDatabase(dbURL, dumpPath string) error

func DumpDatabaseWithConnection

func DumpDatabaseWithConnection(profile config.Profile, dumpPath string) error

DumpDatabaseWithConnection performs a database dump using a DBConnection (supports SSH tunnels)

func DumpDatabaseWithConnectionAndOptions

func DumpDatabaseWithConnectionAndOptions(profile config.Profile, dumpPath string, options *DumpOptions) error

DumpDatabaseWithConnectionAndOptions performs a database dump with advanced options using DBConnection

func DumpDatabaseWithOptions

func DumpDatabaseWithOptions(dbURL, dumpPath string, options *DumpOptions) error

DumpDatabaseWithOptions performs a database dump with advanced options

func ExportCSV

func ExportCSV(db *sql.DB, table, exportPath string) error

ExportCSV exports a PostgreSQL table to a CSV file with a live progress bar.

Kept for callers that only need the defaults; it shares the implementation with ExportCSVWithOptions so the two cannot diverge.

func ExportCSVWithOptions

func ExportCSVWithOptions(db *sql.DB, table, exportPath string, options *CSVOptions) error

ExportCSVWithOptions exports a PostgreSQL table to CSV with batch processing for better memory efficiency

func ExportCSVWithQuery

func ExportCSVWithQuery(db *sql.DB, query, exportPath string, includeHeaders bool) (int, error)

ExportCSVWithQuery runs an arbitrary SELECT and writes the results to a CSV file. It returns the number of data rows written (headers excluded).

func FormatCSVValue

func FormatCSVValue(v interface{}) string

formatCSVValue properly formats a value for CSV export

func ImportCSV

func ImportCSV(db *sql.DB, table, importPath string) error

ImportCSV imports data from a CSV file into a PostgreSQL table.

func ImportCSVWithOptions

func ImportCSVWithOptions(db *sql.DB, table, importPath string, options *CSVOptions) error

ImportCSVWithOptions imports data from a CSV file with batch processing for better performance

func MigrateDatabaseWithConnection

func MigrateDatabaseWithConnection(opts *MigrationOptions) error

MigrateDatabaseWithConnection performs database migration using DBConnection objects for SSH support

func MigrateDatabaseWithOptions

func MigrateDatabaseWithOptions(opts *MigrationOptions) error

MigrateDatabaseWithOptions performs database migration with specified options

func QuoteTable added in v1.2.1

func QuoteTable(table string) string

QuoteTable is quoteTable for callers outside this package.

func RestoreDatabase

func RestoreDatabase(dbURL, dumpPath string) error

func RestoreDatabaseSmart

func RestoreDatabaseSmart(dbURL, dumpPath string) error

RestoreDatabaseSmart automatically detects dump format and uses appropriate tool

func RestoreDatabaseSmartWithOptions

func RestoreDatabaseSmartWithOptions(dbURL, dumpPath string, options *RestoreOptions) error

RestoreDatabaseSmartWithOptions detects the dump format and restores it, honouring the supplied RestoreOptions.

func RestoreDatabaseWithConnection

func RestoreDatabaseWithConnection(profile config.Profile, dumpPath string) error

RestoreDatabaseWithConnection performs a database restore using a DBConnection (supports SSH tunnels) RestoreDatabaseWithConnection performs a database restore using a DBConnection (supports SSH tunnels)

func RestoreDatabaseWithConnectionAndOptions

func RestoreDatabaseWithConnectionAndOptions(profile config.Profile, dumpPath string, options *RestoreOptions) error

RestoreDatabaseWithConnectionAndOptions performs a database restore using a DBConnection (supports SSH tunnels), honouring the supplied RestoreOptions.

Types

type Bar added in v1.2.1

type Bar struct {
	*progressbar.ProgressBar
	// contains filtered or unexported fields
}

Bar is a progress bar whose elapsed-time goroutine is tied to its lifetime.

The elapsed-time suffix is driven by a goroutine that polls the bar. Stopping it on IsFinished alone leaves that goroutine alive for any caller that never finishes the bar — an error return, a cancelled command — for the rest of the process. A CLI that runs one command and exits does not care; a process that hosts the MCP tools, or a test binary running thousands of cases, accumulates one leaked goroutine per bar.

Wrapping the progress bar means every existing Finish call releases the goroutine, because Finish is the method being called.

func NewProgressBarWithTimer

func NewProgressBarWithTimer(total int64, description string) *Bar

NewProgressBarWithTimer creates a progress bar (or a spinner when the total is unknown/non-positive) and starts a goroutine that continuously appends an elapsed-time suffix to the bar's description.

Call Finish on the returned bar, normally with defer, so the goroutine ends on every path out of the function.

func (*Bar) Add added in v1.2.1

func (b *Bar) Add(n int) error

Add advances the bar.

func (*Bar) Describe added in v1.2.1

func (b *Bar) Describe(description string)

Describe replaces the bar description, keeping the elapsed-time suffix.

func (*Bar) Finish added in v1.2.1

func (b *Bar) Finish() error

Finish completes the bar and stops its elapsed-time goroutine. Safe to call more than once.

type CSVOptions

type CSVOptions struct {
	BatchSize int  // Number of rows to process in each batch
	HasHeader bool // Whether the first row of the CSV contains column headers
}

CSVOptions contains configuration for CSV operations

func DefaultCSVOptions

func DefaultCSVOptions() *CSVOptions

DefaultCSVOptions returns default CSV configuration

type DumpOptions

type DumpOptions struct {
	Format        string   // plain, custom, directory, tar
	Compress      bool     // Enable compression
	SchemaOnly    bool     // Export schema only
	DataOnly      bool     // Export data only
	Tables        []string // Specific tables to include
	ExcludeTables []string // Tables to exclude
	Schema        string   // Specific schema
	Verbose       bool     // Verbose output
	Timeout       int      // Command timeout in seconds
}

DumpOptions contains advanced options for pg_dump

type MigrationOptions

type MigrationOptions struct {
	SourceProfile  config.Profile
	TargetProfile  config.Profile
	SchemaOnly     bool
	DataOnly       bool
	Tables         []string
	Validate       bool
	EnableRollback bool
	Verbose        bool
	Timeout        int
	Overwrite      bool
	Strict         bool
	BatchSize      int
}

MigrationOptions defines options for database migration

type RestoreOptions

type RestoreOptions struct {
	// Clean drops database objects before recreating them (pg_restore only).
	Clean bool
	// Timeout, when > 0, aborts the restore after the given number of seconds.
	Timeout int
	// Strict makes any restore error fatal. By default restore tolerates
	// non-fatal errors reported by pg_restore (e.g. minor client/server
	// version mismatches such as "unrecognized configuration parameter").
	// Note that pg_restore exits with code 1 when it encounters ignorable
	// errors, so the default (Strict == false) keeps restores resilient to
	// version skew.
	Strict bool
}

RestoreOptions controls restore behaviour for pg_restore/psql.

Jump to

Keyboard shortcuts

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