Documentation
¶
Index ¶
- Constants
- Variables
- func AddMigration(up func(*sql.Tx) error, down func(*sql.Tx) error)
- func AddNamedMigration(filename string, up func(*sql.Tx) error, down func(*sql.Tx) error)
- func AreEventsEqual(e1, e2 Eventer) bool
- func BaseDir(dir string) func(p *Provider)
- func Create(db *sql.DB, dir, name, migrationType string) error
- func CreateWithTemplate(db *sql.DB, dir string, tmpl *template.Template, name, migrationType string) error
- func Dialect(dialect string) func(p *Provider)
- func DialectObject(dialect SQLDialect) func(p *Provider)
- func Down(db *sql.DB, dir string, opts ...OptionsFunc) error
- func DownTo(db *sql.DB, dir string, version int64, opts ...OptionsFunc) error
- func EnsureDBVersion(db *sql.DB) (int64, error)
- func Filesystem(baseFS fs.FS) func(p *Provider)
- func Fix(dir string) error
- func GetDBVersion(db *sql.DB) (int64, error)
- func Log(log Logger) func(p *Provider)
- func NumericComponent(name string) (int64, error)
- func OpenDBWithDriver(driver string, dbstring string) (*sql.DB, error)
- func ProviderPackage(packageName, providerVar string) func(p *Provider)
- func Redo(db *sql.DB, dir string, opts ...OptionsFunc) error
- func Reset(db *sql.DB, dir string, opts ...OptionsFunc) error
- func Run(command string, db *sql.DB, dir string, args ...string) error
- func RunWithOptions(command string, db *sql.DB, dir string, args []string, options ...OptionsFunc) error
- func SequentialVersion(versionTemplate string) func(p *Provider)
- func SetBaseFS(fsys fs.FS)
- func SetDialect(d string) error
- func SetLogger(l Logger)
- func SetSequential(s bool)
- func SetTableName(n string)
- func SetVerbose(v bool)
- func Status(db *sql.DB, dir string, opts ...OptionsFunc) error
- func TableName() string
- func Tablename(tablename string) func(p *Provider)
- func TimeFunction(fn func() time.Time) func(p *Provider)
- func TimestampFormat(format string) func(p *Provider)
- func TimestampVersion(p *Provider)
- func Up(db *sql.DB, dir string, opts ...OptionsFunc) error
- func UpByOne(db *sql.DB, dir string, opts ...OptionsFunc) error
- func UpTo(db *sql.DB, dir string, version int64, opts ...OptionsFunc) error
- func Verbose(b bool) func(p *Provider)
- func Version(db *sql.DB, dir string, opts ...OptionsFunc) error
- type BaseDialect
- type ClickHouseDialect
- type ErrMigrationSQLParse
- type ErrTimestampVersionsExist
- type ErrUnknownExtension
- type ErrUnwrap
- type Event
- type Eventer
- type Logger
- type Migration
- type MigrationRecord
- type Migrations
- func (ms Migrations) Current(current int64) (*Migration, error)
- func (ms Migrations) Last() (*Migration, error)
- func (ms Migrations) Len() int
- func (ms Migrations) Less(i, j int) bool
- func (ms Migrations) Next(current int64) (*Migration, error)
- func (ms Migrations) NumberOfMigrations(current int64, down bool) int
- func (ms Migrations) NumberOfMigrationsTo(from int64, to int64) int
- func (ms Migrations) Previous(current int64) (*Migration, error)
- func (ms Migrations) String() string
- func (ms Migrations) Swap(i, j int)
- type MissingMigrations
- type MissingMigrationsErr
- type MySQLDialect
- type OptionsFunc
- type PostgresDialect
- type Provider
- func (p *Provider) AddMigration(up func(*sql.Tx) error, down func(*sql.Tx) error)
- func (p *Provider) AddNamedMigration(filename string, up func(*sql.Tx) error, down func(*sql.Tx) error)
- func (p *Provider) BaseDir(dir string) string
- func (p *Provider) CollectMigrations(dirPath string, current, target int64) (Migrations, error)
- func (p *Provider) Create(db *sql.DB, dir, name, migrationType string) error
- func (p *Provider) CreateWithTemplate(_ *sql.DB, dir string, tmpl *template.Template, name, migrationType string) error
- func (p *Provider) Dialect() SQLDialect
- func (p *Provider) Down(db *sql.DB, dir string, opts ...OptionsFunc) error
- func (p *Provider) DownTo(db *sql.DB, dir string, version int64, opts ...OptionsFunc) error
- func (p *Provider) EnsureDBVersion(db *sql.DB) (int64, error)
- func (p *Provider) Fix(dir string) error
- func (p *Provider) GetDBVersion(db *sql.DB) (int64, error)
- func (p *Provider) GetVersions(db *sql.DB, dir string, opts ...OptionsFunc) (migrationVersion int64, dbVersion int64, err error)
- func (p *Provider) OpenDBWithDriver(driver string, dbstring string) (*sql.DB, error)
- func (p *Provider) Redo(db *sql.DB, dir string, opts ...OptionsFunc) error
- func (p *Provider) Reset(db *sql.DB, dir string, opts ...OptionsFunc) error
- func (p *Provider) SetBaseFS(fsys fs.FS)
- func (p *Provider) SetDialect(d string) error
- func (p *Provider) SetLogger(l Logger)
- func (p *Provider) SetSequential(s bool)
- func (p *Provider) SetTableName(n string)
- func (p *Provider) SetVerbose(v bool)
- func (p *Provider) Status(db *sql.DB, dir string, opts ...OptionsFunc) (err error)
- func (p *Provider) TableName() string
- func (p *Provider) Up(db *sql.DB, dir string, opts ...OptionsFunc) error
- func (p *Provider) UpByOne(db *sql.DB, dir string, opts ...OptionsFunc) error
- func (p *Provider) UpTo(db *sql.DB, dir string, version int64, opts ...OptionsFunc) (err error)
- func (p *Provider) Verify(dir string) VerifyStatus
- func (p *Provider) Version(db *sql.DB, dir string, opts ...OptionsFunc) error
- type RedshiftDialect
- type SQLDialect
- type SqlServerDialect
- type Sqlite3Dialect
- type StatusEvent
- type TiDBDialect
- type VerifyStatus
- type VersionApplyEvent
- type VersionCountEvent
Constants ¶
const ( DialectPostgres = "postgres" DialectSQLite3 = "sqlite3" DialectMySQL = "mysql" DialectMSSQL = "mssql" DialectRedShit = "redshift" DialectTiDB = "tidb" DialectClickHouse = "clickhouse" )
const ( // VerifyStatusErr indicates an error occurred of some sort. If this is the only bit set, // then it was a generic error dealing with accessing the migrations. The Error field of // VerifyStatus will have the error or errors VerifyStatusErr = 1 << iota // VerifyStatusTsMigrations indicates that there are timestamp-based migrations VerifyStatusTsMigrations // VerifyStatusTplSql indicates that there was an error loading, parsing, or executing sql templates. // the Error field will contain an error list with the error for each template that errored out. VerifyStatusTplSql = VerifyStatusErr | (1 << iota) )
const VERSION = "v3.2.0"
Deprecated: VERSION will no longer be supported in v4.
const (
// VerifyStatusOK indicates that no issue were found, this includes not having any timestamp-based migrations.
VerifyStatusOK = 0
)
Variables ¶
var ( // ErrNoCurrentVersion when a current migration version is not found. ErrNoCurrentVersion = errors.New("no current version found") // ErrNoNextVersion when the next migration version is not found. ErrNoNextVersion = errors.New("no next version found") // MaxVersion is the maximum allowed version. MaxVersion int64 = 9223372036854775807 // max(int64) )
Functions ¶
func AddMigration ¶
AddMigration adds a migration.
func AddNamedMigration ¶
AddNamedMigration : Add a named migration.
func AreEventsEqual ¶ added in v3.6.3
func BaseDir ¶
BaseDir will set the base directory, if an empty string is passed the directory of the package that called BaseDir is used instead this is only useful for Create* and Fix functions
func CreateWithTemplate ¶
func CreateWithTemplate(db *sql.DB, dir string, tmpl *template.Template, name, migrationType string) error
CreateWithTemplate writes a new blank migration file.
func DialectObject ¶
func DialectObject(dialect SQLDialect) func(p *Provider)
func Down ¶
func Down(db *sql.DB, dir string, opts ...OptionsFunc) error
Down rolls back a single migration from the current version.
func EnsureDBVersion ¶
EnsureDBVersion retrieves the current version for this DB. Create and initialize the DB version table if it doesn't exist.
func Filesystem ¶
func GetDBVersion ¶
GetDBVersion is an alias for EnsureDBVersion, but returns -1 in error.
func NumericComponent ¶
NumericComponent looks for migration scripts with names in the form: XXX_descriptive_name.ext where XXX specifies the version number and ext specifies the type of migration
func OpenDBWithDriver ¶
OpenDBWithDriver creates a connection to a database, and modifies goose internals to be compatible with the supplied driver by calling SetDialect.
func ProviderPackage ¶
ProviderPackage sets the packageName and providerVar used in templates
func Redo ¶
func Redo(db *sql.DB, dir string, opts ...OptionsFunc) error
Redo rolls back the most recently applied migration, then runs it again.
func Reset ¶
func Reset(db *sql.DB, dir string, opts ...OptionsFunc) error
Reset rolls back all migrations
func RunWithOptions ¶
func RunWithOptions(command string, db *sql.DB, dir string, args []string, options ...OptionsFunc) error
RunWithOptions runs a goose command with options.
func SequentialVersion ¶
SequentialVersion make the provider use sequential versioning
func SetBaseFS ¶
SetBaseFS sets a base FS to discover migrations. It can be used with 'embed' package. Calling with 'nil' argument leads to default behaviour: discovering migrations from os filesystem. Note that modifying operations like Create will use os filesystem anyway.
func SetSequential ¶
func SetSequential(s bool)
SetSequential set whether to use sequential versioning instead of timestamp based versioning
func Status ¶
func Status(db *sql.DB, dir string, opts ...OptionsFunc) error
Status prints the status of all migrations.
func TimeFunction ¶
TimeFunction sets the time function used to get the time for timestamp numbers defaults to time.Now
func TimestampFormat ¶
TimestampFormat sets the timestamp format for the provider
func TimestampVersion ¶
func TimestampVersion(p *Provider)
TimestampVersion make the provider use sequential versioning
func Up ¶
func Up(db *sql.DB, dir string, opts ...OptionsFunc) error
Up applies all available migrations.
func UpByOne ¶
func UpByOne(db *sql.DB, dir string, opts ...OptionsFunc) error
UpByOne migrates up by a single version.
Types ¶
type BaseDialect ¶
type BaseDialect struct {
TableName string
}
BaseDialect struct.
func (*BaseDialect) SetTableName ¶
func (bd *BaseDialect) SetTableName(name string)
type ErrMigrationSQLParse ¶ added in v3.6.4
func (ErrMigrationSQLParse) Error ¶ added in v3.6.4
func (err ErrMigrationSQLParse) Error() string
type ErrTimestampVersionsExist ¶ added in v3.6.6
type ErrTimestampVersionsExist struct {
Migrations Migrations
}
func (ErrTimestampVersionsExist) Error ¶ added in v3.6.6
func (err ErrTimestampVersionsExist) Error() string
type ErrUnknownExtension ¶ added in v3.6.4
type ErrUnknownExtension struct {
Extension string
}
func (ErrUnknownExtension) Error ¶ added in v3.6.4
func (err ErrUnknownExtension) Error() string
type Logger ¶
type Logger interface { Fatal(v ...interface{}) Fatalf(format string, v ...interface{}) Print(v ...interface{}) Println(v ...interface{}) Printf(format string, v ...interface{}) }
Logger is standard logger interface
type Migration ¶
type Migration struct { Version int64 Next int64 // next version, or -1 if none Previous int64 // previous version, -1 if none Source string // path to .sql script or go file Registered bool UpFn func(*sql.Tx) error // Up go migration function DownFn func(*sql.Tx) error // Down go migration function // contains filtered or unexported fields }
Migration struct.
func (*Migration) DownWithProvider ¶
func (*Migration) IsTimestamp ¶ added in v3.6.6
IsTimestamp returns weather the migration version can be considered to be a timestamp version, v.s. a Seq version. This means that the user can never have more than 19700101000000 migrations
type MigrationRecord ¶
type MigrationRecord struct { VersionID int64 TStamp time.Time IsApplied bool // was this a result of up() or down() }
MigrationRecord struct.
type Migrations ¶
type Migrations []*Migration
Migrations slice.
func CollectMigrations ¶
func CollectMigrations(dirPath string, current, target int64) (Migrations, error)
CollectMigrations returns all the valid looking migration scripts in the migrations folder and go func registry, and key them by version.
func (Migrations) Current ¶
func (ms Migrations) Current(current int64) (*Migration, error)
Current gets the current migration.
func (Migrations) Last ¶
func (ms Migrations) Last() (*Migration, error)
Last gets the last migration.
func (Migrations) Less ¶
func (ms Migrations) Less(i, j int) bool
func (Migrations) Next ¶
func (ms Migrations) Next(current int64) (*Migration, error)
Next gets the next migration.
func (Migrations) NumberOfMigrations ¶ added in v3.6.2
func (ms Migrations) NumberOfMigrations(current int64, down bool) int
NumberOfMigrations returns the number of migrations that could be applied from the provided version number if the current version number is not found then a -1 is returned if -1 is passed in for the current version then the number of migrations in the set is returned down indicates the direction of migration. If down is true, the current is used to report the number of migrations to get to back to the start of the migration sequence. Otherwise, it's the number of migrations to get to the end of the migration sequence.
func (Migrations) NumberOfMigrationsTo ¶ added in v3.6.2
func (ms Migrations) NumberOfMigrationsTo(from int64, to int64) int
NumberOfMigrationsTo returns the number of migrations that could be applied from the starting migration to the given to migration. If the `to` migration does not exist it will be the migration that would be the next on in line of the migrations depending on if the trend is going up or down, e.g. if `to - from > 0` then it's going up if `to - from < 0` then it's going down. If `to - from == 0`, zero is returned. if from migration is not found this function will panic
func (Migrations) Previous ¶
func (ms Migrations) Previous(current int64) (*Migration, error)
Previous : Get the previous migration.
func (Migrations) String ¶
func (ms Migrations) String() string
func (Migrations) Swap ¶
func (ms Migrations) Swap(i, j int)
type MissingMigrations ¶ added in v3.6.2
type MissingMigrationsErr ¶ added in v3.6.2
type MissingMigrationsErr struct {
MissingMigrations []MissingMigrations
}
func MissingMigrationsErrFromMigrations ¶ added in v3.6.2
func MissingMigrationsErrFromMigrations(migrations Migrations) (err MissingMigrationsErr)
func (MissingMigrationsErr) Error ¶ added in v3.6.2
func (err MissingMigrationsErr) Error() string
type OptionsFunc ¶
type OptionsFunc func(o *options)
func WithAllowMissing ¶
func WithAllowMissing() OptionsFunc
func WithEvents ¶ added in v3.6.2
func WithEvents(events chan<- Eventer, DontCloseChannelOnComplete bool) OptionsFunc
WithEvents will publish events to the given channel, and close the channel upon the completion of the function.
func WithNoOutput ¶ added in v3.6.2
func WithNoOutput() OptionsFunc
WithNoOutput will suppress the output of the function
func WithNoVersioning ¶
func WithNoVersioning() OptionsFunc
func WithOnlySequentialVersions ¶ added in v3.6.6
func WithOnlySequentialVersions() OptionsFunc
WithOnlySequentialVersions states that Up should error if there are any timestamp based version files. Up will only run if the only version files left are sequential.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
func NewProvider ¶
func NewProvider(options ...providerOptions) *Provider
func (*Provider) AddMigration ¶
func (*Provider) AddNamedMigration ¶
func (p *Provider) AddNamedMigration(filename string, up func(*sql.Tx) error, down func(*sql.Tx) error)
AddNamedMigration : Add a named migration.
func (*Provider) CollectMigrations ¶
func (p *Provider) CollectMigrations(dirPath string, current, target int64) (Migrations, error)
func (*Provider) CreateWithTemplate ¶
func (p *Provider) CreateWithTemplate(_ *sql.DB, dir string, tmpl *template.Template, name, migrationType string) error
CreateWithTemplate writes a new blank migration file.
func (*Provider) Dialect ¶
func (p *Provider) Dialect() SQLDialect
Dialect returns the SQLDialect of the provider
func (*Provider) EnsureDBVersion ¶
EnsureDBVersion retrieves the current version for this DB. Create and initialize the DB version table if it doesn't exist.
func (*Provider) GetDBVersion ¶
GetDBVersion is an alias for EnsureDBVersion, but returns -1 in error.
func (*Provider) GetVersions ¶ added in v3.6.2
func (p *Provider) GetVersions(db *sql.DB, dir string, opts ...OptionsFunc) (migrationVersion int64, dbVersion int64, err error)
GetVersion will return the current version of the migration, and database version, or -1, -1 if not found or if there is an error If db is nil, or the option.noVersioning is specificed, then the dbVersion will be -1.
func (*Provider) OpenDBWithDriver ¶
OpenDBWithDriver creates a connection to a database, and modifies goose internals to be compatible with the supplied driver by calling SetDialect.
func (*Provider) SetBaseFS ¶
SetBaseFS sets a base FS to discover migrations. It can be used with the `embed` package. Calling with `nil` argument leads to the default behavior: discovering migrations from the os filesystem.
Note: that modifying operations like Create will use os filesystem anyway.
func (*Provider) SetDialect ¶
SetDialect sets the SQLDialect
func (*Provider) SetSequential ¶
SetSequential set's whether to use sequential versioning instead of timestamp based versioning
func (*Provider) SetTableName ¶
SetTableName set goose db version table name
func (*Provider) SetVerbose ¶
SetVerbose set the goose verbosity mode
func (*Provider) Verify ¶ added in v3.6.4
func (p *Provider) Verify(dir string) VerifyStatus
Verify will check the migration directory to see if there are any errors, or other issues. It will return a VerifyStatus with any errors it found
type SQLDialect ¶
type SQLDialect interface { SetTableName(name string) // set table name to use for SQL generation // contains filtered or unexported methods }
SQLDialect abstracts the details of specific SQL dialects for goose's few SQL specific statements
func SelectDialect ¶
func SelectDialect(tableName, d string) (SQLDialect, error)
type StatusEvent ¶ added in v3.6.2
type StatusEvent struct { *Event // Source is the full path of the source file, use `Script` to get the name Source string // Version is the version number, it should be -1 if not set Version int64 Versioned bool // If not zero then the time the migration was applied at AppliedAt time.Time }
StatusEvent is a version number, and source of a migration, and whether it has been applied
func (StatusEvent) AppliedString ¶ added in v3.6.2
func (se StatusEvent) AppliedString() string
func (StatusEvent) IsEqual ¶ added in v3.6.3
func (se StatusEvent) IsEqual(e Eventer) bool
func (StatusEvent) Script ¶ added in v3.6.2
func (se StatusEvent) Script() string
func (StatusEvent) String ¶ added in v3.6.2
func (se StatusEvent) String() string
func (StatusEvent) VersionedString ¶ added in v3.6.3
func (se StatusEvent) VersionedString() string
type VerifyStatus ¶ added in v3.6.4
func Verify ¶ added in v3.6.4
func Verify(dir string) VerifyStatus
Verify will check the migration directory to see if there are any errors, or other issues. It will return a VerifyStatus with any errors it found
func (VerifyStatus) HasTsMigrations ¶ added in v3.6.4
func (vs VerifyStatus) HasTsMigrations() bool
func (VerifyStatus) Ok ¶ added in v3.6.4
func (vs VerifyStatus) Ok() bool
type VersionApplyEvent ¶ added in v3.6.2
type VersionApplyEvent struct { *Event From int64 FromSource string To int64 ToSource string ApplyAT time.Time Missing bool Applied bool Versioned bool Down bool }
VersionApplyEvent usually comes in pairs unless there is an error, the first event (with applied set to false) will be emmited before the version is applied to the database, and the applied version after the new version has been applied.
func (VersionApplyEvent) IsEqual ¶ added in v3.6.3
func (e VersionApplyEvent) IsEqual(o Eventer) bool