Involved Source Fileslog.go
Package migrate reads migrations from sources and runs them against databases.
Sources are defined by the `source.Driver` and databases by the `database.Driver`
interface. The driver interfaces are kept "dump", all migration logic is kept
in this package.
migration.goutil.go
Package-Level Type Names (total 6, all are exported)
ErrShortLimit is an error returned when not enough migrations
can be returned by a source for a given limit.
Shortuint
Error implements the error interface.
T : error
Logger is an interface so you can pass in your own
logging implementation.
Printf is like fmt.Printf
Verbose should return true when verbose logging output is wanted
GracefulStop accepts `true` and will stop executing migrations
as soon as possible at a safe break point, so that the database
is not corrupted.
LockTimeout defaults to DefaultLockTimeout,
but can be set per Migrate instance.
Log accepts a Logger interface
PrefetchMigrations defaults to DefaultPrefetchMigrations,
but can be set per Migrate instance.
databaseDrvdatabase.DriverdatabaseNamestringisGracefulStopboolisLockedboolisLockedMu*sync.MutexsourceDrvsource.DriversourceNamestring
Close closes the source and the database.
Down looks at the currently active migration version
and will migrate all the way down (applying all down migrations).
Drop deletes everything in the database.
Force sets a migration version.
It does not check any currently active version in database.
It resets the dirty state to false.
Migrate looks at the currently active migration version,
then migrates either up or down to the specified version.
Run runs any migration provided by you against the database.
It does not check any currently active version in database.
Usually you don't need this function at all. Use Migrate,
Steps, Up or Down instead.
Steps looks at the currently active migration version.
It will migrate up if n > 0, and down if n < 0.
Up looks at the currently active migration version
and will migrate all the way up (applying all up migrations).
Version returns the currently active migration version.
If no migration has been applied, yet, it will return ErrNilVersion.
lock is a thread safe helper function to lock the database.
It should be called as late as possible when running migrations.
logErr writes error to m.Log if not nil
logPrintf writes to m.Log if not nil
logVerbosePrintf writes to m.Log if not nil. Use for verbose logging output.
newMigration is a helper func that returns a *Migration for the
specified version and targetVersion.
read reads either up or down migrations from source `from` to `to`.
Each migration is then written to the ret channel.
If an error occurs during reading, that error is written to the ret channel, too.
Once read is done reading it will close the ret channel.
readDown reads down migrations from `from` limitted by `limit`.
limit can be -1, implying no limit and reading until there are no more migrations.
Each migration is then written to the ret channel.
If an error occurs during reading, that error is written to the ret channel, too.
Once readDown is done reading it will close the ret channel.
readUp reads up migrations from `from` limitted by `limit`.
limit can be -1, implying no limit and reading until there are no more migrations.
Each migration is then written to the ret channel.
If an error occurs during reading, that error is written to the ret channel, too.
Once readUp is done reading it will close the ret channel.
runMigrations reads *Migration and error from a channel. Any other type
sent on this channel will result in a panic. Each migration is then
proxied to the database driver and run against the database.
Before running a newly received migration it will check if it's supposed
to stop execution because it might have received a stop signal on the
GracefulStop channel.
stop returns true if no more migrations should be run against the database
because a stop signal was received on the GracefulStop channel.
Calls are cheap and this function is not blocking.
unlock is a thread safe helper function to unlock the database.
It should be called as early as possible when no more migrations are
expected to be executed.
unlockErr calls unlock and returns a combined error
if a prevErr is not nil.
versionExists checks the source if either the up or down migration for
the specified migration version exists.
func New(sourceURL, databaseURL string) (*Migrate, error)
func NewWithDatabaseInstance(sourceURL string, databaseName string, databaseInstance database.Driver) (*Migrate, error)
func NewWithInstance(sourceName string, sourceInstance source.Driver, databaseName string, databaseInstance database.Driver) (*Migrate, error)
func NewWithSourceInstance(sourceName string, sourceInstance source.Driver, databaseURL string) (*Migrate, error)
func newCommon() *Migrate
Migration holds information about a migration.
It is initially created from data coming from the source and then
used when run against the database.
Body holds an io.ReadCloser to the source.
BufferSize defaults to DefaultBufferSize
BufferedBody holds an buffered io.Reader to the underlying Body.
BytesRead holds the number of Bytes read from the migration source.
FinishedBuffering is the time when buffering of the migration source finished.
FinishedReading is the time when the migration source is fully read.
Identifier can be any string to help identifying
the migration in the source.
Scheduled is the time when the migration was scheduled/ queued.
StartedBuffering is the time when buffering of the migration source started.
TargetVersion is the migration version after this migration
has been applied to the database.
Can be -1, implying that this is a NilVersion.
Version is the version of this migration.
bufferWriter holds an io.WriteCloser and pipes to BufferBody.
It's an *Closer for flow control.
Buffer buffers Body up to BufferSize.
Calling this function blocks. Call with goroutine.
LogString returns a string describing this migration to humans.
String implements string.Stringer and is used in tests.
*T : expvar.Var
*T : fmt.Stringer
*T : context.stringer
*T : runtime.stringer
func NewMigration(body io.ReadCloser, identifier string, version uint, targetVersion int) (*Migration, error)
func (*Migrate).newMigration(version uint, targetVersion int) (*Migration, error)
func (*Migrate).Run(migration ...*Migration) error
MultiError holds multiple errors.
Deprecated: Use github.com/hashicorp/go-multierror instead
Errs[]error
Error implements error. Multiple errors are concatenated with 'and's.
T : error
func NewMultiError(errs ...error) MultiError
Package-Level Functions (total 9, in which 7 are exported)
FilterCustomQuery filters all query values starting with `x-`
New returns a new Migrate instance from a source URL and a database URL.
The URL scheme is defined by each driver.
NewMigration returns a new Migration and sets the body, identifier,
version and targetVersion. Body can be nil, which turns this migration
into a "NilMigration". If no identifier is provided, it will default to "<empty>".
targetVersion can be -1, implying it is a NilVersion.
What is a NilMigration?
Usually each migration version coming from source is expected to have an
Up and Down migration. This is not a hard requirement though, leading to
a situation where only the Up or Down migration is present. So let's say
the user wants to migrate up to a version that doesn't have the actual Up
migration, in that case we still want to apply the version, but with an empty
body. We are calling that a NilMigration, a migration with an empty body.
What is a NilVersion?
NilVersion is a const(-1). When running down migrations and we are at the
last down migration, there is no next down migration, the targetVersion should
be nil. Nil in this case is represented by -1 (because type int).
NewMultiError returns an error type holding multiple errors.
Deprecated: Use github.com/hashicorp/go-multierror instead
NewWithDatabaseInstance returns a new Migrate instance from a source URL
and an existing database instance. The source URL scheme is defined by each driver.
Use any string that can serve as an identifier during logging as databaseName.
You are responsible for closing the underlying database client if necessary.
NewWithInstance returns a new Migrate instance from an existing source and
database instance. Use any string that can serve as an identifier during logging
as sourceName and databaseName. You are responsible for closing down
the underlying source and database client if necessary.
NewWithSourceInstance returns a new Migrate instance from an existing source instance
and a database URL. The database URL scheme is defined by each driver.
Use any string that can serve as an identifier during logging as sourceName.
You are responsible for closing the underlying source client if necessary.
suint safely converts int to uint
see https://goo.gl/wEcqof
see https://goo.gl/pai7Dr
Package-Level Variables (total 8, all are exported)
DefaultBufferSize sets the in memory buffer size (in Bytes) for every
pre-read migration (see DefaultPrefetchMigrations).
DefaultLockTimeout sets the max time a database driver has to acquire a lock.
DefaultPrefetchMigrations sets the number of migrations to pre-read
from the source. This is helpful if the source is remote, but has little
effect for a local source (i.e. file system).
Please note that this setting has a major impact on the memory usage,
since each pre-read migration is buffered in memory. See DefaultBufferSize.
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.