Documentation
¶
Overview ¶
Package dbmigrate is a minimal, name-based schema migration engine. It is driver-agnostic: provide an *sql.DB and a Dialect implementation.
- Migrations are tracked by NAME, not by version/timestamp comparison. Two branches creating migrations with colliding or out-of-order timestamps is a non-issue: each file is checked for presence individually, never compared against a "last applied" pointer.
- Migrations applied together (one `up` run) share a BATCH number, mirroring Laravel's model, which is what makes `rollback`/`refresh` sensible in a name-keyed (non-strictly-ordered) system.
- Down migrations are optional per-file but required for `rollback` and `refresh`. `fresh` never needs them (it drops tables directly).
Index ¶
- func Quoted(d Dialect, s string) string
- type Dialect
- type HistoryEntry
- type Migration
- type Migrator
- func (m *Migrator) Baseline(ctx context.Context, migrations []Migration, lastFileToApply string) error
- func (m *Migrator) Fresh(ctx context.Context, migrations []Migration, schemaName string) error
- func (m *Migrator) History(ctx context.Context) ([]HistoryEntry, error)
- func (m *Migrator) Refresh(ctx context.Context, migrations []Migration) error
- func (m *Migrator) Reset(ctx context.Context, migrations []Migration) error
- func (m *Migrator) Rollback(ctx context.Context, migrations []Migration, steps int) error
- func (m *Migrator) Status(ctx context.Context, migrations []Migration) ([]string, error)
- func (m *Migrator) Up(ctx context.Context, migrations []Migration) error
- type OpenFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Dialect ¶
type Dialect interface {
// Name returns the driver name for logging/errors.
Name() string
// EnsureTableSQL returns the CREATE TABLE statement for the tracking table.
EnsureTableSQL(tableName string) string
// Lock serializes concurrent migrators. It should return a release
// function that is safe to call even if the context is cancelled.
Lock(ctx context.Context, db *sql.DB) (release func() error, err error)
// ListTablesSQL returns a query that yields one column of table names.
// Used by Fresh. May return an empty string if Fresh is unsupported.
ListTablesSQL() string
// DropTableSQL returns the DDL to drop the named table. schema may be
// empty for drivers that do not support schemas.
DropTableSQL(schema, table string) string
// TimestampDefault returns the default expression for applied_at,
// e.g. "now()" or "CURRENT_TIMESTAMP".
TimestampDefault() string
// QuoteIdentifier quotes a table/schema/column identifier.
QuoteIdentifier(s string) string
}
Dialect abstracts the database-specific bits of the migration engine. Adding a new driver means implementing this interface plus an Open() function that returns an *sql.DB.
type HistoryEntry ¶
HistoryEntry is one applied migration record.
type Migration ¶
type Migration struct {
Name string // base name, e.g. "2022_03_19_153546_add_users_table"
UpSQL string
DownSQL string // empty if no .down.sql file exists
HasDown bool
Checksum string // sha256 of UpSQL only
}
Migration represents one migration unit: an up file and an optional paired down file, identified by a shared base name.
func Load ¶
Load reads paired {name}.up.sql / {name}.down.sql files from dir inside fsys, sorted by name ascending.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator applies migrations from an fs.FS (e.g. embed.FS or os.DirFS) against a database connection, tracking progress by migration name + batch.
func (*Migrator) Baseline ¶
func (m *Migrator) Baseline(ctx context.Context, migrations []Migration, lastFileToApply string) error
Baseline records pending migrations as applied without executing their SQL. Use when adopting dbmigrate on a database whose schema already matches some or all migration files (for example after switching from golang-migrate).
If lastFileToApply is non-empty, only migrations up to and including that file (by load order) are baselined; later files stay pending for a normal Up. Accepts a migration base name, optional .up.sql/.down.sql suffix, or a path basename.
func (*Migrator) Fresh ¶
Fresh drops every user table directly (no down.sql needed) and then re-runs every migration from scratch as batch 1. Equivalent to Laravel's migrate:fresh. schemaName is ignored for drivers that do not support schemas.
func (*Migrator) History ¶
func (m *Migrator) History(ctx context.Context) ([]HistoryEntry, error)
History returns all applied migrations ordered by batch, then name.
func (*Migrator) Refresh ¶
Refresh rolls back every applied batch (requires down.sql on all of them) and then re-runs Up. Equivalent to Laravel's migrate:refresh.
func (*Migrator) Reset ¶
Reset rolls back every applied batch (requires down.sql on all of them) and does NOT re-apply anything afterward. Equivalent to Laravel's migrate:reset.
func (*Migrator) Rollback ¶
Rollback reverses the last `steps` batches (default 1), running each migration's down.sql in reverse name order within each batch. All targeted migrations must have a down.sql or Rollback fails before changing anything.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
dbmigrate
command
Command dbmigrate is a small CLI around the dbmigrate package.
|
Command dbmigrate is a small CLI around the dbmigrate package. |
|
Package postgres provides the PostgreSQL dialect for dbmigrate.
|
Package postgres provides the PostgreSQL dialect for dbmigrate. |
|
Package sqlite provides the SQLite dialect for dbmigrate.
|
Package sqlite provides the SQLite dialect for dbmigrate. |