Documentation
¶
Index ¶
- func Backup(ctx context.Context, dsn string, w io.Writer) error
- func BackupToSFTP(ctx context.Context, dsn string, cfg SFTPConfig) (string, error)
- func BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func Command() *cobra.Command
- func ConfigGet(ctx context.Context, key string) (string, bool, error)
- func ConfigSet(ctx context.Context, key, value string) error
- func DB() *sql.DB
- func DeleteOldBackups(cfg SFTPConfig) error
- func ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func HealthCheck() error
- func Init(dsn string) error
- func LastBackupStatus() (time.Time, string)
- func Open(dsn string) (*sql.DB, error)
- func QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func RegisterMigrations(ms MigrationSet)
- func ResetProjectTables(t *testing.T, ctx context.Context, db *sql.DB)
- func Restore(ctx context.Context, dsn string, r io.Reader) error
- func RestoreFromSFTP(ctx context.Context, dsn string, cfg SFTPConfig, filename string) error
- func RunBackupScheduler(ctx context.Context, dsn string, cfg SFTPConfig)
- func RunMigrations(ctx context.Context) error
- func RunMigrationsOn(ctx context.Context, db *sql.DB) error
- func SetDSN(dsn string)
- type BackupInfo
- type DBTX
- type InsertMigrationParams
- type MigrationSet
- type Queries
- type SFTPConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BackupToSFTP ¶ added in v0.8.0
BackupToSFTP creates a backup and uploads it to the configured SFTP server. Returns the remote filename.
func Command ¶ added in v0.4.0
Command returns the "db" cobra command group with query and migrate subcommands.
func ConfigGet ¶
ConfigGet retrieves a value from _schemaf_config. Returns ("", false, nil) if the key doesn't exist.
func 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 ¶
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 ¶
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
LastBackupStatus returns the time and error string of the last backup. Returns zero time if no backup has run yet.
func QueryContext ¶
QueryContext executes a query that returns rows using the singleton connection.
func QueryRowContext ¶
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 ¶
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 RestoreFromSFTP ¶ added in v0.8.0
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 ¶
RunMigrations bootstraps the tracking table if needed, then runs all registered migration sets in registration order. Uses the singleton connection.
func RunMigrationsOn ¶
RunMigrationsOn runs migrations on a specific *sql.DB. Used for testing.
Types ¶
type BackupInfo ¶ added in v0.8.0
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 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 (*Queries) GetAppliedMigrations ¶
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).