sqlize

package
v0.0.0-...-d988ceb Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MigrationUp     = Action(0)
	MigrationDown   = Action(1)
	MigrationStatus = Action(2)
)

Variables

View Source
var (
	ErrVersionMissing     = errors.New("sqlize: version part is missing")
	ErrVersionTypeInvalid = errors.New("sqlize: version type must be int64 and must be a positive number")
)
View Source
var DefaultPrinter = PrinterFunc(func(format string, args ...interface{}) { fmt.Printf(format, args...) })

DefaultPrinter is a default logger for migrator.

View Source
var DefaultTemplating = &templating{}

DefaultTemplating is a default templating engine. This templating engine use token `---+split+---` to split migration actions. for example:

 create table if not exists migrator_test_table_1(name text);
	---+split+---
	drop table if exists migrator_test_table_1;
View Source
var DefaultVersioning = func() int64 { return time.Now().UnixNano() }

DefaultVersioning is a default version generator using time unix nano.

Functions

This section is empty.

Types

type Action

type Action uint

Action is a type for available actions.

type FileSource

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

func (*FileSource) AppendMigration

func (f *FileSource) AppendMigration(version int64, name string, content string) (string, error)

func (*FileSource) FetchMigrations

func (f *FileSource) FetchMigrations(reader Reader, action Action, ascending bool) ([]Migration, error)

type Migration

type Migration struct {
	Script     string
	Version    int64
	SourcePath string
}

Migration represents a migration unit.

type MigrationHistory

type MigrationHistory struct {
	Migration
	Applied     bool
	DateApplied time.Time
}

MigrationHistory represent a migration history unit.

type Migrator

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

Migrator knows how to manage database migration.

func NewMigrator

func NewMigrator(source Source, repository Repository, options ...Option) *Migrator

NewMigrator creates a new Migrator instance.

func (*Migrator) Down

func (m *Migrator) Down(ctx context.Context) error

Down knows how to reset migration into initial state.

func (*Migrator) Init

func (m *Migrator) Init(ctx context.Context) error

Init knows how to init migrations.

func (*Migrator) New

func (m *Migrator) New(name string) (string, error)

New creates a new migration source.

func (*Migrator) Status

func (m *Migrator) Status(ctx context.Context) error

Status prints the migration status.

func (*Migrator) Undo

func (m *Migrator) Undo(ctx context.Context) error

Undo knows how to undo one-step migration.

func (*Migrator) Up

func (m *Migrator) Up(ctx context.Context) error

Up knows how to apply all pending or created migrations.

type Option

type Option func(m *Migrator)

Option is an option type that can be used to customize the Migrator configs.

func WithPrinter

func WithPrinter(printer Printer) Option

WithPrinter knows how to set Migrator printer.

func WithTemplating

func WithTemplating(t TemplateReader) Option

WithTemplating know how to set Migrator templating engine.

func WithVersioning

func WithVersioning(fn VersionFunc) Option

WithVersioning know how to set Migrator version generator.

type Printer

type Printer interface {
	// Printf knows how to print formatted text.
	Printf(format string, args ...interface{})
}

Printer is a contract for migration logger.

type PrinterFunc

type PrinterFunc func(format string, args ...interface{})

PrinterFunc is adapter function that can be used to create a new Printer by using function signature.

func (PrinterFunc) Printf

func (p PrinterFunc) Printf(format string, args ...interface{})

type Reader

type Reader interface {
	// Read knows how to read and parse template based on given action.
	Read(reader io.Reader, action Action) (string, error)
}

Reader knows how to read and parse template.

type Repository

type Repository interface {
	// CreateMigrationTable knows how to create the migration table.
	CreateMigrationTable(ctx context.Context) error

	// FetchCurrentMigrations know how to fetch current migration histories.
	FetchCurrentMigrations(ctx context.Context) (map[int64]MigrationHistory, error)

	// ApplyNewMigration knows how to apply a new migration.
	ApplyNewMigration(ctx context.Context, version int64, script string, appliedAt time.Time) error

	// ApplyExistingMigration knows how to apply an existing migration.
	ApplyExistingMigration(ctx context.Context, version int64, script string, appliedAt time.Time) error

	// UndoExistingMigration knows how to undo existing migration.
	UndoExistingMigration(ctx context.Context, version int64, script string, appliedAt time.Time) error
}

Repository knows how to manage migration at persistence storage level.

func NewPostgreRepository

func NewPostgreRepository(db *sql.DB, table string) Repository

NewPostgreRepository creates a new Repository for PostgreSQL Database.

type Source

type Source interface {
	// AppendMigration inserts a new migration into source.
	AppendMigration(version int64, name string, content string) (string, error)
	// FetchMigrations knows how to get list of migrations from source.
	FetchMigrations(reader Reader, action Action, ascending bool) ([]Migration, error)
}

Source knows how to manage migration source. We can implement this contract to manage migration source from local file system (for example: FileSource)

func NewSourceFromDir

func NewSourceFromDir(dir string) Source

type Template

type Template interface {
	// Template returns a template format.
	Template() string
}

Template knows how to manage template.

type TemplateReader

type TemplateReader interface {
	Template
	Reader
}

TemplateReader knows how to manage and read template.

type VersionFunc

type VersionFunc func() int64

VersionFunc is a signature for version generator.

Jump to

Keyboard shortcuts

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