migrate

package
v2.45.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2024 License: Apache-2.0 Imports: 28 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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")
)
View Source
var DefaultBufferSize = uint64(100000)

DefaultBufferSize sets the in memory buffer size (in Bytes) for every pre-read migration (see DefaultPrefetchMigrations).

View Source
var DefaultLockTimeout = 15 * time.Second

DefaultLockTimeout sets the max time a database driver has to acquire a lock.

View Source
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

func FilterCustomQuery(u *nurl.URL) *nurl.URL

FilterCustomQuery filters all query values starting with `x-`

func GetDataPath

func GetDataPath(ec *cli.ExecutionContext) *nurl.URL

func GetFilePath

func GetFilePath(dir string) *nurl.URL

func IsMigrationsSupported

func IsMigrationsSupported(kind hasura.SourceKind) bool

Types

type ErrDirty

type ErrDirty struct {
	Version int64
}

func (ErrDirty) Error

func (e ErrDirty) Error() string

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 NewMigrate(ec *cli.ExecutionContext, isCmd bool, sourceName string, sourceKind hasura.SourceKind) (*Migrate, error)

func (*Migrate) Close

func (m *Migrate) Close() error

Close closes the source and the database.

func (*Migrate) Down

func (m *Migrate) Down() error

Down looks at the currently active migration version and will migrate all the way down (applying all down migrations).

func (*Migrate) ExportSchemaDump

func (m *Migrate) ExportSchemaDump(includeSchemas []string, excludeSchemas []string, sourceName string, sourceKind hasura.SourceKind) ([]byte, error)

func (*Migrate) GetSetting

func (m *Migrate) GetSetting(name string) (string, error)

func (*Migrate) GetStatus

func (m *Migrate) GetStatus() (*Status, error)

func (*Migrate) GetUnappliedMigrations

func (m *Migrate) GetUnappliedMigrations(version uint64) []uint64

func (*Migrate) GotoVersion

func (m *Migrate) GotoVersion(gotoVersion int64) error

GotoVersion will apply a version also applying the migration chain leading to it

func (*Migrate) Migrate

func (m *Migrate) Migrate(version uint64, direction string) error

Migrate looks at the currently active migration version, then migrates either up or down to the specified version.

func (*Migrate) Query

func (m *Migrate) Query(data interface{}) error

func (*Migrate) QueryWithVersion

func (m *Migrate) QueryWithVersion(version uint64, data io.ReadCloser, skipExecution bool) error

func (*Migrate) ReScan

func (m *Migrate) ReScan() error

func (*Migrate) RemoveVersions

func (m *Migrate) RemoveVersions(versions []uint64) error

func (*Migrate) Squash

func (m *Migrate) Squash(v1 uint64, v2 int64) (vs []int64, us []byte, ds []byte, err error)

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

func (m *Migrate) Steps(n int64) error

Steps looks at the currently active migration version. It will migrate up if n > 0, and down if n < 0.

func (*Migrate) Up

func (m *Migrate) Up() error

Up looks at the currently active migration version and will migrate all the way up (applying all up migrations).

func (*Migrate) UpdateSetting

func (m *Migrate) UpdateSetting(name string, value string) error

func (*Migrate) Version

func (m *Migrate) Version() (version uint64, dirty bool, err error)

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).

func (*Migration) Buffer

func (m *Migration) Buffer() error

Buffer buffers Body up to BufferSize. Calling this function blocks. Call with goroutine.

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 NewMigrateOpts struct {
	Stdout io.Writer
	Stderr io.Writer
	// contains filtered or unexported fields
}

type Status

type Status struct {
	Index      uint64Slice                 `json:"migrations"`
	Migrations map[uint64]*MigrationStatus `json:"status"`
}

func NewStatus

func NewStatus() *Status

func (*Status) Append

func (i *Status) Append(m *MigrationStatus) (ok bool)

func (*Status) Read

func (i *Status) Read(version uint64) (m *MigrationStatus, ok bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL