Documentation ¶
Overview ¶
Package migrator implements a custom migrator inspired by the upstram migrator(https://github.com/uptrace/bun/blob/master/migrate/migrator.go).
The upstream migrator first marks the migration as applied and then only after runs it. This migrator runs both the migration and the "successful mark" in a transaction. Therefore if the migration is aborted (or fails) the transaction is not applied and no manual rollback is needed.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Migration ¶
type Migration struct { bun.BaseModel ID int64 `bun:",pk,autoincrement"` Name string MigratedAt time.Time `bun:",notnull,nullzero,default:current_timestamp"` Up MigrationFunc `bun:"-"` Down MigrationFunc `bun:"-"` // Unused, just here so that this is backwards compatible. GroupID int64 }
type MigrationFile ¶
type MigrationFunc ¶
func NewSQLMigrationFunc ¶
func NewSQLMigrationFunc(fsys fs.FS, name string) MigrationFunc
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) 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() *Migrations
func (*Migrations) Add ¶
func (m *Migrations) Add(migration Migration)
func (*Migrations) Sorted ¶
func (m *Migrations) Sorted() MigrationSlice
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator is database migrator.
func NewMigrator ¶
func NewMigrator(db *bun.DB, migrations *Migrations) *Migrator
NewMigrator initializes the migrator.
func (*Migrator) MigrationsWithStatus ¶
func (m *Migrator) MigrationsWithStatus(ctx context.Context) (MigrationSlice, error)
MigrationsWithStatus returns migrations with status in ascending order.