sqlmigr

package
v1.0.0-rc10 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2021 License: MIT Imports: 15 Imported by: 2

Documentation

Overview

Package sqlmigr provides primitives and functions to work with SQL sqlmigrs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flog

func Flog(logger log.Logger, migrations []*Migration)

Flog prints the migrations as fields

func Ftable

func Ftable(w io.Writer, migrations []*Migration)

Ftable prints the migrations as table

func IsNotExist

func IsNotExist(err error) bool

IsNotExist reports if the error is because of migration table not exists

func RunAll

func RunAll(db *sqlx.DB, storage FileSystem) error

RunAll runs all sqlmigrs

Types

type Content

type Content struct {
	// UpCommand is the content for upgrade operation.
	UpCommand io.Reader
	// DownCommand is the content for rollback operation.
	DownCommand io.Reader
}

Content represents a migration content.

type Executor

type Executor struct {
	// Logger logs each execution step
	Logger log.Logger
	// Provider provides all migrations for the current project.
	Provider MigrationProvider
	// Runner runs or reverts migrations for the current project.
	Runner MigrationRunner
	// Generator generates a migration file.
	Generator MigrationGenerator
}

Executor provides a group of operations that works with migrations.

func (*Executor) Create

func (m *Executor) Create(name string) (*Migration, error)

Create creates a migration script successfully if the project has already been setup, otherwise returns an error.

func (*Executor) Migrations

func (m *Executor) Migrations() ([]*Migration, error)

Migrations returns all migrations.

func (*Executor) Revert

func (m *Executor) Revert(step int) (int, error)

Revert reverts an applied migration for given count. If the count is negative number, it will revert all applied migrations.

func (*Executor) RevertAll

func (m *Executor) RevertAll() (int, error)

RevertAll reverts all applied migrations.

func (*Executor) Run

func (m *Executor) Run(step int) (int, error)

Run runs a pending migration for given count. If the count is negative number, it will execute all pending migrations.

func (*Executor) RunAll

func (m *Executor) RunAll() (int, error)

RunAll runs all pending migrations.

func (*Executor) Setup

func (m *Executor) Setup() error

Setup setups the current project for database migrations by creating migration directory and related database.

type FileSystem

type FileSystem = fs.FS

FileSystem provides with primitives to work with the underlying file system

type Generator

type Generator struct {
	// FileSystem is the file system where all sqlmigrs are created.
	FileSystem WriteFileSystem
}

Generator generates a new sqlmigr file for given directory.

func (*Generator) Create

func (g *Generator) Create(m *Migration) error

Create creates a new sqlmigr.

func (*Generator) Write

func (g *Generator) Write(m *Migration, content *Content) error

Write creates a new sqlmigr for given content.

type Migration

type Migration struct {
	// Id is the primary key for this sqlmigr
	ID string `db:"id"`
	// Description is the short description of this sqlmigr.
	Description string `db:"description"`
	// CreatedAt returns the time of sqlmigr execution.
	CreatedAt time.Time `db:"created_at"`
	// Drivers return all supported drivers
	Drivers []string `db:"-"`
}

Migration represents a single migration record.

func Parse

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

Parse parses a given file path to a sqlmigr item.

func (*Migration) Equal

func (m *Migration) Equal(migration *Migration) bool

Equal returns true if the migrations are equal

func (*Migration) Filenames

func (m *Migration) Filenames() []string

Filenames return the migration filenames

func (*Migration) String

func (m *Migration) String() string

String returns the migration as string

type MigrationGenerator

type MigrationGenerator interface {
	// Create creates a new sqlmigr.
	Create(m *Migration) error
	// Write creates a new sqlmigr for given content.
	Write(m *Migration, content *Content) error
}

MigrationGenerator generates a migration item file.

type MigrationProvider

type MigrationProvider interface {
	// Migrations returns all sqlmigr items.
	Migrations() ([]*Migration, error)
	// Insert inserts executed sqlmigr item in the sqlmigrs table.
	Insert(item *Migration) error
	// Delete deletes applied sqlmigr item from sqlmigrs table.
	Delete(item *Migration) error
	// Exists returns true if the sqlmigr exists
	Exists(item *Migration) bool
}

MigrationProvider provides all items.

type MigrationRunner

type MigrationRunner interface {
	// Run runs a given sqlmigr item.
	Run(item *Migration) error
	// Revert reverts a given sqlmigr item.
	Revert(item *Migration) error
}

MigrationRunner runs or reverts a given sqlmigr item.

type Provider

type Provider struct {
	// FileSystem represents the project directory file system.
	FileSystem FileSystem
	// DB is a client to underlying database.
	DB *sqlx.DB
}

Provider provides all migration for given project.

func (*Provider) Delete

func (m *Provider) Delete(item *Migration) error

Delete deletes applied sqlmigr item from sqlmigrs table.

func (*Provider) Exists

func (m *Provider) Exists(item *Migration) bool

Exists returns true if the sqlmigr exists

func (*Provider) Insert

func (m *Provider) Insert(item *Migration) error

Insert inserts executed sqlmigr item in the sqlmigrs table.

func (*Provider) Migrations

func (m *Provider) Migrations() ([]*Migration, error)

Migrations returns the project migrations.

type Runner

type Runner struct {
	// FileSystem represents the project directory file system.
	FileSystem FileSystem
	// DB is a client to underlying database.
	DB *sqlx.DB
}

Runner runs or reverts a given migration item.

func (*Runner) Revert

func (r *Runner) Revert(m *Migration) error

Revert reverts a given migration item.

func (*Runner) Run

func (r *Runner) Run(m *Migration) error

Run runs a given migration item.

type RunnerError

type RunnerError struct {
	// Err the actual error
	Err error
	// Statement that cause the issue
	Statement string
}

RunnerError represents a runner error

func (*RunnerError) Error

func (e *RunnerError) Error() string

Error returns the error as string

type WriteFileSystem

type WriteFileSystem interface {
	FileSystem

	// OpenFile opens a new file
	OpenFile(string, int, fs.FileMode) (fs.File, error)
}

WriteFileSystem represents a wriable file system

Jump to

Keyboard shortcuts

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