Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoAppliedMigrations = errors.New("migrate: no applied migrations to rollback")
ErrNoAppliedMigrations indicates that rollback could not run because no migrations have been recorded in erm_schema_migrations.
Functions ¶
func Apply ¶
Apply discovers SQL migration files in fsys, executes unapplied migrations, and records them in erm_schema_migrations. All work occurs inside a single transaction protected by pg_advisory_xact_lock.
func ParseVersion ¶
ParseVersion extracts the version identifier from a migration filename. The identifier is derived from the portion of the name that precedes the first "__", "_", or "-" separator, falling back to the stem when none are present.
Types ¶
type FileMigration ¶
type FileMigration struct {
// Version is the parsed migration identifier recorded in erm_schema_migrations.
Version string
// Name is the base filename (e.g. 0001_init.sql).
Name string
// Path is the path relative to the root of the provided fs.FS.
Path string
// Type identifies whether the migration is an up (forward) or down (rollback) script.
Type MigrationType
}
FileMigration represents a single SQL migration discovered on disk.
type MigrationType ¶
type MigrationType int
MigrationType differentiates forward (up) migrations from rollback (down) scripts.
const ( // MigrationTypeUp represents a forward migration that applies schema changes. MigrationTypeUp MigrationType = iota // MigrationTypeDown represents a rollback script that reverts a previously applied migration. MigrationTypeDown )
func (MigrationType) String ¶
func (mt MigrationType) String() string
String implements fmt.Stringer.
type Option ¶
type Option func(*Options)
Option mutates Options.
func WithAdvisoryLock ¶
WithAdvisoryLock overrides the advisory lock identifier used to serialize migration runs.
func WithBatchSize ¶
WithBatchSize limits the number of unapplied migrations executed per Apply invocation.
func WithDirectory ¶
WithDirectory instructs Apply to look for migration files under dir.
type Options ¶
type Options struct {
// Directory indicates the root within the supplied fs.FS that contains the
// migration files. Defaults to "migrations".
Directory string
// BatchSize controls how many unapplied migrations are executed in a single
// invocation. Zero means all pending migrations run in one transaction.
BatchSize int
// AdvisoryLockID overrides the pg_advisory_xact_lock key that guards
// migrations. Defaults to a deterministic key derived from the project
// prefix.
AdvisoryLockID int64
}
Options configures how migrations are discovered and applied.
type PlanResult ¶
type PlanResult struct {
// Pending lists unapplied up migrations in execution order.
Pending []FileMigration
// Applied lists versions recorded in erm_schema_migrations.
Applied []string
}
PlanResult summarises the outcome of inspecting migrations without executing them.
type SchemaDriftError ¶
type SchemaDriftError struct {
Missing []string
}
SchemaDriftError signals that the database has recorded migrations whose SQL files are no longer present in the migrations directory.
func (SchemaDriftError) Error ¶
func (e SchemaDriftError) Error() string