migrate

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0 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 migrate runs the embedded SQL migrations in order, idempotently.

Migrations live in /migrations as numbered .sql files. They are embedded into the chalkd binary and applied at startup (or via `chalkd --migrate-only`, added in phase 12). Each migration runs inside its own transaction; a schema_migrations table records what's been applied.

Migrations are forward-only. Down migrations are not provided; if a rollback is needed, restore from a Postgres snapshot. This is a deliberate choice: down migrations are routinely wrong, especially for data-bearing changes, and the project doesn't need them.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoMigrations = errors.New("no migrations found")

ErrNoMigrations is returned when the embedded fs is empty.

Functions

This section is empty.

Types

type Migration

type Migration struct {
	// Version is the file's numeric prefix (e.g. "0001"). Sortable lexicographically.
	Version string
	// Name is the descriptive part of the filename (e.g. "init").
	Name string
	// SQL is the entire file contents.
	SQL string
}

Migration represents a single SQL file to apply.

func Load

func Load(fsys fs.FS, dir string) ([]Migration, error)

Load parses the supplied filesystem into an ordered slice of migrations. Files must be named like "NNNN_description.sql" (e.g. "0001_init.sql"). Files not matching this pattern are ignored with no error, allowing README.md or .gitkeep to live alongside.

fsys is typically the package-level embed.FS exposed by package chalk (it satisfies io/fs.FS), but any io/fs.FS works -- handy for unit tests.

func (Migration) FullName

func (m Migration) FullName() string

FullName returns "version_name", which is what's stored in schema_migrations.

type Result

type Result struct {
	Migration Migration
	Skipped   bool
	Duration  time.Duration
}

Result describes one applied migration.

func Run

func Run(ctx context.Context, pool *pgxpool.Pool, ms []Migration, logf func(string, ...any)) ([]Result, error)

Run applies all pending migrations against the pool. Already-applied migrations are skipped. Application is logged via the supplied logf.

Jump to

Keyboard shortcuts

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