storage

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package storage wraps the local SQLite database (WAL mode).

A pure-Go driver (modernc.org/sqlite) is used so the binary cross-compiles to linux/arm64 without a C toolchain, which the design doc requires for deploying to Orange Pi / Raspberry Pi nodes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Checkpoint added in v0.0.8

func Checkpoint(ctx context.Context, db *sql.DB, mode string) error

Checkpoint executes PRAGMA wal_checkpoint with the given mode ("PASSIVE", "FULL", "RESTART", "TRUNCATE"). "TRUNCATE" or "PASSIVE" flush committed WAL pages into the database file, preventing unbounded WAL growth and ensuring durability before graceful shutdown. PRAGMA arguments cannot be bound parameters, so the mode is whitelisted rather than quoted — only the four names SQLite defines may reach the string.

func LatestVersion added in v0.0.9

func LatestVersion() int

LatestVersion returns the highest schema version this binary can migrate to — the value Migrate stamps into user_version after a fully applied run. The self-updater compares a staged release's LatestVersion against the data directory's user_version before swapping binaries, so an update can never install a build that would refuse to open the database at startup.

func Migrate

func Migrate(db *sql.DB) error

Migrate applies pending schema migrations and advances PRAGMA user_version. It is safe to call repeatedly: already-applied versions are skipped.

Migrations are defined in migrations.go. Each migration runs in its own BEGIN IMMEDIATE transaction and updates user_version on success, so a partially failed run can be retried without double-applying earlier steps — and two handles migrating the same file serialize on the reserved lock instead of colliding at commit.

A database whose user_version is ahead of this binary's migration list is an error, not a no-op: running an old binary against a newer schema would silently corrupt assumptions the newer schema already made.

func Now

func Now() int64

Now returns Unix seconds; SQLite timestamps are stored as epoch seconds.

func Open

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

Open opens (or creates) the SQLite database at path in WAL mode. The parent directory is created automatically so callers never hit SQLITE_CANTOPEN (error 14) because a parent directory was missing — this is the most common first-run failure when `panda` is invoked from any directory other than the project root.

Types

type Migration

type Migration struct {
	Version int
	Name    string
	Apply   func(MigrationExec) error
}

Migration is a single schema change identified by a monotonic version. Each migration runs inside its own BEGIN IMMEDIATE transaction (see migrate.go) and, on success, advances PRAGMA user_version to its Version. The Apply body receives a MigrationExec (Exec/Query/QueryRow) rather than a *sql.Tx: the transaction is driven explicitly on the pooled connection.

type MigrationExec added in v0.0.7

type MigrationExec interface {
	Exec(query string, args ...any) (sql.Result, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
}

MigrationExec is the statement surface a migration body needs: Exec, Query, QueryRow — nothing else is used by any of the migrateVN functions. It exists so a migration can run inside an explicit BEGIN IMMEDIATE transaction driven on the raw *sql.DB (see runMigration), which db.Begin() cannot produce (it only issues a plain BEGIN).

Jump to

Keyboard shortcuts

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