dbtest

package
v0.70.11 Latest Latest
Warning

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

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

Documentation

Overview

Package dbtest composes storage providers for tests that need a real database without putting provider construction back into package database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseDatabase

func CloseDatabase(db *database.Database) error

CloseDatabase closes a test database and its provider host in dependency order. Use it when a test must close a database before its cleanup phase.

func LazyManifestIndex added in v0.70.11

func LazyManifestIndex(tb testing.TB) string

LazyManifestIndex returns the name of a non-critical deferred-index manifest entry, so a test can distinguish the full rebuild from the critical subset without pinning an index the manifest may reclassify later.

func MetadataIndexExists added in v0.70.11

func MetadataIndexExists(tb testing.TB, raw *sql.DB, name string) bool

MetadataIndexExists reports whether the named index is present in the SQLite catalog behind raw (see RawSQLiteMetadata). Deferred-index tests in several packages assert against the catalog rather than a store method, because the state under test is "the manifest disagrees with the schema", which no store method can express.

func MetadataTemplateBuilt added in v0.70.10

func MetadataTemplateBuilt() bool

MetadataTemplateBuilt reports whether this process has run the metadata migration to build its template. A forked child seeded through SeedMetadataTemplateBytes before NewDatabase must report false.

func MetadataTemplateBytes added in v0.70.10

func MetadataTemplateBytes() ([]byte, error)

MetadataTemplateBytes returns the bytes of a fully migrated SQLite metadata database for this process, building it (once, via the same sync.Once every other caller in this process shares) if nothing has needed it yet.

A caller that forks a child test process -- see database/lifecycle's TestSnapshotInterruptedBeforeManifest -- can hand these bytes to the child (there is no way to share the in-process cache itself across a process boundary) and have the child seed its own data directory with SeedMetadataTemplateBytes before ever calling NewDatabase. That skips the child's own migration entirely, rather than merely repeating the same one-per-process cost a second time in a fresh process.

func NewDatabase

func NewDatabase(
	tb testing.TB,
	config *database.Config,
) (*database.Database, error)

NewDatabase composes Badger and SQLite for a test. The database is closed before its provider host during the cleanup registered with tb.

func NewDatabaseWithMetadataWrapper added in v0.70.10

func NewDatabaseWithMetadataWrapper(
	tb testing.TB,
	opts Options,
	wrap func(metadata.MetadataStore) metadata.MetadataStore,
) (*database.Database, error)

NewDatabaseWithMetadataWrapper is NewDatabaseWithOptions, but passes the resolved metadata store through wrap before it is injected into the database. It exists for tests that must force a metadata-store method to return an error its backing implementation would not organically produce -- the same rationale RawSQLiteMetadata serves for a deliberately impossible row, applied instead to a synthetic storage fault (a timeout, a lost connection) from a specific lookup.

func NewDatabaseWithOptions

func NewDatabaseWithOptions(
	tb testing.TB,
	opts Options,
) (*database.Database, error)

NewDatabaseWithOptions composes the selected storage providers for a test. It registers the chosen providers on a fresh host, resolves them with the supplied dependencies (including run mode and metadata connection pool size), and injects the resulting stores into a database. The database is closed before its provider host during the cleanup registered with tb.

func RawSQLiteMetadata added in v0.69.0

func RawSQLiteMetadata(
	tb testing.TB,
	db *database.Database,
) (*sql.DB, error)

RawSQLiteMetadata opens a repository-internal database/sql fixture against the metadata file owned by db. It exists only for tests that must seed a deliberately impossible or partially-upgraded state that the public store contract cannot represent. Production code must use MetadataStore methods.

func SeedMetadataTemplateBytes added in v0.70.10

func SeedMetadataTemplateBytes(dir string, raw []byte) error

SeedMetadataTemplateBytes writes raw -- typically obtained from MetadataTemplateBytes, possibly in another process -- into dir as an already-migrated metadata.sqlite, so whatever provider later opens dir finds a migrated database instead of an empty one needing a fresh migration run.

Like seedMetadataTemplate, it is a no-op if dir already has a metadata file. A later NewDatabase on the same directory therefore keeps the seeded file and does not build this process's own template.

Types

type Options

type Options struct {
	Config         *database.Config
	Blob           StorageProvider
	Metadata       StorageProvider
	RunMode        string
	MaxConnections int

	// InMemoryMetadata opts out of the migrated SQLite template (see
	// template.go) and builds the metadata store the old way: a fresh
	// shared-cache in-memory database with the full migration run against
	// it. That costs ~1.7s per call under -race, so use it only for a test
	// that genuinely depends on in-memory semantics rather than on a
	// file-backed database -- WAL behavior, locking, or the absence of a
	// data directory. Ignored unless the metadata provider is sqlite and
	// the caller supplied no Metadata.Config of its own. Options.Config
	// does not disable it: with a DataDir the template is seeded into
	// that directory instead.
	InMemoryMetadata bool
}

Options configures a test database. The zero value composes the badger blob store and the sqlite metadata store with an unset run mode, matching NewDatabase.

type StorageProvider

type StorageProvider struct {
	Name     string
	Config   map[string]any
	Register func(*plugin.Host) error
}

StorageProvider selects a storage provider by name, supplies its provider-specific config, and names the registrar that installs it on the test host. A zero value selects the always-built default for its capability (badger for blob, sqlite for metadata). Register must be set for any non-default provider, such as the tag-gated cloud blob stores.

Jump to

Keyboard shortcuts

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