Documentation
¶
Index ¶
- Constants
- func GetVersion(filename string) (int64, error)
- type Direction
- type Migration
- type MigrationContext
- type MigrationResult
- type OnSuccessHook
- type Options
- func (o Options) WithDataSourceName(s string) Options
- func (o Options) WithDir(s string) Options
- func (o Options) WithDriver(s string) Options
- func (o Options) WithExcludeFilenames(filenames ...string) Options
- func (o Options) WithFilesystem(f fs.FS) Options
- func (o Options) WithOutOfBand(b bool) Options
- func (o Options) WithTableName(s string) Options
- type Provider
- func (p *Provider) Close() error
- func (p *Provider) Current(ctx context.Context) (int64, error)
- func (p *Provider) Down(ctx context.Context) ([]*MigrationResult, error)
- func (p *Provider) DownTo(ctx context.Context, version int64) ([]*MigrationResult, error)
- func (p *Provider) List(ctx context.Context, all bool) ([]*Source, error)
- func (p *Provider) ListSources() []*Source
- func (p *Provider) Ping(ctx context.Context) error
- func (p *Provider) Stamp(ctx context.Context, version int64, force bool) error
- func (p *Provider) Up(ctx context.Context) ([]*MigrationResult, error)
- func (p *Provider) UpByOne(ctx context.Context) ([]*MigrationResult, error)
- func (p *Provider) UpTo(ctx context.Context, version int64) ([]*MigrationResult, error)
- func (p *Provider) Validate() error
- type Source
Constants ¶
const VERSION_HEAD = math.MaxInt64
When passed as version to Provider.Stamp, stamps to latest available version.
Variables ¶
This section is empty.
Functions ¶
func GetVersion ¶
GetVersion parses the version from the migration file name.
Types ¶
type Direction ¶
type Direction string
Direction is a direction in which a migration can be performed.
type Migration ¶
type Migration struct { Source // contains filtered or unexported fields }
Migration struct represents a migration.
type MigrationContext ¶
type MigrationContext struct {
// contains filtered or unexported fields
}
type MigrationResult ¶
type MigrationResult struct { // The source of the executed migration. Source *Source // The duration of the executed migration. Duration time.Duration // The direction of the executed migration. Direction Direction // The number of statements executed. NumStatements int // Error is only set if the migration failed. Error error }
MigrationResult is the result of a single migration operation.
func (*MigrationResult) String ¶
func (m *MigrationResult) String() string
String returns a string representation of the migration result.
type Options ¶
type Options struct { Dir string Driver string DataSourceName string TableName string Filesystem fs.FS OutOfBand bool ExcludeFilenames []string }
Options is a set of options to use when creating a new Provider.
Options can be created with [Options.DefaultOptions] and then modified with WithX methods. For example:
options := brant.DefaultOptions().WithDir("data/schema/migrations").WithVerbose(true) brant.NewProvider(slog.Default(), dialect.Postgres, options)
All options are documented in their respective WithX function:
- Options.WithDir
- Options.WithDriver
- Options.WithDataSourceName
- Options.WithTableName
- Options.WithFilesystem
- Options.WithOutOfBand
- Options.WithExcludeFilenames
func DefaultOptions ¶
func DefaultOptions() Options
func (Options) WithDataSourceName ¶
WithDataSourceName returns a new Options value with DataSourceName set to the given value.
DataSourceName is the driver-specific data source name to use for connecting to the database. See sql.Open.
func (Options) WithDir ¶
WithDir returns a new Options value with Dir set to the given value.
Dir is the directory containing the migration files. It defaults to "migrations".
func (Options) WithDriver ¶
WithDriver returns a new Options value with Driver set to the given value.
Driver is the SQL driver to use for connecting to the database. See sql.Open.
func (Options) WithExcludeFilenames ¶
WithExcludeFilenames returns a new Options value with ExcludeFilenames set to the given value.
ExcludeFilenames is a list of file or directory names to ignore when loading migrations. By default, migrations are loaded by scanning the migration directory recursively and loading all .sql files.
To exclude files or directories, add their base name to ExcludeFilenames. As an example, assuming the following directory contents: - migrations/old/0001_some_old_migration_thats_no_longer_relevant.sql - migrations/old/0002_some_other_migration_thats_no_longer_relevant.sql - migrations/000_template.sql - migrations/001_some_migration.sql
To ignore everything in the "old" directory, and the template for new migrations, use
options.WithExcludeFilenames([]string{"old", "000_template.sql"})
func (Options) WithFilesystem ¶
WithFilesystem returns a new Options value with Filesystem set to the given value.
Filesystem is the filesystem to use for loading migration files. By default, the host filesystem is used.
func (Options) WithOutOfBand ¶
WithOutOfBand returns a new Options value with OutOfBand set to the given value.
If OutOfBand is true, migrations will be run without changing the database version. The default is false.
func (Options) WithTableName ¶
WithTableName returns a new Options value with TableName set to the given value.
TableName is the name of the table used to track the database version. The default is "brant_db_version".
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is a brant migration provider.
func NewProvider ¶
NewProvider creates a new brant migration provider.
func (*Provider) Down ¶
func (p *Provider) Down(ctx context.Context) ([]*MigrationResult, error)
Down rolls back the most recent migration. Returns an empty list and no error if there are no migrations to roll back.
func (*Provider) DownTo ¶
DownTo rolls back all migrations down to but not including the specified version. Returns an empty list and no error if there are no migrations to roll back.
func (*Provider) ListSources ¶
func (*Provider) Stamp ¶
Stamp sets the database version to the provided value without running any migrations.
func (*Provider) Up ¶
func (p *Provider) Up(ctx context.Context) ([]*MigrationResult, error)
Up applies all available migrations. Returns an empty list and no error if there are no migrations to apply.
If error is not nil, the result is the list of all successful migrations plus the one that failed.
func (*Provider) UpByOne ¶
func (p *Provider) UpByOne(ctx context.Context) ([]*MigrationResult, error)
UpByOne applies the next available migration. Returns an empty list and no error if there are no migrations to apply.
type Source ¶
type Source struct { // Path to the migration file, relative to its [Base] Path string // Base is the base directory where all migrations are located Base string // Version is the version of the migration. Version int64 // FS is the filesystem that the source resides on FS fs.FS }
Source represents a single migration source file on disk.
Directories
¶
Path | Synopsis |
---|---|
The cli package provides an entrypoint for writing a `brant`-like CLI tool.
|
The cli package provides an entrypoint for writing a `brant`-like CLI tool. |
cmd
|
|
brant
command
|
|
Package database defines a generic Store interface for interacting with the database.
|
Package database defines a generic Store interface for interacting with the database. |
internal
|
|