gomigrate

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: May 22, 2021 License: MIT Imports: 10 Imported by: 0

README

gomigrate

Build Status

A SQL database migration toolkit in Golang.

This is a fork of https://github.com/DavidHuie/gomigrate that adds support for reading migration files embedded directly within the built executable, or from any other source available via the fs.FS interface present in Go 1.16 and up. This simplifies deployment by removing the need to install and manage a separate migrations directory.

The previous file-based API also remains supported, even though the implementation now uses fs.FS internally.

Supported databases

  • PostgreSQL
  • MariaDB
  • MySQL
  • Sqlite3

Usage

First import the Go 1.16 embed package and this package:

import (
	"embed"
	"github.com/ashearer/gomigrate"
)

Given a database/sql database connection to a PostgreSQL database, db, and a directory named migrations in the same directory as the source code, create a migrator that reads files within the embedded migrations directory:

//go:embed migrations
var migrationsDirFS embed.FS // files will be embedded under path "migrations/"

// make an FS to access the files without the "migrations/" path prefix
migrationsFS, err := fs.Sub(migrationsDirFS, "migrations")
if err != nil {
	log.Fatal(err)
}
migrator, _ := gomigrate.NewMigratorFS(db, gomigrate.Postgres{}, migrationsFS)

You may also specify a logger, such as logrus:

migrator, _ := gomigrate.NewMigratorFSWithLogger(db, gomigrate.Postgres{}, migrationsFS, logrus.New())

To migrate the database, run:

err := migrator.Migrate()

To rollback the last migration, run:

err := migrator.Rollback()

Migration files

Migration files need to follow a standard format and must be present in the same directory. Given "up" and "down" steps for a migration, create a file for each by following this template:

{{ id }}_{{ name }}_{{ "up" or "down" }}.sql

For a given migration, the id and name fields must be the same. The id field is an integer that corresponds to the order in which the migration should run relative to the other migrations.

id should not be 0 as that value is used for internal validations.

Note that in the example above of embedding migrations, Go will automatically skip filenames beginning with a period or underscore. This is typically useful, but could be overridden if necessary by using a wildcard glob pattern.

Example

If I'm trying to add a "users" table to the database, I would create the following two files:

1_add_users_table_up.sql
CREATE TABLE users();
1_add_users_table_down.sql
DROP TABLE users;

Requirements

Go 1.16 or higher.

Copyright © 2014-2021 David Huie and Andrew Shearer. Based on https://github.com/DavidHuie/gomigrate by David Huie. See LICENSE.txt for further details.

Documentation

Index

Constants

View Source
const (
	Inactive = iota
	Active
)

Migration statuses.

Variables

View Source
var (
	InvalidMigrationFile  = errors.New("Invalid migration file")
	InvalidMigrationPair  = errors.New("Invalid pair of migration files")
	InvalidMigrationsPath = errors.New("Invalid migrations path")
	InvalidMigrationType  = errors.New("Invalid migration type")
	NoActiveMigrations    = errors.New("No active migrations to rollback")
)

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
	Fatalf(format string, v ...interface{})
}

type Mariadb

type Mariadb struct {
	Mysql
}

type Migratable

type Migratable interface {
	SelectMigrationTableSql() string
	CreateMigrationTableSql() string
	GetMigrationSql() string
	MigrationLogInsertSql() string
	MigrationLogDeleteSql() string
	GetMigrationCommands(string) []string
}

type Migration

type Migration struct {
	DownPath string
	Id       uint64
	Name     string
	Status   int
	UpPath   string
}

Holds configuration information for a given migration.

type Migrator

type Migrator struct {
	DB *sql.DB
	// contains filtered or unexported fields
}

func NewMigrator

func NewMigrator(db *sql.DB, adapter Migratable, migrationsPath string) (*Migrator, error)

Returns a new migrator based on a directory pathname.

func NewMigratorFS

func NewMigratorFS(db *sql.DB, adapter Migratable, migrationsFS fs.FS) (*Migrator, error)

Returns a new migrator based on an fs.FS filesystem.

func NewMigratorFSWithLogger

func NewMigratorFSWithLogger(db *sql.DB, adapter Migratable, migrationsFS fs.FS, logger Logger) (*Migrator, error)

