golazy.dev
–
golazy.dev
/
lazymigrate
Index
|
Files
|
Directories
package lazymigrate ¶
import "golazy.dev/lazymigrate"
Types ¶
type Backend ¶
type Backend interface {
Setup(context.Context) error
List(context.Context) ([]BackendMigration, error)
Run(context.Context, Step) error
DumpSchema(context.Context) ([]byte, error)
LoadSchema(context.Context, []byte) error
}
type BackendMigration ¶
type BackendMigration struct {
ID string
}
type Catalog ¶
type Catalog struct {
// contains filtered or unexported fields
}
func (c *Catalog) Add ¶
func (c *Catalog) Add(database string, source Source) error
func (c *Catalog) LoadMigrations ¶
func (c *Catalog) LoadMigrations(ctx context.Context, database string) ([]Migration, error)
func (c *Catalog) Sources ¶
func (c *Catalog) Sources(database string) []Source
type Config ¶
type Config struct {
Backend Backend
Sources []Source
}
type Direction ¶
type Direction string
type FS ¶
type FS struct {
Files fs.FS
Dir string
}
func ForDatabase ¶
func ForDatabase(files fs.FS, database string) FS
func (source FS) LoadMigrations ¶
func (source FS) LoadMigrations(ctx context.Context) ([]Migration, error)
type Migration ¶
type Migration struct {
ID string
Prefix string
Timestamp string
Path string
Content []byte
}
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
func New ¶
func New(config Config) (*Migrator, error)
func (m *Migrator) Apply ¶
func (m *Migrator) Apply(ctx context.Context, plan Plan) error
func (m *Migrator) Down ¶
func (m *Migrator) Down(ctx context.Context, limit int) (Plan, error)
func (m *Migrator) DumpSchema ¶
func (m *Migrator) DumpSchema(ctx context.Context) ([]byte, error)
func (m *Migrator) List ¶
func (m *Migrator) List(ctx context.Context) ([]Status, error)
func (m *Migrator) LoadSchema ¶
func (m *Migrator) LoadSchema(ctx context.Context, schema []byte) error
func (m *Migrator) PlanDown ¶
func (m *Migrator) PlanDown(ctx context.Context, limit int) (Plan, error)
func (m *Migrator) PlanRedo ¶
func (m *Migrator) PlanRedo(ctx context.Context, limit int) (Plan, error)
func (m *Migrator) PlanUp ¶
func (m *Migrator) PlanUp(ctx context.Context, limit int) (Plan, error)
func (m *Migrator) Redo ¶
func (m *Migrator) Redo(ctx context.Context, limit int) (Plan, error)
func (m *Migrator) Setup ¶
func (m *Migrator) Setup(ctx context.Context) error
func (m *Migrator) Up ¶
func (m *Migrator) Up(ctx context.Context, limit int) (Plan, error)
type Plan ¶
type Plan struct {
Steps []Step
}
func (p Plan) Empty ¶
func (p Plan) Empty() bool
type Source ¶
type Source interface {
LoadMigrations(context.Context) ([]Migration, error)
}
type SourceFunc ¶
type SourceFunc func(context.Context) ([]Migration, error)
func (fn SourceFunc) LoadMigrations ¶
func (fn SourceFunc) LoadMigrations(ctx context.Context) ([]Migration, error)
type State ¶
type State string
type Status ¶
type Status struct {
ID string
State State
Migration Migration
BackendMigration BackendMigration
}
type Step ¶
type Step struct {
Direction Direction
Migration Migration
}
Directories ¶
| Path | Synopsis |
|---|---|
| lazymigrate/fakemigrator | Package fakemigrator provides an in-memory lazymigrate backend for tests. |
Package lazymigrate loads, plans, and applies ordered file-based migrations through backend-owned execution.
The package is intentionally backend-agnostic. It knows how to collect source migrations, compare them with the migration IDs a backend reports as already applied, and build an up, down, or redo plan. The backend owns the concrete migration language, the meaning of up and down sections, locking, transactions, metadata tables, and schema load or dump support.
A Source returns migration files. FS reads direct children from a directory in an fs.FS, skips nested directories and migrations.toml, rejects Go migration files, and requires each filename to contain a sortable timestamp. ForDatabase is the common layout helper for migrations/<database>, for example migrations/postgres. File extensions are ignored for migration IDs: 202606280001_create_documents.sql becomes ID 202606280001_create_documents. Prefix and Timestamp are parsed only for stable ordering; duplicate IDs across all loaded sources are rejected.
Catalog is useful when an application combines its own migrations with package-provided migrations. PostgreSQL service packages in golazy.dev/pg, such as pgjobs, pgfiles, pgmedia, and pgstorage, expose embedded migrations as lazymigrate.Source values so an application can add them to the same catalog as application migrations.
A Migrator is the planner and executor. List reports applied source migrations, pending source migrations, and missing backend migrations. PlanUp, PlanDown, and PlanRedo return the Step values that would run without touching the backend. Up, Down, and Redo build the plan and then call Apply. Apply calls Backend.Setup once before running the plan and passes each Step to Backend.Run in order. A migration recorded by the backend but missing from the loaded sources blocks execution plans so down and redo operations do not operate from incomplete source history.
Backend implementations connect this package to a real store. The golazy.dev/pg/pgmigrate package implements Backend for PostgreSQL. It parses SQL files with -- +lazy Up and -- +lazy Down sections, stores applied IDs in lazy_migrations, runs each step in a transaction, and uses an advisory lock while applying a migration. lazymigrate itself does not parse those sections; it passes the file content through unchanged so other backends can use their own format.
The fakemigrator subpackage provides an in-memory Backend for tests, examples, and early command wiring. It records planned steps and applied IDs but does not execute SQL.