Documentation
¶
Overview ¶
Package migrate provides versioned database migrations for SQL backends.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AppliedMigration ¶
type AppliedMigration struct {
Version int `json:"version"`
Description string `json:"description"`
AppliedAt time.Time `json:"applied_at"`
}
AppliedMigration records a migration that has been applied.
type Dialect ¶ added in v0.4.0
type Dialect int
Dialect identifies the SQL dialect a Migrator targets. It controls placeholder syntax (`?` vs `$N`) for the migrator's own bookkeeping statements and whether a cross-process advisory lock is available.
const ( // DialectSQLite uses `?` placeholders and a no-op advisory lock (SQLite is // single-writer, so concurrent migrators cannot corrupt each other). DialectSQLite Dialect = iota // DialectPostgres uses `$N` placeholders and a pg_advisory_lock so multiple // processes migrating the same database serialize safely. DialectPostgres )
type Migration ¶
type Migration struct {
Version int
Description string
Up string // SQL to apply
Down string // SQL to roll back
}
Migration represents a single versioned migration.
type MigrationStatus ¶
type MigrationStatus struct {
CurrentVersion int `json:"current_version"`
Applied []AppliedMigration `json:"applied"`
Pending []Migration `json:"pending"`
}
MigrationStatus reports the current migration version plus applied and pending migrations.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator manages versioned migrations for a SQL database.
func New ¶
New creates a new Migrator for the given database connection. Without options it targets SQLite; pass WithDialect(DialectPostgres) for PostgreSQL.
func (*Migrator) Migrate ¶
Migrate applies all pending migrations. Under Postgres it holds a pg_advisory_lock for the duration so concurrent migrators serialize.
func (*Migrator) Rollback ¶
Rollback reverts the last applied migration. Like Migrate it pins a single connection, holds the advisory lock for the duration (Postgres), and runs the Down SQL together with the version-table delete inside one transaction, so a partial failure never leaves the _migrations table inconsistent.