Documentation
¶
Overview ¶
Package migrate is a small numbered up/down schema migration runner for control-plane state stores and (separately) the pgvector vector store. No external migrator dependency: each backend registers Go Migration steps (baseline = today's full Init DDL) and tracks applied versions in a bookkeeper table (schema_migrations for state; vector stores that share Postgres must use a distinct table name).
Index ¶
- Variables
- func Downgrade(ctx context.Context, bk Bookkeeper, migrations []Migration) error
- func MongoCollectionExists(ctx context.Context, db *mongo.Database, name string) (bool, error)
- func PgxTableExists(ctx context.Context, db PgxDB, table string) (bool, error)
- func TableExists(ctx context.Context, db DB, d Dialect, table string) (bool, error)
- func Upgrade(ctx context.Context, bk Bookkeeper, migrations []Migration, ...) error
- type Bookkeeper
- type DB
- type Dialect
- type Migration
- type Mongo
- type Pgx
- type PgxDB
- type SQL
Constants ¶
This section is empty.
Variables ¶
var ErrNoMigration = errors.New("no migration to roll back")
ErrNoMigration means Downgrade was asked to roll back but nothing is applied.
Functions ¶
func Downgrade ¶
func Downgrade(ctx context.Context, bk Bookkeeper, migrations []Migration) error
Downgrade runs Down for the highest applied version and deletes its row.
func MongoCollectionExists ¶
MongoCollectionExists reports whether a collection name is present.
func PgxTableExists ¶
PgxTableExists reports whether a relation exists in the public schema.
func TableExists ¶
TableExists reports whether a user table exists (legacy-schema probe).
func Upgrade ¶
func Upgrade(ctx context.Context, bk Bookkeeper, migrations []Migration, legacyExists func(context.Context) (bool, error)) error
Upgrade ensures the migrations table exists, heals and stamps baseline v1 when an already-initialized legacy database has no version rows, then applies every pending Up in order.
Legacy path (tables exist, schema_migrations empty): runs migration 1's Up before stamping. Baseline Up is written to be idempotent (CREATE IF NOT EXISTS / existence-checked ADD COLUMN), and those ADD COLUMN steps are exactly the self-healing the old unconditional Init ran on every boot -- skipping Up and only inserting the version row would leave older schemas missing columns like threads.version.
Types ¶
type Bookkeeper ¶
type Bookkeeper interface {
Ensure(ctx context.Context) error
Current(ctx context.Context) (int, error)
Insert(ctx context.Context, version int, name string) error
Delete(ctx context.Context, version int) error
}
Bookkeeper records which migration versions have been applied.
type DB ¶
type DB interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
DB is the subset of *sql.DB / *sql.Conn used for version bookkeeping and helpers. Postgres uses a separate pgx-backed Bookkeeper.
type Migration ¶
type Migration struct {
Version int
Name string
Up func(ctx context.Context) error
Down func(ctx context.Context) error
}
Migration is one numbered schema step. Versions must be contiguous from 1.
type Pgx ¶
Pgx is a Bookkeeper backed by pgx.
TableName selects the version table (default schema_migrations). Vector stores that share a Postgres database with the control-plane state store must use a distinct name (e.g. vector_schema_migrations) so the two version streams never collide.
func NewPgxTable ¶
NewPgxTable returns a Bookkeeper writing to tableName. tableName must be a fixed safe identifier from code (never request/user input).
type PgxDB ¶
type PgxDB interface {
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}
PgxDB is the subset of *pgxpool.Pool / *pgxpool.Conn used for version bookkeeping under Postgres.