migrate

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 11 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 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

Constants

This section is empty.

Variables

View Source
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

func MongoCollectionExists(ctx context.Context, db *mongo.Database, name string) (bool, error)

MongoCollectionExists reports whether a collection name is present.

func PgxTableExists

func PgxTableExists(ctx context.Context, db PgxDB, table string) (bool, error)

PgxTableExists reports whether a relation exists in the public schema.

func TableExists

func TableExists(ctx context.Context, db DB, d Dialect, table string) (bool, error)

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 Dialect

type Dialect int

Dialect selects CREATE TABLE / time literals for schema_migrations.

const (
	SQLite Dialect = iota
	Postgres
	MySQL
)

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 Mongo

type Mongo struct {
	DB *mongo.Database
}

Mongo is a Bookkeeper backed by a MongoDB database.

func NewMongo

func NewMongo(db *mongo.Database) *Mongo

NewMongo returns a Bookkeeper for MongoDB.

func (*Mongo) Current

func (m *Mongo) Current(ctx context.Context) (int, error)

func (*Mongo) Delete

func (m *Mongo) Delete(ctx context.Context, version int) error

func (*Mongo) Ensure

func (m *Mongo) Ensure(ctx context.Context) error

func (*Mongo) Insert

func (m *Mongo) Insert(ctx context.Context, version int, name string) error

type Pgx

type Pgx struct {
	DB        PgxDB
	TableName string
}

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 NewPgx

func NewPgx(db PgxDB) *Pgx

NewPgx returns a Bookkeeper for Postgres writing to schema_migrations.

func NewPgxTable

func NewPgxTable(db PgxDB, tableName string) *Pgx

NewPgxTable returns a Bookkeeper writing to tableName. tableName must be a fixed safe identifier from code (never request/user input).

func (*Pgx) Current

func (p *Pgx) Current(ctx context.Context) (int, error)

func (*Pgx) Delete

func (p *Pgx) Delete(ctx context.Context, version int) error

func (*Pgx) Ensure

func (p *Pgx) Ensure(ctx context.Context) error

func (*Pgx) Insert

func (p *Pgx) Insert(ctx context.Context, version int, name string) error

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.

type SQL

type SQL struct {
	DB      DB
	Dialect Dialect
}

SQL is a Bookkeeper backed by database/sql (SQLite and MySQL).

func NewSQL

func NewSQL(db DB, d Dialect) *SQL

NewSQL returns a Bookkeeper for SQLite or MySQL.

func (*SQL) Current

func (s *SQL) Current(ctx context.Context) (int, error)

func (*SQL) Delete

func (s *SQL) Delete(ctx context.Context, version int) error

func (*SQL) Ensure

func (s *SQL) Ensure(ctx context.Context) error

func (*SQL) Insert

func (s *SQL) Insert(ctx context.Context, version int, name string) error

Jump to

Keyboard shortcuts

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