...

Package popx

import "github.com/ory/x/popx"
Overview
Index

Overview ▾

Index ▾

Constants
func ParameterizedMigrationContent(params map[string]interface{}) func(mf Migration, c *pop.Connection, r []byte, usingTemplate bool) (string, error)
func WithMigrationContentMiddleware(middleware func(content string, err error) (string, error)) func(*MigrationBox) *MigrationBox
func WithTemplateValues(v map[string]interface{}) func(*MigrationBox) *MigrationBox
type Migration
    func (mf Migration) Run(c *pop.Connection, tx *pop.Tx) error
type MigrationBox
    func NewMigrationBox(dir embed.FS, m *Migrator, opts ...func(*MigrationBox) *MigrationBox) (*MigrationBox, error)
type MigrationContent
type MigrationStatus
type MigrationStatuses
    func (m MigrationStatuses) HasPending() bool
    func (m MigrationStatuses) Header() []string
    func (m MigrationStatuses) IDs() []string
    func (m MigrationStatuses) Interface() interface{}
    func (m MigrationStatuses) Len() int
    func (m MigrationStatuses) Table() [][]string
    func (m MigrationStatuses) Write(out io.Writer) error
type Migrations
    func (mfs Migrations) Len() int
    func (mfs Migrations) Less(i, j int) bool
    func (mfs Migrations) SortAndFilter(dialect string, modifiers ...func(sort.Interface) sort.Interface) Migrations
    func (mfs Migrations) Swap(i, j int)
type Migrator
    func NewMigrator(c *pop.Connection, l *logrusx.Logger, tracer *tracing.Tracer, perMigrationTimeout time.Duration) *Migrator
    func (m *Migrator) CreateSchemaMigrations(ctx context.Context) error
    func (m *Migrator) Down(ctx context.Context, step int) error
    func (m *Migrator) DumpMigrationSchema(ctx context.Context) error
    func (m *Migrator) MigrationIsCompatible(dialect string, mi Migration) bool
    func (m *Migrator) Reset(ctx context.Context) error
    func (m *Migrator) Status(ctx context.Context) (MigrationStatuses, error)
    func (m *Migrator) Up(ctx context.Context) error
    func (m *Migrator) UpTo(ctx context.Context, step int) (applied int, err error)
type TestMigrator
    func NewTestMigrator(t *testing.T, c *pop.Connection, migrationPath, testDataPath string, l *logrusx.Logger) *TestMigrator

Package files

migration_box.go migration_content.go migration_info.go migrator.go span.go test_migrator.go

Constants

const (
    Pending = "Pending"
    Applied = "Applied"
)
const (
    MigrationStatusOpName         = "migration-status"
    MigrationInitOpName           = "migration-init"
    MigrationUpOpName             = "migration-up"
    MigrationRunTransactionOpName = "migration-run-transaction"
    MigrationDownOpName           = "migration-down"
)

func ParameterizedMigrationContent

func ParameterizedMigrationContent(params map[string]interface{}) func(mf Migration, c *pop.Connection, r []byte, usingTemplate bool) (string, error)

func WithMigrationContentMiddleware

func WithMigrationContentMiddleware(middleware func(content string, err error) (string, error)) func(*MigrationBox) *MigrationBox

func WithTemplateValues

func WithTemplateValues(v map[string]interface{}) func(*MigrationBox) *MigrationBox

type Migration

Migration handles the data for a given database migration

type Migration struct {
    // Path to the migration (./migrations/123_create_widgets.up.sql)
    Path string
    // Version of the migration (123)
    Version string
    // Name of the migration (create_widgets)
    Name string
    // Direction of the migration (up)
    Direction string
    // Type of migration (sql)
    Type string
    // DB type (all|postgres|mysql...)
    DBType string
    // Runner function to run/execute the migration
    Runner func(Migration, *pop.Connection, *pop.Tx) error
}

func (Migration) Run

func (mf Migration) Run(c *pop.Connection, tx *pop.Tx) error

Run the migration. Returns an error if there is no mf.Runner defined.

type MigrationBox

MigrationBox is a embed migration box.

type MigrationBox struct {
    *Migrator

    Dir embed.FS
    // contains filtered or unexported fields
}

