migrations

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnToSQL

func ColumnToSQL(col Column) string

func NotNullConstraintName

func NotNullConstraintName(columnName string) string

func TemporaryName

func TemporaryName(name string) string

func TriggerFunctionName

func TriggerFunctionName(tableName, columnName string) string

func TriggerName

func TriggerName(tableName, columnName string) string

Types

type CallbackFn added in v0.2.0

type CallbackFn func(int64)

type CheckConstraint

type CheckConstraint struct {
	Name       string `json:"name"`
	Constraint string `json:"constraint"`
}

func (*CheckConstraint) Validate

func (c *CheckConstraint) Validate() error

type CheckConstraintError

type CheckConstraintError struct {
	Table  string
	Column string
	Err    error
}

func (CheckConstraintError) Error

func (e CheckConstraintError) Error() string

func (CheckConstraintError) Unwrap

func (e CheckConstraintError) Unwrap() error

type Column

type Column struct {
	Name       string               `json:"name"`
	Type       string               `json:"type"`
	Nullable   bool                 `json:"nullable"`
	Unique     bool                 `json:"unique"`
	PrimaryKey bool                 `json:"pk"`
	Default    *string              `json:"default"`
	Check      *CheckConstraint     `json:"check"`
	References *ForeignKeyReference `json:"references"`
}

type ColumnAlreadyExistsError

type ColumnAlreadyExistsError struct {
	Table string
	Name  string
}

func (ColumnAlreadyExistsError) Error

func (e ColumnAlreadyExistsError) Error() string

type ColumnDoesNotExistError

type ColumnDoesNotExistError struct {
	Table string
	Name  string
}

func (ColumnDoesNotExistError) Error

func (e ColumnDoesNotExistError) Error() string

type ColumnIsNotNullableError

type ColumnIsNotNullableError struct {
	Table string
	Name  string
}

func (ColumnIsNotNullableError) Error

func (e ColumnIsNotNullableError) Error() string

type ColumnReferenceError

type ColumnReferenceError struct {
	Table  string
	Column string
	Err    error
}

func (ColumnReferenceError) Error

func (e ColumnReferenceError) Error() string

func (ColumnReferenceError) Unwrap

func (e ColumnReferenceError) Unwrap() error

type EmptyMigrationError

type EmptyMigrationError struct{}

func (EmptyMigrationError) Error

func (e EmptyMigrationError) Error() string

type FieldRequiredError

type FieldRequiredError struct {
	Name string
}

func (FieldRequiredError) Error

func (e FieldRequiredError) Error() string

type ForeignKeyReference

type ForeignKeyReference struct {
	Name   string `json:"name"`
	Table  string `json:"table"`
	Column string `json:"column"`
}

func (*ForeignKeyReference) Validate

func (f *ForeignKeyReference) Validate(s *schema.Schema) error

type IndexAlreadyExistsError

type IndexAlreadyExistsError struct {
	Name string
}

func (IndexAlreadyExistsError) Error

func (e IndexAlreadyExistsError) Error() string

type IndexDoesNotExistError

type IndexDoesNotExistError struct {
	Name string
}

func (IndexDoesNotExistError) Error

func (e IndexDoesNotExistError) Error() string

type InvalidMigrationError

type InvalidMigrationError struct {
	Reason string
}

func (InvalidMigrationError) Error

func (e InvalidMigrationError) Error() string

type InvalidPrimaryKeyError added in v0.2.0

type InvalidPrimaryKeyError struct {
	Table  string
	Fields int
}

func (InvalidPrimaryKeyError) Error added in v0.2.0

func (e InvalidPrimaryKeyError) Error() string

type IsolatedOperation

type IsolatedOperation interface {
	IsIsolated()
}

IsolatedOperation is an operation that cannot be executed with other operations in the same migration

type Migration

type Migration struct {
	Name string `json:"name"`

	Operations Operations `json:"operations"`
}

func ReadMigrationFile

func ReadMigrationFile(file string) (*Migration, error)

func (*Migration) Validate

func (m *Migration) Validate(ctx context.Context, s *schema.Schema) error

Validate will check that the migration can be applied to the given schema returns a descriptive error if the migration is invalid

type MultipleAlterColumnChangesError

type MultipleAlterColumnChangesError struct {
	Changes int
}

