db

package
v0.11.5 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backup added in v0.8.0

func Backup(ctx context.Context, dsn string, w io.Writer) error

Backup runs pg_dump and writes gzip-compressed SQL to w.

func BackupToSFTP added in v0.8.0

func BackupToSFTP(ctx context.Context, dsn string, cfg SFTPConfig) (string, error)

BackupToSFTP creates a backup and uploads it to the configured SFTP server. Returns the remote filename.

func BeginTx

func BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a transaction using the singleton connection.

func Command added in v0.4.0

func Command() *cobra.Command

Command returns the "db" cobra command group with query and migrate subcommands.

func ConfigGet

func ConfigGet(ctx context.Context, key string) (string, bool, error)

ConfigGet retrieves a value from _schemaf_config. Returns ("", false, nil) if the key doesn't exist.

func ConfigSet

func ConfigSet(ctx context.Context, key, value string) error

ConfigSet inserts or updates a key in _schemaf_config.

func DB

func DB() *sql.DB

DB returns the raw *sql.DB singleton. If the connection hasn't been opened yet but a DSN was registered via SetDSN, it connects lazily (without running migrations).

func DeleteOldBackups added in v0.8.0

func DeleteOldBackups(cfg SFTPConfig) error

DeleteOldBackups removes the oldest backups beyond the retain count.

func ExecContext

func ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext executes a query that doesn't return rows using the singleton connection.

func HealthCheck

func HealthCheck() error

HealthCheck pings the singleton database and returns an error if unhealthy.

func Init

func Init(dsn string) error

Init opens a new Postgres connection pool, pings the server, and stores it as the package-level singleton. Use this for eager initialization (e.g. server startup).

func LastBackupStatus added in v0.10.0

func LastBackupStatus() (time.Time, string)

LastBackupStatus returns the time and error string of the last backup. Returns zero time if no backup has run yet.

func Open

func Open(dsn string) (*sql.DB, error)

Open opens a new Postgres connection pool and returns it. Deprecated: prefer Init.

func QueryContext

func QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext executes a query that returns rows using the singleton connection.

func QueryRowContext

func QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a query that returns a single row using the singleton connection.

func RegisterMigrations

func RegisterMigrations(ms MigrationSet)

RegisterMigrations registers a MigrationSet to be run by RunMigrations. Call before RunMigrations; typically in init().

func ResetProjectTables

func ResetProjectTables(t *testing.T, ctx context.Context, db *sql.DB)

ResetProjectTables truncates all non-framework tables in the public schema. Framework tables (prefixed with "schemaf_" or "_schemaf_") are preserved. Call this at the start of each test that uses a real database to guarantee a clean state.

Example:

func TestMyThing(t *testing.T) {
    db.ResetProjectTables(t, ctx, conn)
    // ... test body
}

func Restore added in v0.8.0

func Restore(ctx context.Context, dsn string, r io.Reader) error

Restore reads gzip-compressed SQL from r and pipes it into psql.

func RestoreFromSFTP added in v0.8.0

func RestoreFromSFTP(ctx context.Context, dsn string, cfg SFTPConfig, filename string) error

RestoreFromSFTP downloads a backup from SFTP and restores it.

func RunBackupScheduler added in v0.8.0

func RunBackupScheduler(ctx context.Context, dsn string, cfg SFTPConfig)

RunBackupScheduler runs daily backups at the configured UTC hour. On startup, it checks whether today's backup was missed (e.g. server was down) and runs a catch-up backup if needed. It blocks until the context is cancelled.

func RunMigrations

func RunMigrations(ctx context.Context) error

RunMigrations bootstraps the tracking table if needed, then runs all registered migration sets in registration order. Uses the singleton connection.

func RunMigrationsOn

func RunMigrationsOn(ctx context.Context, db *sql.DB) error

RunMigrationsOn runs migrations on a specific *sql.DB. Used for testing.

func SetDSN added in v0.3.13

func SetDSN(dsn string)

SetDSN stores the DSN for lazy initialization. The actual connection is deferred until the first call to DB().

Types

type BackupInfo added in v0.8.0

type BackupInfo struct {
	Name    string
	Size    int64
	ModTime time.Time
}

BackupInfo describes a remote backup file.

func ListRemoteBackups added in v0.8.0

func ListRemoteBackups(cfg SFTPConfig) ([]BackupInfo, error)

ListRemoteBackups returns backup files on the SFTP server, newest first.

type DBTX

type DBTX interface {
	ExecContext(context.Context, string, ...any) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...any) *sql.Row
}

DBTX is the interface for database operations.

type InsertMigrationParams

type InsertMigrationParams struct {
	Prefix  string
	Version int32
	Name    string
}

type MigrationSet

type MigrationSet struct {
	Prefix string   // namespaces migrations in schemaf_migrations, e.g. "schemaf", "myapp"
	Files  embed.FS // embedded migration SQL files
}

MigrationSet holds a set of migration files namespaced by prefix.

type Queries

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

Queries holds the database connection.

func New

func New(db DBTX) *Queries

New creates a new Queries instance.

func (*Queries) GetAppliedMigrations

func (q *Queries) GetAppliedMigrations(ctx context.Context, prefix string) ([]int32, error)

func (*Queries) InsertMigration

func (q *Queries) InsertMigration(ctx context.Context, arg InsertMigrationParams) error

type SFTPConfig added in v0.8.0

type SFTPConfig struct {
	Host   string
	Port   int
	User   string
	Key    []byte // PEM-encoded SSH private key
	Path   string // remote directory
	Retain int    // number of backups to keep
	Hour   int    // UTC hour for daily auto-backup
}

SFTPConfig holds connection details for remote backup storage.

func ReadSFTPConfigFromEnv added in v0.8.0

func ReadSFTPConfigFromEnv() (SFTPConfig, bool)

ReadSFTPConfigFromEnv reads backup configuration from environment variables. Returns false if BACKUP_SSH_HOST is not set (backups not configured).

Jump to

Keyboard shortcuts

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