migrator

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

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
}

func (*Migration) IsApplied

func (m *Migration) IsApplied() bool

func (*Migration) String

func (m *Migration) String() string

type MigrationFile

type MigrationFile struct {
	Name    string
	Path    string
	Content string
}

type MigrationFunc

type MigrationFunc func(ctx context.Context, db *bun.Tx) error

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) Delete

func (m *Migrator) Delete(ctx context.Context) error

Delete deletes the migration tables.

func (*Migrator) Init

func (m *Migrator) Init(ctx context.Context) error

Init initializes the migration tables.

func (*Migrator) Migrate

func (m *Migrator) Migrate(ctx context.Context) error

Migrate runs unapplied migrations.

func (*Migrator) MigrationsWithStatus

func (m *Migrator) MigrationsWithStatus(ctx context.Context) (MigrationSlice, error)

MigrationsWithStatus returns migrations with status in ascending order.

func (*Migrator) Rollback

func (m *Migrator) Rollback(ctx context.Context) error

Rollback rollbacks last applied migration.

Jump to

Keyboard shortcuts

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