migrator

package module
v0.0.0-...-2e773cc Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2017 License: MIT Imports: 8 Imported by: 0

README

migrator

Build Status GoDoc Go Report Card

Migrator is a simple tool to aid in the migration of databases. Inspired by how the folks over at StackOverflow manage their database migrations, simply add your SQL scripts into a directory, configure Migrator and run the executable. Alternatively, if you want to use migrator in your application, you can. The same library that powers the executable is available to you.

At this moment in time, Migrator supports only MySQL. However, if there is enough demand I'm happy to create/merge pull requests for other database libraries.

Migrator is not designed to have all of the bells and whistles that other libraries have; it's meant to be simple and minimalistic whilst still having a use. If you want a migration tool that has a number of other great features, I'd suggest looking into one of the repositories listed below:

Getting started

Get the executable:

go get github.com/bunsenapp/migrator/cmd/migrator

Migrator assumes that all of your database migrations are named as per the following format:

ID_name_ACTION.sql

where:

  • ID is the order in which the migrations should be ran
  • name is the name of your migration, it can contain any characters other than an underscore
  • action is a choice of either up or down

For example:

  • 1_my-test-migration_up.sql - MIGRATION
  • 1_my-test-migration_down.sql - ROLLBACK
Executable

The executable has the following usage:

Usage: migrator COMMAND [OPTIONS] [MIGRATION FILE NAME TO ROLLBACK]

A super simple tool to run database migrations.

Commands:
	migrate  Run migrations that don't exist in the database
	rollback Rollback a specific migration

Options:
	-connection-string  The connection string of the database to run the migrations on (default is .)
	-migration-dir      The directory where the UP migration scripts are stored (default is migrations/up)
	-rollback-dir       The directory where the DOWN migration scripts are stored (default is migrations/down)
	-type               The type of database you are connecting to (MySQL) (default is mysql)
Running migrations

To run a migration, you can use a command like the below:

migrator migrate -connection-string root:password@localhost/dbname -migration-dir m/up -rollback-dir m/down -type mysql
Rolling back migrations

To roll back a migration, you can use a command like the below:

migrator rollback -connection-string root:password@localhost/dbname -migration-dir m/up -rollback-dir m/down -type mysql 1_my-migration-name_up.sql

Migrator only lets you roll back a single migration at a time to ensure you are absolutely comfortable with what is happening.

Library

