migrate

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package migrate wraps goose with the safety rules this database needs.

The hazard being guarded against is specific and expensive. Production already carries all 102 tables of migration 00001, created by drizzle and tracked in drizzle.__drizzle_migrations. Goose knows nothing about that ledger, so a plain `goose up` against production would try to CREATE TABLE over a live 285 GB schema. Everything here exists to make that impossible rather than merely discouraged.

Index

Constants

View Source
const BaselineVersion int64 = 1

BaselineVersion is the migration that captures the pre-existing schema. Any database that already has the schema must have exactly this version stamped, never executed.

Variables

View Source
var ErrAlreadyBaselined = errors.New("database already has a goose ledger")

ErrAlreadyBaselined is returned when baselining a database that goose already tracks, which would otherwise silently rewrite history.

View Source
var ErrEmptySchema = errors.New("database has no existing schema to baseline")

ErrEmptySchema is returned when baselining a database with no schema. Stamping there would permanently skip table creation and leave an unusable database.

View Source
var ErrNeedsBaseline = errors.New("database has an existing schema but no goose ledger")

ErrNeedsBaseline is returned when a database already contains the schema but has no goose ledger. Applying migrations in that state would attempt to recreate live tables.

Functions

func Apply

func Apply(ctx context.Context, pool *pgxpool.Pool) error

Apply runs every pending migration. It refuses when the database has a schema but no ledger, because that is the production shape and applying there would try to recreate live tables.

func Baseline

func Baseline(ctx context.Context, pool *pgxpool.Pool) error

Baseline stamps the baseline version as applied without executing it, which is how a database that already has the schema joins goose's history.

It refuses on an empty database: stamping there would skip table creation forever and leave a database that can never be migrated into a working state.

Types

type Migration

type Migration struct {
	Version int64     `json:"version"`
	Source  string    `json:"source"`
	Applied bool      `json:"applied"`
	At      time.Time `json:"applied_at,omitzero"`
}

Migration is one entry in a migration plan.

type State

type State struct {
	HasLedger    bool        `json:"has_ledger"`
	HasSchema    bool        `json:"has_schema"`
	TableCount   int         `json:"table_count"`
	DBVersion    int64       `json:"db_version"`
	Migrations   []Migration `json:"migrations"`
	NeedsBaselin bool        `json:"needs_baseline"`
}

State describes what goose knows about a database versus what exists on disk.

func Inspect

func Inspect(ctx context.Context, pool *pgxpool.Pool) (*State, error)

Inspect reports the migration state without changing anything. It is the basis for both `db:status` and the guard inside Apply.

func (*State) Pending

func (s *State) Pending() []Migration

Pending returns the migrations that Apply would run.

Jump to

Keyboard shortcuts

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