Documentation
¶
Overview ¶
Package db opens the SQLite database, applies embedded goose migrations and provides transaction helpers shared by the repositories in internal/store.
Index ¶
- func DSN(path string, busy time.Duration) string
- func IsCheckViolation(err error) bool
- func IsForeignKeyViolation(err error) bool
- func IsUniqueViolation(err error) bool
- func Migrations() fs.FS
- func MigrationsPG() fs.FS
- func Rebind(query string) string
- type DB
- func (d *DB) Dialect() Dialect
- func (d *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (d *DB) LatestSchemaVersion() (int64, error)
- func (d *DB) Migrate(ctx context.Context) ([]MigrationResult, error)
- func (d *DB) Path() string
- func (d *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (d *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (d *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (d *DB) SchemaVersion(ctx context.Context) (int64, error)
- func (d *DB) WithTx(ctx context.Context, fn func(tx *sql.Tx) error) (err error)
- func (d *DB) Wrap(q Querier) Querier
- type Dialect
- type MigrationResult
- type Options
- type PostgresOptions
- type Querier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DSN ¶
DSN builds the modernc.org/sqlite connection string for path with the pragmas every connection must carry.
func IsCheckViolation ¶
IsCheckViolation reports whether err is a CHECK constraint failure.
func IsForeignKeyViolation ¶
IsForeignKeyViolation reports whether err is a FOREIGN KEY constraint failure.
func IsUniqueViolation ¶
IsUniqueViolation reports whether err is a UNIQUE or PRIMARY KEY constraint failure.
func Migrations ¶
Migrations returns the embedded goose migration files for SQLite.
func MigrationsPG ¶ added in v0.9.0
MigrationsPG returns the embedded goose migration files for PostgreSQL.
Types ¶
type DB ¶
DB wraps *sql.DB for the panel database.
func Open ¶
Open opens (creating if needed) the SQLite database at path and verifies the connection. It does not run migrations; call Migrate.
func OpenPostgres ¶ added in v0.9.0
OpenPostgres opens a connection pool to a PostgreSQL database at dsn and verifies it.
func (*DB) Dialect ¶ added in v0.9.0
Dialect returns the database engine dialect (sqlite or postgres).
func (*DB) ExecContext ¶ added in v0.9.0
ExecContext executes a query using the underlying db, rebinding parameters for PostgreSQL.
func (*DB) LatestSchemaVersion ¶
LatestSchemaVersion returns the newest embedded migration version.
func (*DB) Migrate ¶
func (d *DB) Migrate(ctx context.Context) ([]MigrationResult, error)
Migrate applies all pending migrations and returns those applied.
func (*DB) PrepareContext ¶ added in v0.9.0
PrepareContext creates a prepared statement using the underlying db, rebinding parameters for PostgreSQL.
func (*DB) QueryContext ¶ added in v0.9.0
QueryContext executes a query using the underlying db, rebinding parameters for PostgreSQL.
func (*DB) QueryRowContext ¶ added in v0.9.0
QueryRowContext executes a query using the underlying db, rebinding parameters for PostgreSQL.
func (*DB) SchemaVersion ¶
SchemaVersion returns the current goose schema version (0 when unmigrated).
type Dialect ¶ added in v0.9.0
type Dialect string
Dialect identifies the underlying SQL database engine.
type MigrationResult ¶
MigrationResult describes one applied migration.
type Options ¶
type Options struct {
// MaxOpenConns caps the pool (default 8). WAL allows concurrent readers;
// writers are serialized by SQLite (BEGIN IMMEDIATE + busy_timeout).
MaxOpenConns int
// BusyTimeout is how long a writer waits for the lock (default 5s).
BusyTimeout time.Duration
}
Options tune Open. The zero value is suitable for production.
type PostgresOptions ¶ added in v0.9.0
type PostgresOptions struct {
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
ConnMaxIdleTime time.Duration
}
PostgresOptions configures connection pooling for PostgreSQL.
type Querier ¶
type Querier interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
Querier is the subset of *sql.DB / *sql.Tx used by repositories, so the same repository code runs inside or outside a transaction.