func (MultipleAlterColumnChangesError) Error

type NoDownSQLAllowedError

type NoDownSQLAllowedError struct{}

func (NoDownSQLAllowedError) Error

func (e NoDownSQLAllowedError) Error() string

type NoUpSQLAllowedError

type NoUpSQLAllowedError struct{}

func (NoUpSQLAllowedError) Error

func (e NoUpSQLAllowedError) Error() string

type OpAddColumn

type OpAddColumn struct {
	Table  string  `json:"table"`
	Up     *string `json:"up"`
	Column Column  `json:"column"`
}

func (*OpAddColumn) Complete

func (o *OpAddColumn) Complete(ctx context.Context, conn *sql.DB) error

func (*OpAddColumn) Rollback

func (o *OpAddColumn) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpAddColumn) Start

func (o *OpAddColumn) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpAddColumn) Validate

func (o *OpAddColumn) Validate(ctx context.Context, s *schema.Schema) error

type OpAlterColumn

type OpAlterColumn struct {
	Table      string               `json:"table"`
	Column     string               `json:"column"`
	Name       string               `json:"name"`
	Type       string               `json:"type"`
	Check      *CheckConstraint     `json:"check"`
	References *ForeignKeyReference `json:"references"`
	NotNull    *bool                `json:"not_null"`
	Unique     *UniqueConstraint    `json:"unique"`
	Up         string               `json:"up"`
	Down       string               `json:"down"`
}

func (*OpAlterColumn) Complete

func (o *OpAlterColumn) Complete(ctx context.Context, conn *sql.DB) error

func (*OpAlterColumn) Rollback

func (o *OpAlterColumn) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpAlterColumn) Start

func (o *OpAlterColumn) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpAlterColumn) Validate

func (o *OpAlterColumn) Validate(ctx context.Context, s *schema.Schema) error

type OpChangeType

type OpChangeType struct {
	Table  string `json:"table"`
	Column string `json:"column"`
	Type   string `json:"type"`
	Up     string `json:"up"`
	Down   string `json:"down"`
}

func (*OpChangeType) Complete

func (o *OpChangeType) Complete(ctx context.Context, conn *sql.DB) error

func (*OpChangeType) Rollback

func (o *OpChangeType) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpChangeType) Start

func (o *OpChangeType) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpChangeType) Validate

func (o *OpChangeType) Validate(ctx context.Context, s *schema.Schema) error

type OpCreateIndex

type OpCreateIndex struct {
	Name    string   `json:"name"`
	Table   string   `json:"table"`
	Columns []string `json:"columns"`
}

func (*OpCreateIndex) Complete

func (o *OpCreateIndex) Complete(ctx context.Context, conn *sql.DB) error

func (*OpCreateIndex) Rollback

func (o *OpCreateIndex) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpCreateIndex) Start

func (o *OpCreateIndex) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpCreateIndex) Validate

func (o *OpCreateIndex) Validate(ctx context.Context, s *schema.Schema) error

type OpCreateTable

type OpCreateTable struct {
	Name    string   `json:"name"`
	Columns []Column `json:"columns"`
}

func (*OpCreateTable) Complete

func (o *OpCreateTable) Complete(ctx context.Context, conn *sql.DB) error

func (*OpCreateTable) Rollback

func (o *OpCreateTable) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpCreateTable) Start

func (o *OpCreateTable) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpCreateTable) Validate

func (o *OpCreateTable) Validate(ctx context.Context, s *schema.Schema) error

type OpDropColumn

type OpDropColumn struct {
	Table  string  `json:"table"`
	Column string  `json:"column"`
	Down   *string `json:"down,omitempty"`
}

func (*OpDropColumn) Complete

func (o *OpDropColumn) Complete(ctx context.Context, conn *sql.DB) error

func (*OpDropColumn) Rollback

func (o *OpDropColumn) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpDropColumn) Start

func (o *OpDropColumn) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpDropColumn) Validate

func (o *OpDropColumn) Validate(ctx context.Context, s *schema.Schema) error

type OpDropConstraint

type OpDropConstraint struct {
	Table  string `json:"table"`
	Column string `json:"column"`
	Name   string `json:"name"`
	Up     string `json:"up"`
	Down   string `json:"down"`
}

func (*OpDropConstraint) Complete

func (o *OpDropConstraint) Complete(ctx context.Context, conn *sql.DB) error

