Documentation
¶
Index ¶
- 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 ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func HealthCheck() error
- func Init(dsn string) error
- 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 RunMigrations(ctx context.Context) error
- func RunMigrationsOn(ctx context.Context, db *sql.DB) error
- func SetDSN(dsn string)
- type DBTX
- type InsertMigrationParams
- type MigrationSet
- type Queries
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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 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 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 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