Documentation
¶
Index ¶
- func Exec(ctx context.Context, db *bun.DB, f io.Reader, isTx bool) error
- type AddColumnOp
- type AddForeignKeyOp
- type AddPrimaryKeyOp
- type AddUniqueConstraintOp
- type AutoMigrator
- type AutoMigratorOption
- func WithExcludeTable(tables ...string) AutoMigratorOption
- func WithLocksTableNameAuto(table string) AutoMigratorOption
- func WithMarkAppliedOnSuccessAuto(enabled bool) AutoMigratorOption
- func WithMigrationsDirectoryAuto(directory string) AutoMigratorOption
- func WithModel(models ...interface{}) AutoMigratorOption
- func WithSchemaName(schemaName string) AutoMigratorOption
- func WithTableNameAuto(table string) AutoMigratorOption
- type ChangeColumnTypeOp
- type ChangePrimaryKeyOp
- type CompareTypeFunc
- type CreateTableOp
- type DropColumnOp
- type DropForeignKeyOp
- type DropPrimaryKeyOp
- type DropTableOp
- type DropUniqueConstraintOp
- type GoMigrationOption
- type Migration
- type MigrationFile
- type MigrationFunc
- type MigrationGroup
- type MigrationOption
- type MigrationSlice
- type Migrations
- func (m *Migrations) Add(migration Migration)
- func (m *Migrations) Discover(fsys fs.FS) error
- func (m *Migrations) DiscoverCaller() error
- func (m *Migrations) MustRegister(up, down MigrationFunc)
- func (m *Migrations) Register(up, down MigrationFunc) error
- func (m *Migrations) Sorted() MigrationSlice
- type MigrationsOption
- type Migrator
- func (m *Migrator) AppliedMigrations(ctx context.Context) (MigrationSlice, error)
- func (m *Migrator) CreateGoMigration(ctx context.Context, name string, opts ...GoMigrationOption) (*MigrationFile, error)
- func (m *Migrator) CreateSQLMigrations(ctx context.Context, name string) ([]*MigrationFile, error)
- func (m *Migrator) CreateTxSQLMigrations(ctx context.Context, name string) ([]*MigrationFile, error)
- func (m *Migrator) DB() *bun.DB
- func (m *Migrator) Init(ctx context.Context) error
- func (m *Migrator) Lock(ctx context.Context) error
- func (m *Migrator) MarkApplied(ctx context.Context, migration *Migration) error
- func (m *Migrator) MarkUnapplied(ctx context.Context, migration *Migration) error
- func (m *Migrator) Migrate(ctx context.Context, opts ...MigrationOption) (*MigrationGroup, error)
- func (m *Migrator) MigrationsWithStatus(ctx context.Context) (MigrationSlice, error)
- func (m *Migrator) MissingMigrations(ctx context.Context) (MigrationSlice, error)
- func (m *Migrator) Reset(ctx context.Context) error
- func (m *Migrator) Rollback(ctx context.Context, opts ...MigrationOption) (*MigrationGroup, error)
- func (m *Migrator) TruncateTable(ctx context.Context) error
- func (m *Migrator) Unlock(ctx context.Context) error
- type MigratorOption
- type Operation
- type RenameColumnOp
- type RenameTableOp
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AddColumnOp ¶
AddColumnOp adds a new column to the table.
func (*AddColumnOp) GetReverse ¶
func (op *AddColumnOp) GetReverse() Operation
type AddForeignKeyOp ¶
type AddForeignKeyOp struct {
ForeignKey sqlschema.ForeignKey
ConstraintName string
}
AddForeignKey adds a new FOREIGN KEY constraint.
func (*AddForeignKeyOp) DependsOn ¶
func (op *AddForeignKeyOp) DependsOn(another Operation) bool
func (*AddForeignKeyOp) GetReverse ¶
func (op *AddForeignKeyOp) GetReverse() Operation
func (*AddForeignKeyOp) TableName ¶
func (op *AddForeignKeyOp) TableName() string
type AddPrimaryKeyOp ¶
type AddPrimaryKeyOp struct {
TableName string
PrimaryKey sqlschema.PrimaryKey
}
AddPrimaryKeyOp adds a new PRIMARY KEY to the table.
func (*AddPrimaryKeyOp) DependsOn ¶
func (op *AddPrimaryKeyOp) DependsOn(another Operation) bool
func (*AddPrimaryKeyOp) GetReverse ¶
func (op *AddPrimaryKeyOp) GetReverse() Operation
type AddUniqueConstraintOp ¶
AddUniqueConstraintOp adds new UNIQUE constraint to the table.
func (*AddUniqueConstraintOp) DependsOn ¶
func (op *AddUniqueConstraintOp) DependsOn(another Operation) bool
func (*AddUniqueConstraintOp) GetReverse ¶
func (op *AddUniqueConstraintOp) GetReverse() Operation
type AutoMigrator ¶
type AutoMigrator struct {
// contains filtered or unexported fields
}
AutoMigrator performs automated schema migrations.
It is designed to be a drop-in replacement for some Migrator functionality and supports all existing configuration options. Similarly to Migrator, it has methods to create SQL migrations, write them to a file, and apply them. Unlike Migrator, it detects the differences between the state defined by bun models and the current database schema automatically.
Usage:
- Generate migrations and apply them au once with AutoMigrator.Migrate().
- Create up- and down-SQL migration files and apply migrations using Migrator.Migrate().
While both methods produce complete, reversible migrations (with entries in the database and SQL migration files), prefer creating migrations and applying them separately for any non-trivial cases to ensure AutoMigrator detects expected changes correctly.
Limitations:
- AutoMigrator only supports a subset of the possible ALTER TABLE modifications.
- Some changes are not automatically reversible. For example, you would need to manually add a CREATE TABLE query to the .down migration file to revert a DROP TABLE migration.
- Does not validate most dialect-specific constraints. For example, when changing column data type, make sure the data con be auto-casted to the new type.
- Due to how the schema-state diff is calculated, it is not possible to rename a table and modify any of its columns' _data type_ in a single run. This will cause the AutoMigrator to drop and re-create the table under a different name; it is better to apply this change in 2 steps. Renaming a table and renaming its columns at the same time is possible.
- Renaming table/column to an existing name, i.e. like this [A->B] [B->C], is not possible due to how AutoMigrator distinguishes "rename" and "unchanged" columns.
Dialect must implement both sqlschema.Inspector and sqlschema.Migrator to be used with AutoMigrator.
func NewAutoMigrator ¶
func NewAutoMigrator(db *bun.DB, opts ...AutoMigratorOption) (*AutoMigrator, error)
func (*AutoMigrator) CreateSQLMigrations ¶
func (am *AutoMigrator) CreateSQLMigrations(ctx context.Context) ([]*MigrationFile, error)
CreateSQLMigration writes required changes to a new migration file. Use migrate.Migrator to apply the generated migrations.
func (*AutoMigrator) CreateTxSQLMigrations ¶
func (am *AutoMigrator) CreateTxSQLMigrations(ctx context.Context) ([]*MigrationFile, error)
CreateTxSQLMigration writes required changes to a new migration file making sure they will be executed in a transaction when applied. Use migrate.Migrator to apply the generated migrations.
func (*AutoMigrator) Migrate ¶
func (am *AutoMigrator) Migrate(ctx context.Context, opts ...MigrationOption) (*MigrationGroup, error)
Migrate writes required changes to a new migration file and runs the migration. This will create and entry in the migrations table, making it possible to revert the changes with Migrator.Rollback(). MigrationOptions are passed on to Migrator.Migrate().
type AutoMigratorOption ¶
type AutoMigratorOption func(m *AutoMigrator)
func WithExcludeTable ¶
func WithExcludeTable(tables ...string) AutoMigratorOption
WithExcludeTable tells the AutoMigrator to ignore a table in the database. This prevents AutoMigrator from dropping tables which may exist in the schema but which are not used by the application.
Do not exclude tables included via WithModel, as BunModelInspector ignores this setting.
func WithLocksTableNameAuto ¶
func WithLocksTableNameAuto(table string) AutoMigratorOption
WithLocksTableNameAuto overrides default migration locks table name.
func WithMarkAppliedOnSuccessAuto ¶
func WithMarkAppliedOnSuccessAuto(enabled bool) AutoMigratorOption
WithMarkAppliedOnSuccessAuto sets the migrator to only mark migrations as applied/unapplied when their up/down is successful.
func WithMigrationsDirectoryAuto ¶
func WithMigrationsDirectoryAuto(directory string) AutoMigratorOption
WithMigrationsDirectoryAuto overrides the default directory for migration files.
func WithModel ¶
func WithModel(models ...interface{}) AutoMigratorOption
WithModel adds a bun.Model to the scope of migrations.
func WithSchemaName ¶
func WithSchemaName(schemaName string) AutoMigratorOption
WithSchemaName changes the default database schema to migrate objects in.
func WithTableNameAuto ¶
func WithTableNameAuto(table string) AutoMigratorOption
WithTableNameAuto overrides default migrations table name.
type ChangeColumnTypeOp ¶
type ChangeColumnTypeOp struct {
TableName string
Column string
From sqlschema.Column
To sqlschema.Column
}
ChangeColumnTypeOp set a new data type for the column. The two types should be such that the data can be auto-casted from one to another. E.g. reducing VARCHAR lenght is not possible in most dialects. AutoMigrator does not enforce or validate these rules.
func (*ChangeColumnTypeOp) GetReverse ¶
func (op *ChangeColumnTypeOp) GetReverse() Operation
type ChangePrimaryKeyOp ¶
type ChangePrimaryKeyOp struct {
TableName string
Old sqlschema.PrimaryKey
New sqlschema.PrimaryKey
}
ChangePrimaryKeyOp changes the PRIMARY KEY of the table.
func (*ChangePrimaryKeyOp) GetReverse ¶
func (op *ChangePrimaryKeyOp) GetReverse() Operation
type CreateTableOp ¶
type CreateTableOp struct {
TableName string
Model interface{}
}
CreateTableOp creates a new table in the schema.
It does not report dependency on any other migration and may be executed first. Make sure the dialect does not include FOREIGN KEY constraints in the CREATE TABLE statement, as those may potentially reference not-yet-existing columns/tables.
func (*CreateTableOp) GetReverse ¶
func (op *CreateTableOp) GetReverse() Operation
type DropColumnOp ¶
DropColumnOp drop a column from the table.
While some dialects allow DROP CASCADE to drop dependent constraints, explicit handling on constraints is preferred for transparency and debugging. DropColumnOp depends on DropForeignKeyOp, DropPrimaryKeyOp, and ChangePrimaryKeyOp if any of the constraints is defined on this table.
func (*DropColumnOp) DependsOn ¶
func (op *DropColumnOp) DependsOn(another Operation) bool
func (*DropColumnOp) GetReverse ¶
func (op *DropColumnOp) GetReverse() Operation
type DropForeignKeyOp ¶
type DropForeignKeyOp struct {
ForeignKey sqlschema.ForeignKey
ConstraintName string
}
DropForeignKeyOp drops a FOREIGN KEY constraint.
func (*DropForeignKeyOp) GetReverse ¶
func (op *DropForeignKeyOp) GetReverse() Operation
func (*DropForeignKeyOp) TableName ¶
func (op *DropForeignKeyOp) TableName() string
type DropPrimaryKeyOp ¶
type DropPrimaryKeyOp struct {
TableName string
PrimaryKey sqlschema.PrimaryKey
}
DropPrimaryKeyOp drops the table's PRIMARY KEY.
func (*DropPrimaryKeyOp) GetReverse ¶
func (op *DropPrimaryKeyOp) GetReverse() Operation
type DropTableOp ¶
type DropTableOp struct {
TableName string
}
DropTableOp drops a database table. This operation is not reversible.
func (*DropTableOp) DependsOn ¶
func (op *DropTableOp) DependsOn(another Operation) bool
func (*DropTableOp) GetReverse ¶
func (op *DropTableOp) GetReverse() Operation
GetReverse for a DropTable returns a no-op migration. Logically, CreateTable is the reverse, but DropTable does not have the table's definition to create one.
type DropUniqueConstraintOp ¶
DropUniqueConstraintOp drops a UNIQUE constraint.
func (*DropUniqueConstraintOp) DependsOn ¶
func (op *DropUniqueConstraintOp) DependsOn(another Operation) bool
func (*DropUniqueConstraintOp) GetReverse ¶
func (op *DropUniqueConstraintOp) GetReverse() Operation
type GoMigrationOption ¶
type GoMigrationOption func(cfg *goMigrationConfig)
func WithGoTemplate ¶
func WithGoTemplate(template string) GoMigrationOption
func WithPackageName ¶
func WithPackageName(name string) GoMigrationOption
type Migration ¶
type Migration struct {
bun.BaseModel
ID int64 `bun:",pk,autoincrement"`
Name string
Comment string `bun:"-"`
GroupID int64
MigratedAt time.Time `bun:",notnull,nullzero,default:current_timestamp"`
Up MigrationFunc `bun:"-"`
Down MigrationFunc `bun:"-"`
}
type MigrationFile ¶
type MigrationFunc ¶
func NewSQLMigrationFunc ¶
func NewSQLMigrationFunc(fsys fs.FS, name string) MigrationFunc
type MigrationGroup ¶
type MigrationGroup struct {
ID int64
Migrations MigrationSlice
}
func (MigrationGroup) IsZero ¶
func (g MigrationGroup) IsZero() bool
func (MigrationGroup) String ¶
func (g MigrationGroup) String() string
type MigrationOption ¶
type MigrationOption func(cfg *migrationConfig)
func WithNopMigration ¶
func WithNopMigration() MigrationOption
type MigrationSlice ¶
type MigrationSlice []Migration
func (MigrationSlice) Applied ¶
func (ms MigrationSlice) Applied() MigrationSlice
Applied returns applied migrations in descending order (the order is important and is used in Rollback).
func (MigrationSlice) LastGroup ¶
func (ms MigrationSlice) LastGroup() *MigrationGroup
LastGroup returns the last applied migration group.
func (MigrationSlice) LastGroupID ¶
func (ms MigrationSlice) LastGroupID() int64
LastGroupID returns the last applied migration group id. The id is 0 when there are no migration groups.
func (MigrationSlice) String ¶
func (ms MigrationSlice) String() string
func (MigrationSlice) Unapplied ¶
func (ms MigrationSlice) Unapplied() MigrationSlice
Unapplied returns unapplied migrations in ascending order (the order is important and is used in Migrate).
type Migrations ¶
type Migrations struct {
// contains filtered or unexported fields
}
func NewMigrations ¶
func NewMigrations(opts ...MigrationsOption) *Migrations
func (*Migrations) Add ¶
func (m *Migrations) Add(migration Migration)
func (*Migrations) DiscoverCaller ¶
func (m *Migrations) DiscoverCaller() error
func (*Migrations) MustRegister ¶
func (m *Migrations) MustRegister(up, down MigrationFunc)
func (*Migrations) Register ¶
func (m *Migrations) Register(up, down MigrationFunc) error
func (*Migrations) Sorted ¶
func (m *Migrations) Sorted() MigrationSlice
type MigrationsOption ¶
type MigrationsOption func(m *Migrations)
func WithMigrationsDirectory ¶
func WithMigrationsDirectory(directory string) MigrationsOption
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
func NewMigrator ¶
func NewMigrator(db *bun.DB, migrations *Migrations, opts ...MigratorOption) *Migrator
func (*Migrator) AppliedMigrations ¶
func (m *Migrator) AppliedMigrations(ctx context.Context) (MigrationSlice, error)
AppliedMigrations selects applied (applied) migrations in descending order.
func (*Migrator) CreateGoMigration ¶
func (m *Migrator) CreateGoMigration( ctx context.Context, name string, opts ...GoMigrationOption, ) (*MigrationFile, error)
CreateGoMigration creates a Go migration file.
func (*Migrator) CreateSQLMigrations ¶
CreateSQLMigrations creates up and down SQL migration files.
func (*Migrator) CreateTxSQLMigrations ¶
func (m *Migrator) CreateTxSQLMigrations(ctx context.Context, name string) ([]*MigrationFile, error)
CreateTxSQLMigration creates transactional up and down SQL migration files.
func (*Migrator) MarkApplied ¶
MarkApplied marks the migration as applied (completed).
func (*Migrator) MarkUnapplied ¶
MarkUnapplied marks the migration as unapplied (new).
func (*Migrator) Migrate ¶
func (m *Migrator) Migrate(ctx context.Context, opts ...MigrationOption) (*MigrationGroup, error)
Migrate runs unapplied migrations. If a migration fails, migrate immediately exits.
func (*Migrator) MigrationsWithStatus ¶
func (m *Migrator) MigrationsWithStatus(ctx context.Context) (MigrationSlice, error)
MigrationsWithStatus returns migrations with status in ascending order.
func (*Migrator) MissingMigrations ¶
func (m *Migrator) MissingMigrations(ctx context.Context) (MigrationSlice, error)
MissingMigrations returns applied migrations that can no longer be found.
func (*Migrator) Rollback ¶
func (m *Migrator) Rollback(ctx context.Context, opts ...MigrationOption) (*MigrationGroup, error)
type MigratorOption ¶
type MigratorOption func(m *Migrator)
func WithLocksTableName ¶
func WithLocksTableName(table string) MigratorOption
WithLocksTableName overrides default migration locks table name.
func WithMarkAppliedOnSuccess ¶
func WithMarkAppliedOnSuccess(enabled bool) MigratorOption
WithMarkAppliedOnSuccess sets the migrator to only mark migrations as applied/unapplied when their up/down is successful.
func WithTableName ¶
func WithTableName(table string) MigratorOption
WithTableName overrides default migrations table name.
type Operation ¶
type Operation interface {
GetReverse() Operation
}
Operation encapsulates the request to change a database definition and knowns which operation can revert it.
It is useful to define "monolith" Operations whenever possible, even though they a dialect may require several distinct steps to apply them. For example, changing a primary key involves first dropping the old constraint before generating the new one. Yet, this is only an implementation detail and passing a higher-level ChangePrimaryKeyOp will give the dialect more information about the applied change.
Some operations might be irreversible due to technical limitations. Returning a *comment from GetReverse() will add an explanatory note to the generate migation file.
To declare dependency on another Operation, operations should implement { DependsOn(Operation) bool } interface, which Changeset will use to resolve dependencies.
type RenameColumnOp ¶
RenameColumnOp renames a column in the table. If the changeset includes a rename operation for the column's table, it should be executed first.
func (*RenameColumnOp) DependsOn ¶
func (op *RenameColumnOp) DependsOn(another Operation) bool
func (*RenameColumnOp) GetReverse ¶
func (op *RenameColumnOp) GetReverse() Operation
type RenameTableOp ¶
RenameTableOp renames the table. Changing the "schema" part of the table's FQN (moving tables between schemas) is not allowed.
func (*RenameTableOp) GetReverse ¶
func (op *RenameTableOp) GetReverse() Operation