storage

package
v0.2.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package storage opens the hub database and applies its migrations (embedded, applied in order and recorded). It supports SQLite, the default, through a pure Go driver, and PostgreSQL through pgx; the choice is made by the configuration. This is the one package that knows the differences between the two: the other packages receive a *sql.DB, write their queries with ? placeholders and own their own tables. It does not move data from one backend to the other (#124).

Index

Constants

View Source
const (
	TypeSQLite   = "sqlite"
	TypePostgres = "postgres"
)

Database types.

View Source
const FileName = "regulus.db"

FileName is the name of the SQLite database file inside the data directory.

View Source
const MinPostgresVersion = 14

MinPostgresVersion is the oldest PostgreSQL major version Regulus supports: 14, the one in Ubuntu 22.04. Newer ones (16 in Ubuntu 24.04) work too.

Variables

This section is empty.

Functions

func Explain

func Explain(err error) string

Explain turns an error from the database into a sentence that says what to fix. It never includes a password.

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation reports whether err is the database refusing a row because a unique or primary key constraint would be broken, on either backend.

func Migrate

func Migrate(ctx context.Context, db *sql.DB, typ string) error

Migrate applies, in order and each in its own transaction, the embedded migrations that are not yet recorded in schema_migrations. It refuses to run on a database that has migrations this binary does not know, which means the database was written by a newer version of Regulus.

typ is TypeSQLite (also when empty) or TypePostgres. On PostgreSQL the whole run holds an advisory lock, so two hubs that start together on one database do not migrate it at the same time: the second waits, then finds nothing to do.

func Open

func Open(ctx context.Context, cfg Config) (*sql.DB, error)

Open opens the database named by cfg and applies all pending migrations. For SQLite the file is created if needed.

func OpenRaw

func OpenRaw(cfg Config) (*sql.DB, error)

OpenRaw opens a pool without pinging it and without migrating. Tests use it to prepare or clean a database.

func OpenSQLite

func OpenSQLite(ctx context.Context, dataDir string) (*sql.DB, error)

OpenSQLite opens the SQLite database in dataDir. It is shorthand for the default configuration.

func Rebind

func Rebind(query string) string

Rebind rewrites the ? placeholders of a query written for SQLite into the $1, $2, ... that PostgreSQL wants. A ? inside a quoted string or identifier is left alone. The rest of the code writes its queries once, with ?.

func ValidSchema

func ValidSchema(name string) bool

ValidSchema reports whether name is empty or a plain identifier.

Types

type Config

type Config struct {
	// Type is TypeSQLite (the default) or TypePostgres.
	Type string
	// DataDir holds the SQLite file; with PostgreSQL it still holds the keys.
	DataDir string

	// The rest applies to PostgreSQL only.
	Host        string
	Port        int
	Name        string
	User        string
	Password    string
	SSLMode     string
	SSLRootCert string
	// Schema is where the tables live (search_path). Empty means "public".
	Schema string
}

Config says which database the hub uses and how to reach it.

func (Config) Describe

func (c Config) Describe() string

Describe names the backend for logs and status output. It never includes the password.

func (Config) IsPostgres

func (c Config) IsPostgres() bool

IsPostgres reports whether the configuration selects PostgreSQL.

func (Config) URL

func (c Config) URL() string

URL builds the connection URL of a PostgreSQL configuration. It contains the password: use it only to connect, never to print.

func (Config) ValidSchema

func (c Config) ValidSchema() bool

ValidSchema reports whether Schema is empty or a plain identifier.

func (Config) Validate

func (c Config) Validate() error

Validate reports whether the configuration is usable.

type Report

type Report struct {
	// Backend is "SQLite" or "PostgreSQL".
	Backend string
	// Where names the database, without any password.
	Where string
	// Version is the version of the database server (or SQLite library).
	Version string
	// Schema is the highest migration applied, 0 for a database not migrated.
	Schema int
	// Known is the highest migration this binary knows.
	Known int
	// Created is false for a SQLite file that does not exist yet.
	Created bool
}

Report is what Check found out about a database.

func Check

func Check(ctx context.Context, cfg Config) (Report, error)

Check connects with the given settings and reports on the database without changing anything that remains: it does not run migrations and does not create a SQLite file. It tries once and returns at the first problem, with a message that says what to fix (see Explain). The installer uses it.

Directories

Path Synopsis
Package storagetest opens a hub database for tests.
Package storagetest opens a hub database for tests.

Jump to

Keyboard shortcuts

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