Documentation
¶
Overview ¶
Package db opens database connections and executes SQL against supported drivers.
Index ¶
- func DSN(conn Connection) (string, error)
- func DeleteRows(ctx context.Context, cfg Config, tableName, whereClause string) (int, error)
- func DumpTable(ctx context.Context, cfg Config, tableName string, opts ExecuteOptions, ...) (output.Result, error)
- func Execute(ctx context.Context, cfg Config, sqlText string, opts ExecuteOptions) (output.Result, error)
- func ExecuteToWriter(ctx context.Context, cfg Config, sqlText string, opts ExecuteOptions, ...) (output.Result, error)
- func InsertRow(ctx context.Context, cfg Config, tableName string, values map[string]string) (int, error)
- func Open(ctx context.Context, cfg Config) (*sql.DB, string, error)
- func Schemas(ctx context.Context, cfg Config) ([]string, error)
- func UpdateRows(ctx context.Context, cfg Config, tableName string, values map[string]string, ...) (int, error)
- type ColumnInfo
- type Config
- type Connection
- type ExecuteOptions
- type GrantInfo
- type ImportResult
- func LoadCSV(ctx context.Context, cfg Config, tableName string, r io.Reader) (ImportResult, error)
- func LoadJSON(ctx context.Context, cfg Config, tableName string, r io.Reader) (ImportResult, error)
- func LoadJSONL(ctx context.Context, cfg Config, tableName string, r io.Reader) (ImportResult, error)
- func LoadYAML(ctx context.Context, cfg Config, tableName string, r io.Reader) (ImportResult, error)
- type IndexInfo
- type Migration
- type MigrationPlan
- type MigrationResult
- func ApplyMigrations(ctx context.Context, cfg Config, dir string, limit int) (MigrationResult, error)
- func BaselineMigrations(ctx context.Context, cfg Config, dir, version string) (MigrationResult, error)
- func RollbackMigrations(ctx context.Context, cfg Config, dir string, limit int) (MigrationResult, error)
- type RoleInfo
- type SchemaInfo
- type TableInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DSN ¶
func DSN(conn Connection) (string, error)
DSN returns an explicit DSN or builds one from structured connection fields using driver-specific defaults.
func DeleteRows ¶
DeleteRows deletes rows matching whereClause.
func DumpTable ¶
func DumpTable(ctx context.Context, cfg Config, tableName string, opts ExecuteOptions, w io.Writer, format string) (output.Result, error)
DumpTable streams all rows from tableName to w using the requested output format.
func Execute ¶
func Execute(ctx context.Context, cfg Config, sqlText string, opts ExecuteOptions) (output.Result, error)
Execute runs each parsed statement in sqlText and returns the result from the last statement. Row-returning statements are scanned into output.Result, while write statements report affected row count.
func ExecuteToWriter ¶
func ExecuteToWriter(ctx context.Context, cfg Config, sqlText string, opts ExecuteOptions, w io.Writer, format string) (output.Result, error)
ExecuteToWriter runs SQL and streams the last row-returning statement directly to w. Earlier statements are executed for side effects only, matching Execute's "last statement is the result" behavior.
func InsertRow ¶
func InsertRow(ctx context.Context, cfg Config, tableName string, values map[string]string) (int, error)
InsertRow inserts one row into tableName.
func Open ¶
Open validates cfg, opens the matching database driver, and verifies the connection with PingContext. The returned driver is the normalized driver name.
Types ¶
type ColumnInfo ¶
type ColumnInfo struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Primary bool `json:"primary"`
Unique bool `json:"unique,omitempty"`
Default string `json:"default,omitempty"`
References string `json:"references,omitempty"`
}
ColumnInfo describes one column and common relational constraints.
type Connection ¶
type Connection struct {
Driver string
Host string
Port int
Database string
User string
Password string
SSLMode string
DSN string
}
Connection is the user-facing connection model used to construct DSNs.
type ExecuteOptions ¶
ExecuteOptions controls optional execution behavior for a SQL request.
type GrantInfo ¶
type GrantInfo struct {
Grantee string `json:"grantee"`
Object string `json:"object"`
Privilege string `json:"privilege"`
Grantable bool `json:"grantable"`
Raw string `json:"raw,omitempty"`
}
GrantInfo describes a database privilege grant.
type ImportResult ¶
type ImportResult struct {
RowsAffected int `json:"rows_affected"`
}
ImportResult summarizes a table import operation.
func LoadCSV ¶
LoadCSV inserts CSV rows into tableName. The first CSV record is treated as a header containing destination column names.
type IndexInfo ¶
type IndexInfo struct {
Name string `json:"name"`
Columns []string `json:"columns"`
Unique bool `json:"unique"`
Primary bool `json:"primary,omitempty"`
}
IndexInfo describes one table index.
type Migration ¶
type Migration struct {
Version string `json:"version"`
Name string `json:"name"`
Path string `json:"path"`
DownPath string `json:"down_path,omitempty"`
Applied bool `json:"applied"`
AppliedAt string `json:"applied_at,omitempty"`
Checksum string `json:"checksum,omitempty"`
StoredChecksum string `json:"stored_checksum,omitempty"`
Dirty bool `json:"dirty,omitempty"`
}
Migration describes one SQL migration file.
type MigrationPlan ¶
type MigrationPlan struct {
Pending []Migration `json:"pending"`
Applied []Migration `json:"applied"`
Rollback []Migration `json:"rollback"`
}
MigrationPlan describes pending apply and rollback candidates.
func PlanMigrations ¶
func PlanMigrations(ctx context.Context, cfg Config, dir string, rollbackLimit int) (MigrationPlan, error)
PlanMigrations returns pending apply and rollback candidates without changing the database.
type MigrationResult ¶
type MigrationResult struct {
Applied []Migration `json:"applied"`
}
MigrationResult summarizes an apply run.
func ApplyMigrations ¶
func ApplyMigrations(ctx context.Context, cfg Config, dir string, limit int) (MigrationResult, error)
ApplyMigrations applies pending SQL migration files in filename order. Each file is executed in its own transaction.
func BaselineMigrations ¶
func BaselineMigrations(ctx context.Context, cfg Config, dir, version string) (MigrationResult, error)
BaselineMigrations records migrations up to version as applied without executing their SQL.
func RollbackMigrations ¶
func RollbackMigrations(ctx context.Context, cfg Config, dir string, limit int) (MigrationResult, error)
RollbackMigrations rolls back the most recently applied migrations that have matching .down.sql files.
type RoleInfo ¶
type RoleInfo struct {
Name string `json:"name"`
Host string `json:"host,omitempty"`
Login bool `json:"login"`
Superuser bool `json:"superuser,omitempty"`
CreateRole bool `json:"create_role,omitempty"`
CreateDB bool `json:"create_db,omitempty"`
}
RoleInfo describes a database role or user.
type SchemaInfo ¶
type SchemaInfo struct {
Tables []TableInfo `json:"tables"`
}
SchemaInfo is database metadata collected from a live connection.