GoDoc Issue Tracker

The migration package provides a way to generate, list, and apply PostgreSQL database migrations. The package can be imported like so:

import (


The package may be used under the terms of the BSD 2-Clause License a copy of which may be found in the LICENSE file.

Unless you explicitly state otherwise, any contribution submitted for inclusion in the work by you shall be licensed as above, without any additional terms or conditions.

Expand ▾ Collapse ▴



    Package migration contains functions for generating and finding PostgreSQL database migrations.



    This section is empty.


    This section is empty.


    func Generator

    func Generator(basePath string) func(name string) error

      Generator returns a function that creates migration files at the given base path.

      func LastRun

      func LastRun(ctx context.Context, migrationsTable string, vfs fs.FS, tx *sql.Tx) (ident, name string, err error)

        LastRun returns the last migration directory by lexical order that exists in the database and on disk.

        func Setup

        func Setup(ctx context.Context, migrationsTable string, tx *sql.Tx) error

          Setup creates a migrations table with the given name.


          type RunStatus

          type RunStatus int

            RunStatus is a type that indicates if a migration has been run, not run, or if we can't determine the status.

            const (
            	StatusUnknown RunStatus = iota

              Valid RunStatus values. For more information see RunStatus.

              type WalkFunc

              type WalkFunc func(name string, info fs.DirEntry, status RunStatus) error

                WalkFunc is the type of the function called for each file or directory visited by a Walker.

                type Walker

                type Walker func(vfs fs.FS, f WalkFunc) error

                  Walker is a function that can be used to walk a filesystem and calls WalkFunc for each migration.

                  func NewWalker

                  func NewWalker(ctx context.Context, migrationsTable string, tx *sql.Tx) (Walker, error)

                    NewWalker queries the database for migration status information and returns a function that walks the migrations it finds on the filesystem in lexical order (mostly, keep reading) and calls a function for each discovered migration, passing in its name, status, and file information.

                    If a migration exists in the database but not on the filesystem, info will be nil and f will be called for it after the migrations that exist on the filesystem. No particular order is guaranteed for calls to f for migrations that do not exist on the filesystem.

                    If NewWalker returns an error and a non-nil function, the function may still be used to walk the migrations on the filesystem but the status information may be wrong since the DB may not have been queried successfully.