migrate

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Example
package main

import (
	"log"

	"github.com/rickbassham/database"
	"github.com/rickbassham/database/migrate"
)

func main() {
	db, err := database.New(nil)
	if err != nil {
		panic(err.Error())
	}

	migration, err := migrate.NewMigrateDB(db)
	if err != nil {
		panic(err.Error())
	}

	err = migration.Init()
	if err != nil {
		panic(err.Error())
	}

	migration.AddMigration(migrate.NewSQLMigration(1, "CREATE TABLE test (id integer, name varchar(50));"))

	version, err := migration.CurrentVersion()
	if err != nil {
		panic(err.Error())
	}

	log.Printf("starting version: %d\n", version)

	for migration.Upgrade() {
		version, err := migration.CurrentVersion()
		if err != nil {
			panic(err.Error())
		}

		log.Printf("now at version: %d\n", version)
	}

	if err = migration.Err(); err != nil {
		panic(err.Error())
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownMigrationType is returned when we don't recognize the migration file type.
	ErrUnknownMigrationType = errors.New("unknown migration type")

	// ErrInvalidMigrationFileName is returned when we the migration file doens't match the regex `(\d+)_(.*?)\.(sql|so)`.
	ErrInvalidMigrationFileName = errors.New("invalid migration file name")

	// ErrInvalidPluginMigration is returned when an invalid .so file was loaded as a plugin migration.
	ErrInvalidPluginMigration = errors.New("invalid plugin migration")
)

Functions

This section is empty.

Types

type Migration

type Migration interface {
	Runner
	Version() int
}

Migration represents a single migration.

type PluginMigration

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

PluginMigration is a migration loaded from a golang .so plugin.

func NewPluginMigration

func NewPluginMigration(path string) (m PluginMigration, err error)

NewPluginMigration creates a Migration from a golang .so plugin file.

func (PluginMigration) Run

Run will run the sql statement against the database.

func (PluginMigration) Version

func (m PluginMigration) Version() int

Version is the version number of the migration.

type Runner

type Runner interface {
	Run(*database.Database, database.Tx) error
}

Runner defines the method used to actually run a migration.

type SQLMigration

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

SQLMigration is a simple SQL statement based migration.

func NewSQLMigration

func NewSQLMigration(version int, statement string) SQLMigration

NewSQLMigration creates a new SQL migration.

func NewSQLMigrationFile

func NewSQLMigrationFile(path string) (m SQLMigration, err error)

NewSQLMigrationFile creates a new SQL migration from the given file. The file name must match the regex `(\d+)_(.*?)\.(sql|so)`.

func (SQLMigration) Run

func (m SQLMigration) Run(db *database.Database, tx database.Tx) error

Run will run the sql statement against the database.

func (SQLMigration) Version

func (m SQLMigration) Version() int

Version is the version number of the migration.

type Service

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

Service is responsible for holding state to upgrade the database.

func NewService

func NewService(db *database.Database) (*Service, error)

NewService creates a new Service.

func (*Service) AddMigration

func (svc *Service) AddMigration(m Migration)

AddMigration allows the user to add any custom migrations to be run. This is useful for more complex code-based migrations.

func (*Service) CurrentVersion

func (svc *Service) CurrentVersion() (int, error)

CurrentVersion returns the current version of our database.

func (*Service) Err

func (svc *Service) Err() error

Err returns any error that happened as part of Upgrade.

func (*Service) Init

func (svc *Service) Init() (err error)

Init ensures all necessary tables exist to keep migration history.

func (*Service) LoadMigrations

func (svc *Service) LoadMigrations(path string) error

LoadMigrations will load all of the sql and so files in the given path.

func (*Service) Upgrade

func (svc *Service) Upgrade() bool

Upgrade will upgrade the database by one version. Check Err() after running. Returns true if a migration was run, false otherwise.

Jump to

Keyboard shortcuts

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