sqlitetemplate

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: AGPL-3.0 Imports: 8 Imported by: ? 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 sqlitetemplate gives tests a migrated SQLite database without migrating it for every test. A Template is migrated once per test binary, into an in-memory database kept open for the life of the binary, and CopyInto copies it into a test's own database with SQLite's online backup API. Migrating Studio's schema (models.InitModels) costs ~300 ms under -race; copying it costs a few milliseconds.

The package imports no Studio package, so models' own tests can use it. gorm keeps some state on the *gorm.DB handle rather than in the database (custom join models: models.RegisterJoinModels); a caller registers that on its handle after CopyInto.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(ctx context.Context, dst, src *sql.DB) error

Copy copies every page of src's main database into dst's with SQLite's online backup API.

Types

type Template

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

Template is a database migrated once, on first use, and copied from then on.

func New

func New(migrate func(*gorm.DB) error) *Template

New returns a Template that migrate prepares on first use.

func (*Template) CopyInto

func (tp *Template) CopyInto(dst *gorm.DB) error

CopyInto replaces the main database of dst with a copy of the template.

dst must keep its database on one connection (SetMaxOpenConns(1)): every connection to ":memory:" is a database of its own, so with a pool the copy would land in one connection's database and the others would stay empty.

Jump to

Keyboard shortcuts

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