func (*OpDropConstraint) Rollback

func (o *OpDropConstraint) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpDropConstraint) Start

func (o *OpDropConstraint) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpDropConstraint) Validate

func (o *OpDropConstraint) Validate(ctx context.Context, s *schema.Schema) error

type OpDropIndex

type OpDropIndex struct {
	Name string `json:"name"`
}

func (*OpDropIndex) Complete

func (o *OpDropIndex) Complete(ctx context.Context, conn *sql.DB) error

func (*OpDropIndex) Rollback

func (o *OpDropIndex) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpDropIndex) Start

func (o *OpDropIndex) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpDropIndex) Validate

func (o *OpDropIndex) Validate(ctx context.Context, s *schema.Schema) error

type OpDropTable

type OpDropTable struct {
	Name string `json:"name"`
}

func (*OpDropTable) Complete

func (o *OpDropTable) Complete(ctx context.Context, conn *sql.DB) error

func (*OpDropTable) Rollback

func (o *OpDropTable) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpDropTable) Start

func (o *OpDropTable) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpDropTable) Validate

func (o *OpDropTable) Validate(ctx context.Context, s *schema.Schema) error

type OpName

type OpName string
const (
	OpNameCreateTable    OpName = "create_table"
	OpNameRenameTable    OpName = "rename_table"
	OpNameDropTable      OpName = "drop_table"
	OpNameAddColumn      OpName = "add_column"
	OpNameDropColumn     OpName = "drop_column"
	OpNameAlterColumn    OpName = "alter_column"
	OpNameCreateIndex    OpName = "create_index"
	OpNameDropIndex      OpName = "drop_index"
	OpNameDropConstraint OpName = "drop_constraint"
	OpRawSQLName         OpName = "sql"

	// Internal operation types used by `alter_column`
	OpNameRenameColumn       OpName = "rename_column"
	OpNameSetUnique          OpName = "set_unique"
	OpNameSetNotNull         OpName = "set_not_null"
	OpNameSetForeignKey      OpName = "set_foreign_key"
	OpNameSetCheckConstraint OpName = "set_check_constraint"
	OpNameChangeType         OpName = "change_type"
)

func OperationName

func OperationName(op Operation) OpName

type OpRawSQL

type OpRawSQL struct {
	Up   string `json:"up"`
	Down string `json:"down,omitempty"`
}

func (*OpRawSQL) Complete

func (o *OpRawSQL) Complete(ctx context.Context, conn *sql.DB) error

func (*OpRawSQL) IsIsolated

func (o *OpRawSQL) IsIsolated()

this operation is isolated, cannot be executed with other operations

func (*OpRawSQL) RequiresSchemaRefresh

func (o *OpRawSQL) RequiresSchemaRefresh()

this operation requires the resulting schema to be refreshed

func (*OpRawSQL) Rollback

func (o *OpRawSQL) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpRawSQL) Start

func (o *OpRawSQL) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpRawSQL) Validate

func (o *OpRawSQL) Validate(ctx context.Context, s *schema.Schema) error

type OpRenameColumn

type OpRenameColumn struct {
	Table string `json:"table"`
	From  string `json:"from"`
	To    string `json:"to"`
}

func (*OpRenameColumn) Complete

func (o *OpRenameColumn) Complete(ctx context.Context, conn *sql.DB) error

func (*OpRenameColumn) Rollback

func (o *OpRenameColumn) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpRenameColumn) Start

func (o *OpRenameColumn) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpRenameColumn) Validate

func (o *OpRenameColumn) Validate(ctx context.Context, s *schema.Schema) error

type OpRenameTable

type OpRenameTable struct {
	From string `json:"from"`
	To   string `json:"to"`
}

func (*OpRenameTable) Complete

func (o *OpRenameTable) Complete(ctx context.Context, conn *sql.DB) error

func (*OpRenameTable) Rollback

func (o *OpRenameTable) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpRenameTable) Start

func (o *OpRenameTable) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpRenameTable) Validate

func (o *OpRenameTable) Validate(ctx context.Context, s *schema.Schema) error

type OpSetCheckConstraint

type OpSetCheckConstraint struct {
	Table  string          `json:"table"`
	Column string          `json:"column"`
	Check  CheckConstraint `json:"check"`
	Up     string          `json:"up"`
	Down   string          `json:"down"`
}

