trek

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2019 License: BSD-3-Clause Imports: 12 Imported by: 0

README

trek

Package for applying and rolling back migrations in SQL databases.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(logger *log.Logger, driverName, dsn, migrationsPath string) error

Apply applies the migrations in the given path, to the database using the given driver.

func Rollback

func Rollback(logger *log.Logger, driverName, dsn, migrationsPath string) error

Rollback rolls back the migrations in the given path, to the database using the given driver.

Types

type Driver

type Driver interface {
	CreateVersionsTable(db *sql.DB) error
	HasVersionBeenExecuted(db *sql.DB, version string) (bool, error)
	MarkVersionAsExecuted(tx *sql.Tx, version string) error
	UnmarkVersionAsExecuted(tx *sql.Tx, version string) error
}

Driver ...

type Migration

type Migration struct {
	Name     string
	Path     string
	Version  time.Time
	Contents struct {
		Up   string
		Down string
	}
}

Migration represents a single migration to apply to or rollback from a database.

func NewMigration

func NewMigration(path string) (*Migration, error)

NewMigration returns an initialised migration read from the given file path.

func (*Migration) Apply

func (m *Migration) Apply(driver Driver, db *sql.DB) error

Apply applies the migration to the given database using the given driver implementation.

func (*Migration) HasBeenMigrated

func (m *Migration) HasBeenMigrated(driver Driver, db *sql.DB) (bool, error)

HasBeenMigrated checks if the migration has been applied to the given database.

func (*Migration) Rollback

func (m *Migration) Rollback(driver Driver, db *sql.DB) error

Rollback rolls back the migration from the given database using the given driver implementation.

func (*Migration) VersionAsString

func (m *Migration) VersionAsString() string

VersionAsString returns the migration version as a string.

type Migrations

type Migrations []*Migration

Migrations represents a set of migrations to apply to or rollback from a database.

func LoadMigrations added in v0.0.2

func LoadMigrations(migrationsPath string) (Migrations, error)

LoadMigrations returns all the loaded migrations from the given directory path.

func (Migrations) Apply

func (m Migrations) Apply(logger *log.Logger, driver Driver, db *sql.DB) error

Apply applies all the migrations that have not already been applied to the given database.

func (Migrations) Len

func (m Migrations) Len() int

func (Migrations) Less

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

func (Migrations) Rollback

func (m Migrations) Rollback(logger *log.Logger, driver Driver, db *sql.DB) error

Rollback rolls back all the migrations that have been applied to the given database.

func (Migrations) Swap

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

type PostgresDriver

type PostgresDriver struct{}

PostgresDriver is a PostgreSQL implementation of the Driver interface.

func (*PostgresDriver) CreateVersionsTable

func (d *PostgresDriver) CreateVersionsTable(db *sql.DB) error

CreateVersionsTable creates the table for storing migrated versions if it doesn't exist.

func (*PostgresDriver) HasVersionBeenExecuted

func (d *PostgresDriver) HasVersionBeenExecuted(db *sql.DB, version string) (bool, error)

HasVersionBeenExecuted checks if the given version has been executed.

func (*PostgresDriver) MarkVersionAsExecuted

func (d *PostgresDriver) MarkVersionAsExecuted(tx *sql.Tx, version string) error

MarkVersionAsExecuted marks a given version as been executed.

func (*PostgresDriver) UnmarkVersionAsExecuted

func (d *PostgresDriver) UnmarkVersionAsExecuted(tx *sql.Tx, version string) error

UnmarkVersionAsExecuted unmarks a given version as been executed.

type SQLiteDriver

type SQLiteDriver struct {
	PostgresDriver
}

SQLiteDriver is a SQLite implementation of the Driver interface.

Jump to

Keyboard shortcuts

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