func NewMigrationBox

func NewMigrationBox(dir embed.FS, m *Migrator, opts ...func(*MigrationBox) *MigrationBox) (*MigrationBox, error)

NewMigrationBox from a packr.Dir and a Connection.

migrations, err := NewMigrationBox(pkger.Dir("/migrations"))

type MigrationContent

type MigrationContent func(mf Migration, c *pop.Connection, r []byte, usingTemplate bool) (string, error)

type MigrationStatus

type MigrationStatus struct {
    State   string `json:"state"`
    Version string `json:"version"`
    Name    string `json:"name"`
}

type MigrationStatuses

type MigrationStatuses []MigrationStatus

func (MigrationStatuses) HasPending

func (m MigrationStatuses) HasPending() bool

func (MigrationStatuses) Header

func (m MigrationStatuses) Header() []string

func (MigrationStatuses) IDs

func (m MigrationStatuses) IDs() []string

func (MigrationStatuses) Interface

func (m MigrationStatuses) Interface() interface{}

func (MigrationStatuses) Len

func (m MigrationStatuses) Len() int

func (MigrationStatuses) Table

func (m MigrationStatuses) Table() [][]string

func (MigrationStatuses) Write

func (m MigrationStatuses) Write(out io.Writer) error

In the context of a cobra.Command, use cmdx.PrintTable instead.

type Migrations

Migrations is a collection of Migration

type Migrations []Migration

func (Migrations) Len

func (mfs Migrations) Len() int

func (Migrations) Less

func (mfs Migrations) Less(i, j int) bool

func (Migrations) SortAndFilter

func (mfs Migrations) SortAndFilter(dialect string, modifiers ...func(sort.Interface) sort.Interface) Migrations

func (Migrations) Swap

func (mfs Migrations) Swap(i, j int)

type Migrator

Migrator forms the basis of all migrations systems. It does the actual heavy lifting of running migrations. When building a new migration system, you should embed this type into your migrator.

type Migrator struct {
    Connection *pop.Connection
    SchemaPath string
    Migrations map[string]Migrations

    PerMigrationTimeout time.Duration
    // contains filtered or unexported fields
}

func NewMigrator

func NewMigrator(c *pop.Connection, l *logrusx.Logger, tracer *tracing.Tracer, perMigrationTimeout time.Duration) *Migrator

NewMigrator returns a new "blank" migrator. It is recommended to use something like MigrationBox or FileMigrator. A "blank" Migrator should only be used as the basis for a new type of migration system.

func (*Migrator) CreateSchemaMigrations

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

CreateSchemaMigrations sets up a table to track migrations. This is an idempotent operation.

func (*Migrator) Down

func (m *Migrator) Down(ctx context.Context, step int) error

Down runs pending "down" migrations and rolls back the database by the specified number of steps.

func (*Migrator) DumpMigrationSchema

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

DumpMigrationSchema will generate a file of the current database schema based on the value of Migrator.SchemaPath

func (*Migrator) MigrationIsCompatible

func (m *Migrator) MigrationIsCompatible(dialect string, mi Migration) bool

func (*Migrator) Reset

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

Reset the database by running the down migrations followed by the up migrations.

func (*Migrator) Status

func (m *Migrator) Status(ctx context.Context) (MigrationStatuses, error)

Status prints out the status of applied/pending migrations.

func (*Migrator) Up

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

Up runs pending "up" migrations and applies them to the database.

func (*Migrator) UpTo

func (m *Migrator) UpTo(ctx context.Context, step int) (applied int, err error)

UpTo runs up to step "up" migrations and applies them to the database. If step <= 0 all pending migrations are run.

type TestMigrator

TestMigrator is a modified pop.FileMigrator

type TestMigrator struct {
    *Migrator
}

func NewTestMigrator

func NewTestMigrator(t *testing.T, c *pop.Connection, migrationPath, testDataPath string, l *logrusx.Logger) *TestMigrator

Returns a new TestMigrator After running each migration it applies it's corresponding testData sql files. They are identified by having the same version (= number in the front of the filename). The filenames are expected to be of the format ([0-9]+).*(_testdata(\.[dbtype])?.sql