Returns a new migrator with the specified logger based on an fs.FS filesystem.

func NewMigratorWithLogger

func NewMigratorWithLogger(db *sql.DB, adapter Migratable, migrationsPath string, logger Logger) (*Migrator, error)

Returns a new migrator with the specified logger based on a directory pathname.

func (*Migrator) ApplyMigration

func (m *Migrator) ApplyMigration(migration *Migration, mType migrationType) error

Applies a single migration.

func (*Migrator) CreateMigrationsTable

func (m *Migrator) CreateMigrationsTable() error

Creates the migrations table if it doesn't exist.

func (*Migrator) Migrate

func (m *Migrator) Migrate() error

Applies all inactive migrations.

func (*Migrator) MigrationTableExists

func (m *Migrator) MigrationTableExists() (bool, error)

Returns true if the migration table already exists.

func (*Migrator) Migrations

func (m *Migrator) Migrations(status int) []*Migration

Returns a sorted list of migration ids for a given status. -1 returns all migrations.

func (*Migrator) Rollback

func (m *Migrator) Rollback() error

Rolls back the last migration.

func (*Migrator) RollbackAll

func (m *Migrator) RollbackAll() error

Rolls back all migrations.

func (*Migrator) RollbackN

func (m *Migrator) RollbackN(n int) error

Rolls back N migrations.

type Mysql

type Mysql struct{}

func (Mysql) CreateMigrationTableSql

func (m Mysql) CreateMigrationTableSql() string

func (Mysql) GetMigrationCommands

func (m Mysql) GetMigrationCommands(sql string) []string

func (Mysql) GetMigrationSql

func (m Mysql) GetMigrationSql() string

func (Mysql) MigrationLogDeleteSql

func (m Mysql) MigrationLogDeleteSql() string

func (Mysql) MigrationLogInsertSql

func (m Mysql) MigrationLogInsertSql() string

func (Mysql) SelectMigrationTableSql

func (m Mysql) SelectMigrationTableSql() string

type Postgres

type Postgres struct{}

func (Postgres) CreateMigrationTableSql

func (p Postgres) CreateMigrationTableSql() string

func (Postgres) GetMigrationCommands

func (p Postgres) GetMigrationCommands(sql string) []string

func (Postgres) GetMigrationSql

func (p Postgres) GetMigrationSql() string

func (Postgres) MigrationLogDeleteSql

func (p Postgres) MigrationLogDeleteSql() string

func (Postgres) MigrationLogInsertSql

func (p Postgres) MigrationLogInsertSql() string

func (Postgres) SelectMigrationTableSql

func (p Postgres) SelectMigrationTableSql() string

type SqlServer

type SqlServer struct{}

func (SqlServer) CreateMigrationTableSql

func (s SqlServer) CreateMigrationTableSql() string

func (SqlServer) GetMigrationCommands

func (s SqlServer) GetMigrationCommands(sql string) []string

func (SqlServer) GetMigrationSql

func (s SqlServer) GetMigrationSql() string

func (SqlServer) MigrationLogDeleteSql

func (s SqlServer) MigrationLogDeleteSql() string

func (SqlServer) MigrationLogInsertSql

func (s SqlServer) MigrationLogInsertSql() string

func (SqlServer) SelectMigrationTableSql

func (s SqlServer) SelectMigrationTableSql() string

type Sqlite3

type Sqlite3 struct{}

func (Sqlite3) CreateMigrationTableSql

func (s Sqlite3) CreateMigrationTableSql() string

func (Sqlite3) GetMigrationCommands

func (s Sqlite3) GetMigrationCommands(sql string) []string

func (Sqlite3) GetMigrationSql

func (s Sqlite3) GetMigrationSql() string

func (Sqlite3) MigrationLogDeleteSql

func (s Sqlite3) MigrationLogDeleteSql() string

func (Sqlite3) MigrationLogInsertSql

func (s Sqlite3) MigrationLogInsertSql() string

func (Sqlite3) SelectMigrationTableSql

func (s Sqlite3) SelectMigrationTableSql() string

Jump to

Keyboard shortcuts

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