migrations

package
v0.0.0-...-f306a79 Latest Latest
Warning

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

Go to latest
Published: May 31, 2020 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package migrations manages the state of the Catena database. SQL files should be added to this directory that implement DDL commands that update the schema of the database that Catena is connected to. The SQL files are then compiled into the binary using the catena makemigrations command along with go generate. The catena server and command can compare the state of the database with its expected state and run any migrations that are required.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(r int64, conn *sql.DB) (n int, err error)

Migrate the database to the specified revision, if the revision is negative, then apply all unapplied migrations to the database. If the revision is less than the current revision, then the database is rolled back to that state. This function cannot drop the migrations table, use the Delete() function to completely rollback all migrations and delete the migrations table. Returns the total number of migrations that were executed against the database.

func New

func New(name, dir string) (path string, err error)

New creates a new migration file from a template for the next revision and checks to make sure that it is valid. Specify the migrations directory for verification.

func Num

func Num() int

Num returns the number of migrations in the package

func Refresh

func Refresh(conn *sql.DB) (err error)

Refresh the state of the migrations from the database.

Types

type ByRevision

type ByRevision []Migration

ByRevision implements sort.Interface for []Migration based on the Revision field.

func (ByRevision) Len

func (a ByRevision) Len() int

func (ByRevision) Less

func (a ByRevision) Less(i, j int) bool

func (ByRevision) Swap

func (a ByRevision) Swap(i, j int)

type Migration

type Migration struct {
	Revision int64     // the unique id of the migration, prefix from the migration file
	Name     string    // the human readable name of the migration, suffix of migration file
	Active   bool      // if the migration has been applied or not
	Applied  time.Time // the timestamp the migration was applied
	Created  time.Time // the timestamp the migration was created in the database
	// contains filtered or unexported fields
}

Migration combines the information about the state of the database and how it has been migrated from the migrations table alongside the migration code stored in SQL files and compiled into the binary using go generate.

func Current

func Current(conn *sql.DB) (Migration, error)

Current returns the most recently applied revision.

func Parse

func Parse(filename string) (m *Migration, err error)

Parse a migration file into an unsynchronized migration struct. This function is only used by go generate and though it can help users diagnose migration parsing issues, is generally not useful outside of the package.

func Revision

func Revision(r int64, conn *sql.DB) (Migration, error)

Revision returns the migration for the specified revision.

func (*Migration) DBSync

func (m *Migration) DBSync() bool

DBSync returns true if the migration is in the database.

func (*Migration) Down

func (m *Migration) Down(conn *sql.DB) (err error)

Down rolls back the migration from the database.

func (*Migration) DownSQL

func (m *Migration) DownSQL() string

DownSQL returns the query that will be executed when Down() is run

func (*Migration) Filename

func (m *Migration) Filename() string

Filename returns the original filename (before generation)

func (*Migration) Predecessors

func (m *Migration) Predecessors() (n int)

Predecessors returns the number of migrations before this migration.

func (*Migration) String

func (m *Migration) String() string

func (*Migration) Successors

func (m *Migration) Successors() (n int)

Successors returns the number of migrations after this migration.

func (*Migration) Up

func (m *Migration) Up(conn *sql.DB) (err error)

Up applies the migration to the database.

func (*Migration) UpSQL

func (m *Migration) UpSQL() string

UpSQL returns the query that will be executed when Up() is run

Jump to

Keyboard shortcuts

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