db

package
v0.3.10 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginTx

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

BeginTx starts a transaction using the singleton connection.

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. Prefer the package-level helpers (QueryContext, ExecContext, QueryRowContext) over this where possible.

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. Call once at startup before any handlers run.

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 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.

Types

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

Jump to

Keyboard shortcuts

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