Documentation
¶
Overview ¶
Package migrate provides a minimal database migration runner. Migrations are read from one or more named Layer instances (each wrapping an fs.FS) and applied in global lexicographic order by filename. Using a date-based filename prefix (e.g. 20060102_description.sql) across all layers gives a natural chronological ordering regardless of which layer a migration comes from.
Applied versions are tracked as "layername/filename" (without .sql) so migrations from different layers with the same filename do not collide in the tracking table.
Database access and dialect differences are abstracted through the DB interface. Use NewDriver to wrap a *sql.DB with a Dialect.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrConflict = errors.New("conflict")
ErrConflict is returned when an insert or update violates a uniqueness constraint.
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when a queried record does not exist.
Functions ¶
Types ¶
type DB ¶
type DB interface {
Exec(ctx context.Context, query string, args ...any) error
Query(ctx context.Context, query string, args ...any) (Rows, error)
Begin(ctx context.Context) (Tx, error)
CreateTableSQL(table string) string
InsertVersionSQL(table string) string
QueryVersionsSQL(table string) string
}
DB abstracts database operations and dialect-specific SQL. Use NewDriver to create one from a *sql.DB and Dialect.
type Dialect ¶
type Dialect interface {
CreateTableSQL(table string) string
InsertVersionSQL(table string) string
QueryVersionsSQL(table string) string
}
Dialect abstracts dialect-specific SQL differences.
type Layer ¶
Layer associates a name with an fs.FS containing migration files. The name is used to namespace version strings in the tracking table (e.g. "auth/20060102_create_users"), so migrations from different layers with the same filename do not collide.
type MigrateResult ¶
type MigrateResult struct {
Applied []string // versions applied in this run
Total int // total migrations (applied + already present)
}
MigrateResult contains information about a migration run.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator runs migrations against a database.
type Option ¶
type Option func(*Migrator)
Option configures a Migrator.
func WithDirectory ¶
WithDirectory sets the subdirectory within each Layer's FS to read migration files from (default: "migrations"). Use "." to read from the root.
func WithLayers ¶
WithLayers appends one or more Layer values to the migrator. All migrations from all layers are sorted globally by filename before being applied, so date-prefixed filenames (e.g. 20060102_name.sql) produce a correct chronological order across layers.
type PostgresDialect ¶
type PostgresDialect struct{}
PostgresDialect implements Dialect for PostgreSQL databases.
func (PostgresDialect) CreateTableSQL ¶
func (PostgresDialect) CreateTableSQL(table string) string
func (PostgresDialect) InsertVersionSQL ¶
func (PostgresDialect) InsertVersionSQL(table string) string
func (PostgresDialect) QueryVersionsSQL ¶
func (PostgresDialect) QueryVersionsSQL(table string) string
type SQLiteDialect ¶
type SQLiteDialect struct{}
SQLiteDialect implements Dialect for SQLite databases.
func (SQLiteDialect) CreateTableSQL ¶
func (SQLiteDialect) CreateTableSQL(table string) string
func (SQLiteDialect) InsertVersionSQL ¶
func (SQLiteDialect) InsertVersionSQL(table string) string
func (SQLiteDialect) QueryVersionsSQL ¶
func (SQLiteDialect) QueryVersionsSQL(table string) string