You can also use the library by following the below steps:

  • Get the library: go get github.com/bunsenapp/migrator
  • Go get your chosen database driver (for MySQL: go get github.com/bunsenapp/migrator/mysql)
  • Import it within your application.
  • Create an instance of the Configuration struct, setting the appropriate values:
    config := migrator.Configuration{
		DatabaseConnectionString: "connection-string",
		MigrationsDir: "migration-dir/",
		RollbacksDir: "rollbacks-dir/",
		MigrationToRollback: "1_test_up.sql", // Only required if you are executing a rollback
	}
  • Create a logging instance that implements the migrator.LogServicer interface.
  • Call the NewMigrator function with the required parameters migrator := migrator.NewMigrator(config, mysql.NewMySQLDatabaseServicer(config.DatabaseConnectionString, logImplementation)
  • Call the appropriate method on the Migrator struct:
    migrator.Migrate()
	
	or

	migrator.Rollback("1_test_up.sql")

Documentation

Overview

Package migrator is a library that makes it super simple to run database migrations.

Index

Constants

View Source
const (
	// MySQLDatabaseType represents a MySQL database type.
	MySQLDatabaseType = iota

	// PostgreSQLDatabaseType represents a PostgreSQL database type.
	PostgreSQLDatabaseType
)

Variables

View Source
var (
	// ErrConfigurationInvalid is an error that is raised when a configuration
	// property is invalid.
	ErrConfigurationInvalid = errors.New("configuration of Migrator is invalid")

	// ErrDbServicerNotInitialised is an error that is raised when the database
	// server on the cmd.Migration struct has not been initialised.
	ErrDbServicerNotInitialised = errors.New("database servicer not initialised")

	// ErrNoMigrationsInDir is an error that is raised when the migrations directory
	// does not have any migration scripts.
	ErrNoMigrationsInDir = errors.New("no migration files in configured migrations dir")

	// ErrNoRollbacksInDir is an error that is raised when the rollbacks directory does
	// not have any rollback scripts.
	ErrNoRollbacksInDir = errors.New("no rollback files in configured rollbacks dir")

	// ErrUnableToRetrieveRanMigrations is an error that is raised when the
	// migration history table cannot be queried.
	ErrUnableToRetrieveRanMigrations = errors.New("unable to retrieve ran migrations")

	// ErrCreatingDbTransaction is an error that is raised when the application
	// is unable to create a transaction in the database.
	ErrCreatingDbTransaction = errors.New("unable to create database transaction")

	// ErrNotLatestMigration is an error that is raised when the user
	// tries to rollback a migration which is not the latest migration. This
	// was explicitly designed in this way to ensure the user has knowledge
	// of every single migration that they are rolling back.
	ErrNotLatestMigration = errors.New("cannot rollback a not-latest migration")

	// ErrCommittingTransaction is an error that is raised when the application
	// is, for some reason, unable to commit the transaction to the database.
	ErrCommittingTransaction = errors.New("unable to commit database transaction")
)

Functions

func NewErrCreatingHistoryTable

func NewErrCreatingHistoryTable(err error) error

NewErrCreatingHistoryTable creates a new instance of the ErrCreatingHistoryTable struct.

func NewErrInvalidMigrationID

func NewErrInvalidMigrationID(migration string, err error) error

NewErrInvalidMigrationID creates a new instance of the ErrInvalidMigrationId struct.

func NewErrMissingRollbackFile

func NewErrMissingRollbackFile(file string) error

NewErrMissingRollbackFile creates a new instance of the ErrMissingRollbackFile struct.

func NewErrReadingFile

func NewErrReadingFile(file string, err error) error

NewErrReadingFile creates a new instance of the ErrReadingFile struct.

func NewErrRunningMigration

func NewErrRunningMigration(m Migration, err error) error

NewErrRunningMigration creates a new instance of the ErrRunningMigration struct.

func NewErrRunningRollback

func NewErrRunningRollback(r Rollback, err error) error

NewErrRunningRollback creates a new instance of the ErrRunningRollback struct.

func NewErrSearchingDir

func NewErrSearchingDir(dir string, err error) error

NewErrSearchingDir creates a new instance of the ErrSearchingDir struct.

Types

type Configuration

type Configuration struct {
	// DatabaseConnectionString is the connection string where the migrations
	// will be ran against.
	DatabaseConnectionString string

	// MigrationsDir is the directory where the migration SQL scripts
	// are stored.
	MigrationsDir string

	// RollbacksDir is the directory where the rollback SQL scripts
	// are stored.
	RollbacksDir string

	// MigrationToRollback is the migration that needs to be rolled back. This
	// is useful when a development mistake may have been made.
	MigrationToRollback string
}

Configuration is an object where the configuration of migrator is stored.

func (Configuration) Validate

func (c Configuration) Validate() error

Validate validates the configuration object ensuring it is ready to be used within the Migrator application.

type DatabaseServicer

type DatabaseServicer interface {
	// BeginTransaction creates a transaction in the implementing database
	// servicer.
	BeginTransaction() error

	// CommitTransaction ends the created transaction providing there is one
	// and commits it to the database.
	CommitTransaction() error

	// RanMigrations retrieves all previously ran migrations.
	RanMigrations() ([]RanMigration, error)

	// RemoveMigrationHistory removes the specified migration from the
	// history table.
	RemoveMigrationHistory(m Migration) error

	// RollbackTransaction ends the created transaction providing there is one
	// and rolls it back, ensuring there are no changes to the database made.
	RollbackTransaction() error

	// RollbackMigration rolls back the specified migration and removes it
	// from the RanMigrations table.
	RollbackMigration(m Migration) error

	// RunMigration runs the specified migration against the current database.
	RunMigration(m Migration) error

	// TryCreateHistoryTable creates the migration history table if it does
	// not already exist. The boolean return value indicates whether or not
	// the table had to be created.
	TryCreateHistoryTable() (bool, error)

	// WriteMigrationHistory writes that a migration has been ran into the
	// migration history table.
	WriteMigrationHistory(m Migration) error
}

DatabaseServicer represents a service that runs the migrations.

type ErrCreatingHistoryTable

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

ErrCreatingHistoryTable is an error that is raised when the application is unable to create the migration history table.

func (ErrCreatingHistoryTable) Error

func (e ErrCreatingHistoryTable) Error() string

Error yields the error string for the ErrCreatingHistoryTable struct.

type ErrInvalidMigrationID

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

ErrInvalidMigrationID is an error that is raised when the index on a migration files does not successfully convert to an integer.

func (ErrInvalidMigrationID) Error

func (e ErrInvalidMigrationID) Error() string

Error yields the error string for the ErrInvalidMigrationId struct.

type ErrMissingRollbackFile

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

ErrMissingRollbackFile is an error that is raised when a migration file does not have a corresponding rollback file (in the format of ROLLBACK-{original file name}).

func (ErrMissingRollbackFile) Error

func (e ErrMissingRollbackFile) Error() string

Error yields the error string for the ErrMissingRollbackFile error struct.

type ErrReadingFile

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

ErrReadingFile is an error that is raised when the application is unable to read a migration/rollback file.

func (ErrReadingFile) Error

func (e ErrReadingFile) Error() string

Error yields the error string for the ErrReadingFile struct.

type ErrRunningMigration

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

ErrRunningMigration is an error that is raised when the application fails to run a migration.

func (ErrRunningMigration) Error

func (e ErrRunningMigration) Error() string

Error yields the error string for the ErrRunningMigration struct.

type ErrRunningRollback

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

ErrRunningRollback is an error that is raised when the application fails to run a rollback.

func (ErrRunningRollback) Error

func (e ErrRunningRollback) Error() string

Error yields the error string for the ErrRunningRollback struct.

type ErrSearchingDir

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

ErrSearchingDir is an error that is raised when the searching of a directory fails.

func (ErrSearchingDir) Error

func (e ErrSearchingDir) Error() string

Error yields the error string for the ErrSearchingDir error struct.

type LogServicer

type LogServicer interface {
	// Printf formats a string with a set of parameters before printing it to
	// the output.
	Printf(format string, v ...interface{})
}

LogServicer abstracts common logging functions so we do not have to call the log.Logger implementation directly.

type Migration

type Migration struct {
	// ID represents where the migration is in the order of those to be
	// completed.
	ID int

	// FileName is the file name of the migration.
	FileName string

	// FileContents is the contents of the migration to run.
	FileContents []byte

	// Rollback is the rollback file for the current migration. There must
	// always be one; otherwise an error will occur.
	Rollback Rollback
}

Migration is a representation of a migration that needs to run.

type Migrator

type Migrator struct {
	// Config is the configuration object of the
	Config Configuration

	// DatabaseServicer is the service that performs all database operations.
	DatabaseServicer DatabaseServicer

	// LogServicer is the service that will perform all logging routines.
	// This abstraction exists only to decouple the application from the
	// implementation of log.Logger.
	LogServicer LogServicer
}

Migrator is the main application to be tested.

func NewMigrator

func NewMigrator(c Configuration, db DatabaseServicer, l LogServicer) (Migrator, error)

NewMigrator initialises a set up migrator that can be used without having to manually construct dependencies. You must inject a LogServicer implementation into this function. You will be able to use most logging libraries with it.

func (Migrator) Migrate

func (m Migrator) Migrate() error

Migrate migrates all available migrations.

func (Migrator) Rollback

func (m Migrator) Rollback(name string) error

Rollback rolls back a specified transaction.

type RanMigration

type RanMigration struct {
	// ID is the identifier of the migration that was ran.
	ID int

	// FileName is the name of the migration.
	FileName string

	// Ran is when the migration was ran into the database.
	Ran time.Time
}

RanMigration is a representation of a migration that was previously ran into the database.

type Rollback

type Rollback struct {
	// FileName is the file name of the rollback.
	FileName string

	// FileContents is the contents of the associated rollback.
	FileContents []byte
}

Rollback is a rollback script related to a migration.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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