func (*OpSetCheckConstraint) Complete

func (o *OpSetCheckConstraint) Complete(ctx context.Context, conn *sql.DB) error

func (*OpSetCheckConstraint) Rollback

func (o *OpSetCheckConstraint) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpSetCheckConstraint) Start

func (o *OpSetCheckConstraint) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpSetCheckConstraint) Validate

func (o *OpSetCheckConstraint) Validate(ctx context.Context, s *schema.Schema) error

type OpSetForeignKey

type OpSetForeignKey struct {
	Table      string              `json:"table"`
	Column     string              `json:"column"`
	References ForeignKeyReference `json:"references"`
	Up         string              `json:"up"`
	Down       string              `json:"down"`
}

func (*OpSetForeignKey) Complete

func (o *OpSetForeignKey) Complete(ctx context.Context, conn *sql.DB) error

func (*OpSetForeignKey) Rollback

func (o *OpSetForeignKey) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpSetForeignKey) Start

func (o *OpSetForeignKey) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpSetForeignKey) Validate

func (o *OpSetForeignKey) Validate(ctx context.Context, s *schema.Schema) error

type OpSetNotNull

type OpSetNotNull struct {
	Table  string `json:"table"`
	Column string `json:"column"`
	Up     string `json:"up"`
	Down   string `json:"down"`
}

func (*OpSetNotNull) Complete

func (o *OpSetNotNull) Complete(ctx context.Context, conn *sql.DB) error

func (*OpSetNotNull) Rollback

func (o *OpSetNotNull) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpSetNotNull) Start

func (o *OpSetNotNull) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpSetNotNull) Validate

func (o *OpSetNotNull) Validate(ctx context.Context, s *schema.Schema) error

type OpSetUnique

type OpSetUnique struct {
	Name   string `json:"name"`
	Table  string `json:"table"`
	Column string `json:"column"`
	Up     string `json:"up"`
	Down   string `json:"down"`
}

func (*OpSetUnique) Complete

func (o *OpSetUnique) Complete(ctx context.Context, conn *sql.DB) error

func (*OpSetUnique) Rollback

func (o *OpSetUnique) Rollback(ctx context.Context, conn *sql.DB) error

func (*OpSetUnique) Start

func (o *OpSetUnique) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

func (*OpSetUnique) Validate

func (o *OpSetUnique) Validate(ctx context.Context, s *schema.Schema) error

type Operation

type Operation interface {
	// Start will apply the required changes to enable supporting the new schema
	// version in the database (through a view)
	// update the given views to expose the new schema version
	Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error

	// Complete will update the database schema to match the current version
	// after calling Start.
	// This method should be called once the previous version is no longer used
	Complete(ctx context.Context, conn *sql.DB) error

	// Rollback will revert the changes made by Start. It is not possible to
	// rollback a completed migration.
	Rollback(ctx context.Context, conn *sql.DB) error

	// Validate returns a descriptive error if the operation cannot be applied to the given schema
	Validate(ctx context.Context, s *schema.Schema) error
}

type Operations

type Operations []Operation

func (Operations) MarshalJSON

func (v Operations) MarshalJSON() ([]byte, error)

MarshalJSON serializes the list of operations into a JSON array.

func (*Operations) UnmarshalJSON

func (v *Operations) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes the list of operations from a JSON array.

type RequiresSchemaRefreshOperation

type RequiresSchemaRefreshOperation interface {
	RequiresSchemaRefresh()
}

RequiresSchemaRefreshOperation is an operation that requires the resulting schema to be refreshed

type TableAlreadyExistsError

type TableAlreadyExistsError struct {
	Name string
}

func (TableAlreadyExistsError) Error

func (e TableAlreadyExistsError) Error() string

type TableDoesNotExistError

type TableDoesNotExistError struct {
	Name string
}

func (TableDoesNotExistError) Error

func (e TableDoesNotExistError) Error() string

type TriggerDirection

type TriggerDirection string
const (
	TriggerDirectionUp   TriggerDirection = "up"
	TriggerDirectionDown TriggerDirection = "down"
)

type UniqueConstraint

type UniqueConstraint struct {
	Name string `json:"name"`
}

func (*UniqueConstraint) Validate

func (c *UniqueConstraint) Validate() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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