Documentation ¶
Overview ¶
Package migrate implements migrations on Hasura GraphQL Engine.
This package is borrowed from https://github.com/golang-migrate/migrate with additions for Hasura specific yaml file support and a improved Rails-like migration pattern.
Index ¶
- Variables
- func FilterCustomQuery(u *nurl.URL) *nurl.URL
- func GetDataPath(ec *cli.ExecutionContext) *nurl.URL
- func GetFilePath(dir string) *nurl.URL
- func IsMigrationsSupported(kind hasura.SourceKind) bool
- type ErrDirty
- type ErrShortLimit
- type Migrate
- func (m *Migrate) Close() error
- func (m *Migrate) Down() error
- func (m *Migrate) ExportSchemaDump(includeSchemas []string, excludeSchemas []string, sourceName string, ...) ([]byte, error)
- func (m *Migrate) GetSetting(name string) (string, error)
- func (m *Migrate) GetStatus() (*Status, error)
- func (m *Migrate) GetUnappliedMigrations(version uint64) []uint64
- func (m *Migrate) GotoVersion(gotoVersion int64) error
- func (m *Migrate) Migrate(version uint64, direction string) error
- func (m *Migrate) Query(data interface{}) error
- func (m *Migrate) QueryWithVersion(version uint64, data io.ReadCloser, skipExecution bool) error
- func (m *Migrate) ReScan() error
- func (m *Migrate) RemoveVersions(versions []uint64) error
- func (m *Migrate) Squash(v1 uint64, v2 int64) (vs []int64, us []byte, ds []byte, err error)
- func (m *Migrate) Steps(n int64) error
- func (m *Migrate) Up() error
- func (m *Migrate) UpdateSetting(name string, value string) error
- func (m *Migrate) Version() (version uint64, dirty bool, err error)
- type Migration
- type MigrationStatus
- type MultiError
- type NewMigrateOpts
- type Status
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoChange = fmt.Errorf("no change") ErrNilVersion = fmt.Errorf("no migration") ErrLocked = fmt.Errorf("database locked") ErrNoMigrationFiles = fmt.Errorf("no migration files found") ErrLockTimeout = fmt.Errorf("timeout: can't acquire database lock") ErrApplied = fmt.Errorf("Version already applied in database") ErrNotApplied = fmt.Errorf("Migration not applied in database") ErrNoMigrationMode = fmt.Errorf("Migration mode is disabled") ErrMigrationMode = fmt.Errorf("Migration mode is enabled") )
var DefaultBufferSize = uint64(100000)
DefaultBufferSize sets the in memory buffer size (in Bytes) for every pre-read migration (see DefaultPrefetchMigrations).
var DefaultLockTimeout = 15 * time.Second
DefaultLockTimeout sets the max time a database driver has to acquire a lock.
var DefaultPrefetchMigrations = uint64(10)
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.
Functions ¶
func FilterCustomQuery ¶
FilterCustomQuery filters all query values starting with `x-`
func GetDataPath ¶
func GetFilePath ¶
func IsMigrationsSupported ¶
func IsMigrationsSupported(kind hasura.SourceKind) bool
Types ¶
type ErrShortLimit ¶
type ErrShortLimit struct {
Short uint64
}
ErrShortLimit is an error returned when not enough migrations can be returned by a source for a given limit.
func (ErrShortLimit) Error ¶
func (e ErrShortLimit) Error() string
Error implements the error interface.
type Migrate ¶
type Migrate struct { // Logger is the global logger object to print logs. Logger *log.Logger // GracefulStop accepts `true` and will stop executing migrations // as soon as possible at a safe break point, so that the database // is not corrupted. GracefulStop chan bool // PrefetchMigrations defaults to DefaultPrefetchMigrations, // but can be set per Migrate instance. PrefetchMigrations uint64 // LockTimeout defaults to DefaultLockTimeout, // but can be set per Migrate instance. LockTimeout time.Duration SkipExecution bool DryRun bool ProgressBarLogs bool // contains filtered or unexported fields }
func New ¶
func New(opts NewMigrateOpts) (*Migrate, error)
New returns a new Migrate instance from a source URL and a database URL. The URL scheme is defined by each driver.
func NewMigrate ¶
func (*Migrate) Down ¶
Down looks at the currently active migration version and will migrate all the way down (applying all down migrations).
func (*Migrate) ExportSchemaDump ¶
func (*Migrate) GetUnappliedMigrations ¶
func (*Migrate) GotoVersion ¶
GotoVersion will apply a version also applying the migration chain leading to it
func (*Migrate) Migrate ¶
Migrate looks at the currently active migration version, then migrates either up or down to the specified version.
func (*Migrate) QueryWithVersion ¶
func (*Migrate) RemoveVersions ¶
func (*Migrate) Squash ¶
Squash migrations from version v into a new migration. Returns a list of migrations that are squashed: vs the squashed metadata for all UP steps: um the squashed SQL for all UP steps: us the squashed metadata for all down steps: dm the squashed SQL for all down steps: ds
func (*Migrate) Steps ¶
Steps looks at the currently active migration version. It will migrate up if n > 0, and down if n < 0.
type Migration ¶
type Migration struct { // Identifier can be any string to help identifying // the migration in the source. Identifier string // Version is the version of this migration. Version uint64 // TargetVersion is the migration version after this migration // has been applied to the database. // Can be -1, implying that this is a NilVersion. TargetVersion int64 // File Type FileType string // File name FileName string // Body holds an io.ReadCloser to the source. Body io.ReadCloser // BufferedBody holds an buffered io.Reader to the underlying Body. BufferedBody io.Reader // BufferSize defaults to DefaultBufferSize BufferSize uint64 // Scheduled is the time when the migration was scheduled/ queued. Scheduled time.Time // StartedBuffering is the time when buffering of the migration source started. StartedBuffering time.Time // FinishedBuffering is the time when buffering of the migration source finished. FinishedBuffering time.Time // FinishedReading is the time when the migration source is fully read. FinishedReading time.Time // BytesRead holds the number of Bytes read from the migration source. BytesRead int64 // contains filtered or unexported fields }
Migration holds information about a migration. It is initially created from data coming from the source and then used when run against the database.
func NewMigration ¶
func NewMigration(body io.ReadCloser, identifier string, version uint64, targetVersion int64, fileType string, fileName string) (*Migration, error)
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).
type MigrationStatus ¶
type MigrationStatus struct { // Version is the version of this migration. Version uint64 `json:"-"` // Name helps finding this migration in the source folder Name string `json:"-"` // Check if the migration is applied on the cluster IsApplied bool `json:"database_status"` // Check if the migration is present on the local. IsPresent bool `json:"source_status"` IsDirty bool `json:"-"` }
type MultiError ¶
type MultiError struct {
Errs []error
}
MultiError holds multiple errors.
func NewMultiError ¶
func NewMultiError(errs ...error) MultiError
NewMultiError returns an error type holding multiple errors.
func (MultiError) Error ¶
func (m MultiError) Error() string
Error implements error. Mulitple errors are concatenated with 'and's.
type NewMigrateOpts ¶
type Status ¶
type Status struct { Index uint64Slice `json:"migrations"` Migrations map[uint64]*MigrationStatus `json:"status"` }
func (*Status) Append ¶
func (i *Status) Append(m *MigrationStatus) (ok bool)