Documentation ¶
Overview ¶
Package burrow allows for no-frills database migrations.
Index ¶
Constants ¶
const ( // DialctNone is the default, and indicates that burrow will attempt to // detect the dialect automatically. DialectNone dialect = iota // DialectMySQL enables the use of GET_LOCK() and RELEASE_LOCK() in addition // to row-level locking on the migrations table, since MySQL doesn't // properly support normal transactions for table creation and alternation // queries. DialectMySQL // DialectPostgres enables Postgres-specific features. DialectPostgres // DialectPgx enables Postgres-specific features using the pgx driver, with // COPY support. DialectPgx // DialectSQLite enables SQLite-specific features. DialectSQLite )
Supported SQL Dialects. Pass one of these as an argument to New to enable dialect-specific features.
const DefaultStateTable = "migrations"
DefaultStateTable is the default name of the table where migration state is stored. It is created the first time Migrator.Migrate is called.
const ErrUnimplemented = customError("unimplemented")
ErrUnimplemented may be returned by [Migration.PreRun], [Migration.Run], or [Migration.GetBody] to indicate that the respective method is not implemented, and that the migration should fall back to other methods.
Variables ¶
This section is empty.
Functions ¶
func ValidateSources ¶ added in v0.12.0
ValidateSources does minimal validation of the sources. Notably, it checks that the migration IDs are unique across all sources. There is normally no need to call this function, as Migrator.Migrate does the same checks, but it can be useful to do these checks separataely, such as in a build pipeline test.
Types ¶
type MemorySource ¶
MemorySource is a Source implemented by an in-memory map
type Migration ¶
type Migration struct { Version uint // Name is a descriptive name, such as full path to the migration, used for // debugging/informational purposes. Name string // Body is expected to return the SQL to execute for the migration. Body io.Reader // PreRun is an optional function to run before executing [Body] or [Run]. // It is primarily intended for migrations that need to download large data // sets (i.e. from a cloud storage bucket) or perform other expensive // operations before running the migration. By putting such work in PreRun, // it may be run concurrently with other migrations or other PreRun // functions. If PreRun returns an error, the migration itself will fail. PreRun func(context.Context) error // GetBody is an optional function to get the body of the migration. If set, // [Body] is ignored. GetBody func(context.Context) (io.Reader, error) // Run is an optional function to run instead of executing [Body] as SQL. If // set, [Body] is ignored. Run func(context.Context, *sql.Tx) error }
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator is an instance that applies migrations to a database. A Migrator instance is stateful, and should only be used once after initialization. If you need to apply the same migration to multiple databases, or to the same database multiple times, create one Migrator instance for migration run.
func (*Migrator) AddSource ¶
AddSource adds a source to m. You may provide multiple sources, but the migration IDs returned must be unique across all sources.
func (*Migrator) Migrate ¶
Migrate performs the actual migration. It returns the number of successful migration scripts applied.
Migrate will create the state table if it does not exist. It will also lock the state table for each migration. If Migrate returns a non-nil error, it means that all migrations up to that point were applied successfully.
type Option ¶ added in v0.2.0
type Option interface {
// contains filtered or unexported methods
}
Option is a functional that can be used to configure a Burrow instance or source.
func Exclude ¶ added in v0.2.0
Exclude returns an Option that excludes migrations matching the given regular expression pattern.
func Executable ¶ added in v0.11.0
Executable returns an Option that matches files which should be executed directly, rather than interpreted as SQL.
Example:
source, err := FileSource("migrations", Executable(`\.sh$`))
Binaries executed this way will have certain environment variables set, depending on the database driver in use.
For SQLite:
- SQLITE_FILE is set to the path of the database file. (empty for in-memory databases)
func WithLogger ¶ added in v0.5.0
WithLogger allows setting a custom logger for logs.
A single INFO-level log is emitted for each migration, indicating whether it was run, or skipped. A failed migration logs an error, as well as returning the error value. All other information is logged at DEBUG level.
func WithStateTable ¶ added in v0.6.0
WithStateTable allows specifying a custom state table name.
type Source ¶
type Source interface { // Index returns a list of all available migration versions. Index(context.Context) ([]uint, error) // Migration is expected to return the numbered migration. Migration(_ context.Context, id uint) (*Migration, error) }
Source is the interface for a migration source.
func FSSource ¶
FSSource returns a Source implemented by a directory of files. See FileSource for full description of behavior.