migrate

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

norm/migrate

The migration files in the provided fs.FS must match (\d+)_(\w+)\.(apply|discard)\.sql naming pattern.

Example:

.
`-- versions
	|-- 0001_orders_table.apply.sql
	|-- 0001_orders_table.discard.sql
	|-- 0002_users_table.apply.sql
	|-- 0002_users_table.discard.sql
	|-- 0003_payments_table.apply.sql
	`-- 0003_payments_table.discard.sql

In the current implementation each migration for either apply or discard is run inside its own transaction.

TODO

  • Add support for running migrations outside a transaction

Example

	ctx := context.Background()

	db, err := sql.Open("pgx", "postgres://user:pass@postgres:5432/db")
	if err != nil {
		// handle err
	}

	// use a local dir with migrations, could also be a embed.FS
	//go:embed versions/*
	// var versions embed.FS
	m, err := migrate.NewWithFiles(db, os.DirFS("./versions"), log.Printf)
	if err != nil {
		// handle err
	}

	// migrate all the way up
	err = m.Up(ctx)
	if err != nil {
		// handle err
	}

	// get current version
	v, err := m.Version(ctx)
	if err != nil {
		// handle err
	}

	fmt.Println(v.Version)


	// migrate forward or backward to a specific version
	err = m.Apply(ctx, 2)
	if err != nil {
		panic(err)
	}

	// migrate all the way down and remove migration history
	err = m.Down(ctx)
	if err != nil {
		panic(err)
	}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StdLog = log.Printf

StdLog is the log.Printf function from the standard library

Functions

This section is empty.

Types

type Logger

type Logger func(s string, args ...interface{})

Logger function signature

type Migrate

type Migrate struct {
	// contains filtered or unexported fields
}

Migrate manages database migrations

func New

func New(db *sql.DB, logger Logger, migrations []*Migration) (m *Migrate, err error)

New creates a new Migrate with the given database and versions.

If the provided logger function is not `nil` additional information will be logged during the migrations apply or discard.

func NewWithFiles

func NewWithFiles(db *sql.DB, files fs.FS, logger Logger) (m *Migrate, err error)

NewWithFiles is like new but takes a fs.Fs as a source for migration files. Only files within the 1st level of the provided path matching the `(\d+)_(\w+)\.(apply|discard)\.sql` pattern will be added to the Migrate catalog.

func (*Migrate) Apply

func (m *Migrate) Apply(ctx context.Context, version int64) (err error)

Apply either rolls forward or backwards the migrations to the specified version

func (*Migrate) Down

func (m *Migrate) Down(ctx context.Context) (err error)

Down discards all existing database migrations and migration history

func (*Migrate) Up

func (m *Migrate) Up(ctx context.Context) (err error)

Up apply all existing migrations to the database

func (*Migrate) Version

func (m *Migrate) Version(ctx context.Context) (version *Version, err error)

Version returns the current database migration version. If the database migrations are not initialized version is -1.

type Migration

type Migration struct {
	Version int64
	Name    string
	Apply   string
	Discard string
}

type Version

type Version struct {
	Version int64
	Date    time.Time
	Name    string
}

Jump to

Keyboard shortcuts

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