Documentation
¶
Index ¶
- Variables
- func Down(ctx context.Context, db *pack.DB) error
- func Register(m Migration)
- func Up(ctx context.Context, db *pack.DB) error
- type ColumnType
- type ConstraintOption
- type DropOption
- type ErrConstraintWrongRelationKind
- type ErrForeignKeyCheckFailed
- type ErrRelationNotFound
- type IndexOption
- type Migration
- type Migrator
- func (m *Migrator) AddColumn(ctx context.Context, dst any, field string) error
- func (m *Migrator) AlterColumn(ctx context.Context, dst any, field string) error
- func (m *Migrator) ColumnTypes(ctx context.Context, dst any) ([]ColumnType, error)
- func (m *Migrator) CreateConstraint(ctx context.Context, dst any, relationName string, opts ...ConstraintOption) error
- func (m *Migrator) CreateIndex(ctx context.Context, dst any, name string, fields []string, ...) error
- func (m *Migrator) CreateTable(ctx context.Context, dst ...any) error
- func (m *Migrator) CurrentDatabase(ctx context.Context) (string, error)
- func (m *Migrator) DropColumn(ctx context.Context, dst any, field string, opts ...DropOption) error
- func (m *Migrator) DropConstraint(ctx context.Context, dst any, name string, opts ...DropOption) error
- func (m *Migrator) DropIndex(ctx context.Context, dst any, name string, opts ...DropOption) error
- func (m *Migrator) DropTable(ctx context.Context, dst any, opts ...DropOption) error
- func (m *Migrator) GetTables(ctx context.Context) ([]string, error)
- func (m *Migrator) HasColumn(ctx context.Context, dst any, field string) (bool, error)
- func (m *Migrator) HasConstraint(ctx context.Context, dst any, name string) (bool, error)
- func (m *Migrator) HasIndex(ctx context.Context, dst any, name string) (bool, error)
- func (m *Migrator) HasTable(ctx context.Context, dst any) (bool, error)
- func (m *Migrator) RenameColumn(ctx context.Context, dst any, field, newName string) error
- func (m *Migrator) RenameIndex(ctx context.Context, dst any, oldName, newName string) error
- func (m *Migrator) RenameTable(ctx context.Context, dst any, newName string) error
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
func (*ErrConstraintWrongRelationKind) Error ¶
func (e *ErrConstraintWrongRelationKind) Error() string
type ErrForeignKeyCheckFailed ¶
func (*ErrForeignKeyCheckFailed) Error ¶
func (e *ErrForeignKeyCheckFailed) Error() string
type ErrRelationNotFound ¶
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 (*Migrator) AlterColumn ¶
func (*Migrator) ColumnTypes ¶
func (*Migrator) CreateConstraint ¶
func (*Migrator) CreateIndex ¶
func (*Migrator) CreateTable ¶
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.