migrate

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package migrate provides a versioned schema migration engine for tapes storage backends.

Storage drivers construct a Migrator with NewMigrator, passing their own ordered list of Migration values (typically loaded via //go:embed from a migrations/ directory under the driver package). The engine tracks applied versions in a schema_migrations table with a UNIQUE constraint on version, and detects gaps to fail fast on corrupted state.

Postgres uses an advisory lock to serialize concurrent migrators; SQLite relies on its file-level lock.

Index

Constants

View Source
const (
	// DialectPostgres identifies the PostgreSQL dialect.
	DialectPostgres = "postgres"

	// DialectSQLite identifies the SQLite dialect.
	DialectSQLite = "sqlite"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Migration

type Migration struct {
	// Version is the sequential migration number (1, 2, 3, ...).
	Version int

	// Description is a human-readable summary of the migration.
	Description string

	// Up is the SQL to apply for this migration.
	// The SQL is dialect-specific — each driver supplies its own migrations
	// so the engine does not need to branch on dialect for the SQL itself.
	Up string
}

Migration represents a single versioned schema migration.

func MigrationsFromFS

func MigrationsFromFS(fsys fs.FS) ([]Migration, error)

MigrationsFromFS reads all .sql files from an fs.FS (typically produced by //go:embed) and returns an ordered slice of Migration values.

File names must follow the pattern "NNN_description.sql" where NNN is a zero-padded integer version (e.g. "001_baseline_schema.sql"). Files that don't match this pattern are silently skipped.

type Migrator

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

Migrator applies an ordered set of migrations to a database. Create one with NewMigrator.

func NewMigrator

func NewMigrator(db *sql.DB, dialect string, migrations []Migration) (*Migrator, error)

NewMigrator creates a Migrator for the given database, dialect, and ordered migration list. The dialect must be DialectPostgres or DialectSQLite; unknown dialects are rejected immediately.

Migrations are sorted by version internally. Callers should supply them in order, but the constructor enforces sorting for safety.

func (*Migrator) Apply

func (m *Migrator) Apply(ctx context.Context) error

Apply runs all pending migrations against the database. It is safe to call concurrently from multiple processes — Postgres uses an advisory lock to serialize, and SQLite serializes via its file lock.

func (*Migrator) CurrentVersion

func (m *Migrator) CurrentVersion(ctx context.Context) (int, error)

CurrentVersion returns the current schema version for the database. Returns 0 if no migrations have been applied (or the table doesn't exist).

Jump to

Keyboard shortcuts

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