migrate

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlterColumnRequiresTopLevelConnection = fmt.Errorf(
	"pack/migrate: AlterColumn on SQLite requires a table rebuild, which cannot run inside an existing transaction; call it on a top-level *pack.DB",
)

Functions

func Down

func Down(ctx context.Context, db *pack.DB) error

Down rolls back the single most recently applied migration. It is a no-op if no migration has been applied.

func Register

func Register(m Migration)

Register adds m to the set of known migrations. Generated migration files call this from an init() function, so that blank-importing a db/migrations package is enough to make every migration in it known to Up and Down.

func Up

func Up(ctx context.Context, db *pack.DB) error

Up applies every registered migration that has not yet been recorded in schema_migrations, in ID order. A migration that fails leaves every migration applied before it in place: migrations run against a top-level *Migrator rather than inside a transaction, since SQLite's AlterColumn requires a top-level connection and MySQL DDL isn't transactional anyway.

Types

type ColumnType

type ColumnType = dialect.ColumnMeta

type ConstraintOption

type ConstraintOption func(*constraintConfig)

func WithConstraintName

func WithConstraintName(name string) ConstraintOption

type DropOption

type DropOption func(*dropConfig)

DropOption configures Drop{Table,Column,Index,Constraint}.

func WithoutIfExists

func WithoutIfExists() DropOption

type ErrConstraintWrongRelationKind

type ErrConstraintWrongRelationKind struct {
	Model    string
	Relation string
	Kind     string
}

func (*ErrConstraintWrongRelationKind) Error

type ErrForeignKeyCheckFailed

type ErrForeignKeyCheckFailed struct {
	Table string
	Rows  []map[string]any
}

func (*ErrForeignKeyCheckFailed) Error

func (e *ErrForeignKeyCheckFailed) Error() string

type ErrRelationNotFound

type ErrRelationNotFound struct {
	Model    string
	Relation string
}

func (*ErrRelationNotFound) Error

func (e *ErrRelationNotFound) Error() string

type IndexOption

type IndexOption func(*indexConfig)

func WithUniqueIndex

func WithUniqueIndex() IndexOption

type Migration

type Migration struct {
	ID       string
	Migrate  func(ctx context.Context, m *Migrator) error
	Rollback func(ctx context.Context, m *Migrator) error
}

Migration is one versioned schema change, written in Go against the DDL primitives exposed by *Migrator.

func Registered

func Registered() []Migration

Registered returns every registered migration, sorted by ID.

type Migrator

type Migrator struct {
	// contains filtered or unexported fields
}

func New

func New(db *pack.DB) *Migrator

New returns a Migrator for db. It panics if db's dialect doesn't implement dialect.DDL.

func (*Migrator) AddColumn

func (m *Migrator) AddColumn(ctx context.Context, dst any, field string) error

func (*Migrator) AlterColumn

func (m *Migrator) AlterColumn(ctx context.Context, dst any, field string) error

func (*Migrator) ColumnTypes

func (m *Migrator) ColumnTypes(ctx context.Context, dst any) ([]ColumnType, error)

func (*Migrator) CreateConstraint

func (m *Migrator) CreateConstraint(ctx context.Context, dst any, relationName string, opts ...ConstraintOption) error

func (*Migrator) CreateIndex

func (m *Migrator) CreateIndex(ctx context.Context, dst any, name string, fields []string, opts ...IndexOption) error

func (*Migrator) CreateTable

func (m *Migrator) CreateTable(ctx context.Context, dst ...any) error

CreateTable creates one table per model, deriving the full column set (types, primary key, unique, not-null, default) from the same struct tags Create[T]/Query[T] already use.

func (*Migrator) CurrentDatabase

func (m *Migrator) CurrentDatabase(ctx context.Context) (string, error)

func (*Migrator) DropColumn

func (m *Migrator) DropColumn(ctx context.Context, dst any, field string, opts ...DropOption) error

func (*Migrator) DropConstraint

func (m *Migrator) DropConstraint(ctx context.Context, dst any, name string, opts ...DropOption) error

func (*Migrator) DropIndex

func (m *Migrator) DropIndex(ctx context.Context, dst any, name string, opts ...DropOption) error

func (*Migrator) DropTable

func (m *Migrator) DropTable(ctx context.Context, dst any, opts ...DropOption) error

func (*Migrator) GetTables

func (m *Migrator) GetTables(ctx context.Context) ([]string, error)

func (*Migrator) HasColumn

func (m *Migrator) HasColumn(ctx context.Context, dst any, field string) (bool, error)

func (*Migrator) HasConstraint

func (m *Migrator) HasConstraint(ctx context.Context, dst any, name string) (bool, error)

func (*Migrator) HasIndex

func (m *Migrator) HasIndex(ctx context.Context, dst any, name string) (bool, error)

func (*Migrator) HasTable

func (m *Migrator) HasTable(ctx context.Context, dst any) (bool, error)

HasTable reports whether dst's table exists.

func (*Migrator) RenameColumn

func (m *Migrator) RenameColumn(ctx context.Context, dst any, field, newName string) error

func (*Migrator) RenameIndex

func (m *Migrator) RenameIndex(ctx context.Context, dst any, oldName, newName string) error

func (*Migrator) RenameTable

func (m *Migrator) RenameTable(ctx context.Context, dst any, newName string) error

RenameTable renames dst's table to newName.

Jump to

Keyboard shortcuts

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