Documentation
¶
Overview ¶
Package migrate provides a minimal database migration runner. Migrations are read from an fs.FS (typically embed.FS) and applied in lexicographic order. Applied versions are tracked in a configurable table.
Database access and dialect differences are abstracted through the DB interface. Use NewDriver to wrap a *sql.DB with a Dialect, or the pgxmigrate sub-package for native pgx support.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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, or implement this interface for other database libraries such as pgx.
type Dialect ¶
type Dialect interface {
CreateTableSQL(table string) string
InsertVersionSQL(table string) string
QueryVersionsSQL(table string) string
}
Dialect abstracts dialect-specific SQL differences.
type MigrateResult ¶
type MigrateResult struct {
Applied []string // versions that were 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.
func New ¶
New creates a Migrator. The fs should contain .sql files named with a sortable prefix (e.g. 0001_initial.sql, 0002_add_users.sql).
type Option ¶
type Option func(*Migrator)
Option configures a Migrator.
func WithDirectory ¶
WithDirectory sets the subdirectory within the fs.FS to read migration files from (default: "migrations"). Use "." to read from the root.
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
Directories
¶
| Path | Synopsis |
|---|---|
|
Package pgxmigrate provides a migrate.DB adapter for pgx.
|
Package pgxmigrate provides a migrate.DB adapter for pgx. |