trek

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 9, 2020 License: BSD-3-Clause Imports: 11 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 Migrate added in v0.0.6

func Migrate(driverName, dsn, migrationsPath string) error

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

func Rollback

func Rollback(driverName, dsn, migrationsPath string, steps int) error

Rollback rolls back the migrations in the given path, to the database using the given driver. If steps is provided as a positive integer, it only rolls back that many migrations.

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) 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) Migrate added in v0.0.6

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

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

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) Len

func (m Migrations) Len() int

func (Migrations) Less

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

func (Migrations) Migrate added in v0.0.6

func (m Migrations) Migrate(driver Driver, db *sql.DB) error

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

func (Migrations) Rollback

func (m Migrations) Rollback(driver Driver, db *sql.DB, steps int) 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