Help us shape the future of Go! Take the 2026 Go Developer Survey

database

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const DB_PRAGMAS = "?_foreign_keys=ON&_journal_mode=WAL&_synchronous=NORMAL"

DB_PRAGMAS are the connection parameters for a read-write database: foreign keys enforced, write-ahead logging and synchronous NORMAL. The driver runs each of them as a PRAGMA when a connection is opened.

View Source
const DB_PRAGMAS_READONLY = "?_foreign_keys=ON&mode=ro"

DB_PRAGMAS_READONLY are the connection parameters for a read-only database. There is deliberately no journal mode: a read-only connection can't change it, and asking for WAL on a database that is stored in rollback-journal mode fails with "attempt to write a readonly database". That is exactly how the databases inside an export are stored, since VACUUM INTO writes them that way.

Variables

This section is empty.

Functions

func DSN

func DSN(path string, readOnly bool) string

DSN returns the connection string for the SQLite database at path, which every database open in modctl should use rather than building its own.

It is a file: URI with an escaped path. With a plain path the driver cuts the query string off before SQLite sees it, which silently drops mode=ro (the connection stays writable), and a path that contains a '?' is cut short at it. The read-only variant is described at DB_PRAGMAS_READONLY.

func EnsureExists

func EnsureExists(path string) error

EnsureExists verifies that the database file at path exists and is a regular file. If not, it returns a user-friendly error.

func InTx

func InTx(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error

InTx runs fn in a transaction on db. It is committed if fn returns nil, and rolled back if fn returns an error (which is returned as it is) or panics (as it goes up). A transaction that can't be committed or can't be started is an error that says so ("commit: ..." and "begin transaction: ..."), and in the second case fn is not called.

fn must not use db for what has to be in the transaction: the transaction is its own connection, and what is done through db is not part of it.

func IsForeignKeyConstraint

func IsForeignKeyConstraint(err error) bool

IsForeignKeyConstraint reports whether err is SQLite complaining that a FOREIGN KEY constraint would have been violated: a row that refers to something that isn't there, or the deletion of something that is still referred to.

func IsTriggerConstraint

func IsTriggerConstraint(err error) bool

IsTriggerConstraint reports whether err is a trigger of ours stopping the statement with RAISE(ABORT, ...) and the like.

func IsUniqueConstraint

func IsUniqueConstraint(err error) bool

IsUniqueConstraint reports whether err is SQLite complaining that a UNIQUE constraint (or unique index) would have been violated. A PRIMARY KEY violation is a different error and is not matched.

func SchemaVersion

func SchemaVersion(ctx context.Context, p *goose.Provider) (int64, error)

SchemaVersion returns the applied migration version as seen by p. It is split out from Schema.CurrentVersion so that a test can have a provider that is controlled further than a Schema is (how far it has migrated, say).

func Setup

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

Setup opens the read-write database at path, with the migrations not applied (see Migrate and Open). Nothing is done to the file until the first statement is: the driver is lazy, so a path that is not there is no error here.

func SetupReadOnly

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

SetupReadOnly is Setup for a database that is only read: the connection can't change it, and it is not created by looking for it.

func WithTx

func WithTx(ctx context.Context, db *sql.DB, fn func(q *dbq.Queries) error) error

WithTx is InTx for what is done with the queries of dbq: fn is given the queries for the transaction.

Types

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

A Schema is the schema of the database: the migrations that make it. They are in a directory called "migrations" of the file system that the Schema is made with, which in the binary is the ones that are embedded in it (main has them, since they are not in a directory that this package can embed them from).

What works on a database is given the Schema that it brings the database to, and does not find one: a test has the Schema of the repository, or one of its own, and none of them is anything but what it is given.

func NewSchema

func NewSchema(root fs.FS) *Schema

NewSchema is the Schema that is made by the migrations in the "migrations" directory of root. A root that is nil has no migrations: every use of the Schema is an error that says so (and so is every use of a Schema that is nil).

func (*Schema) CurrentVersion

func (s *Schema) CurrentVersion(ctx context.Context, db *sql.DB) (int64, error)

CurrentVersion returns the version of the latest migration that has been applied to db. This is what the database itself is at, which is not necessarily the latest migration of the Schema: a new database reports 0 and one that has not been migrated since an upgrade reports its older version.

func (*Schema) LatestVersion

func (s *Schema) LatestVersion() (int64, error)

LatestVersion returns the version of the latest migration of the Schema: the highest version among its migrations. It does not look at any database, so it is the version a database ends up at once it has been fully migrated, and what a bundle's schema version has to be compared against to tell whether this build can import it.

func (*Schema) Migrate

func (s *Schema) Migrate(ctx context.Context, db *sql.DB) error

Migrate brings db to the latest migration of the Schema.

func (*Schema) Open

func (s *Schema) Open(ctx context.Context, path string) (*sql.DB, error)

Open opens the database that the commands use, at path: it has to be there (it is made by init), and it is migrated to the latest migration of the Schema before it is handed over. The caller closes it.

func (*Schema) Provider

func (s *Schema) Provider(db *sql.DB) (*goose.Provider, error)

Provider is a goose provider for db that has the migrations of the Schema.

type SqliteStore

type SqliteStore struct {
	goosedb.Store // embed and delegate everything
}

A custom goose store to let us override the schema migrations table to use more idiomatic sqlite

func (*SqliteStore) CreateVersionTable

func (s *SqliteStore) CreateVersionTable(ctx context.Context, db goosedb.DBTxConn) error

Jump to

Keyboard shortcuts

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