tarantool_migrator

package module
v0.0.0-...-b5d8a90 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 12 Imported by: 0

README

Tarantool Migrator

Go Test Codecov License

Description

Simple Tarantool migrator

Download

go get -u github.com/kachit/tarantool-migrator

Usage

Migrations as lua files
|-- migrations
    |-- 202410082345_test_migration_1.down.lua // 202410082345_test_migration_1 Down cmd
    |-- 202410082345_test_migration_1.up.lua // 202410082345_test_migration_1 Up cmd
    |-- 202410091201_test_migration_2.down.lua // 202410091201_test_migration_2 Down cmd
    |-- 202410091201_test_migration_2.up.lua // 202410091201_test_migration_2 Up cmd
    |-- --202410091545_test_migration_3.up.lua //excluded migration
Migrations as go files

NOTICE: When migrations loaded from filesystem they sorted by name automatically

package migrations

import (
	tarantool_migrator "github.com/kachit/tarantool-migrator"
	"github.com/tarantool/go-tarantool/v2/pool"
	"context"
)

var Migrations = tarantool_migrator.MigrationsCollection{
	&tarantool_migrator.Migration{
		ID: "202410082345_test_migration_1",
		Migrate: func(ctx context.Context, pooler pool.Pooler, options *tarantool_migrator.Options) error {
			//your migration Up code here
			return nil
		},
		Rollback: func(ctx context.Context, pooler pool.Pooler, options *tarantool_migrator.Options) error {
			//your migration Down code here
			return nil
		},
	},
}
Let's connect to tarantool

NOTICE: When migrations built as go slice they order can`t change

package main

import (
    "fmt"
    "time"
	"context"
	"github.com/tarantool/go-tarantool/v2"
	"github.com/tarantool/go-tarantool/v2/pool"
)

func main(){
	//Your tarantool config
	config := &TarantoolConfig{
		Host:     "127.0.0.1",
		Port:     3301,
		Login:    "login",
		Password: "password",
	}
	ctx := context.Background()
	dialer := tarantool.NetDialer{
		Address:  fmt.Sprintf("%s:%d", config.Host, config.Port),
		User:     config.Login,
		Password: config.Password,
	}
	opts := tarantool.Opts{
		Timeout:   time.Second,
		Reconnect: 2 * time.Second,
	}
	
	//connect to tarantool pool
	instance1 := pool.Instance{Name: "your-instance-address", Dialer: dialer, Opts: opts}
	tt, err :=  pool.Connect(ctx, []pool.Instance{
		instance1,
	})
	if err != nil {
		panic(err)
	}
}
Let's starting migrate
package main

import (
	"embed"
	"context"
	tarantool_migrator "github.com/kachit/tarantool-migrator"
)

//go:embed migrations
var LuaFs embed.FS

func main(){
	ctx := context.Background()
	//load migrations list from embed
	fsLoader := tarantool_migrator.NewEmbedFsLoader(LuaFs)
	migrations, err := fsLoader.LoadMigrations("migrations")
	if err != nil {
		panic(err)
	}

	//migrate
	migrator := tarantool_migrator.NewMigrator(tt, migrations)
	err = migrator.Migrate(ctx)
	if err != nil {
		panic(err)
	}
}
Let's rollback latest migration
package main

import (
	"embed"
	"context"
	tarantool_migrator "github.com/kachit/tarantool-migrator"
)

//go:embed migrations
var LuaFs embed.FS

func main(){
	ctx := context.Background()
	//load migrations list from embed
	fsLoader := tarantool_migrator.NewEmbedFsLoader(LuaFs)
	migrations, err := fsLoader.LoadMigrations("migrations")
	if err != nil {
		panic(err)
	}

	//migrate
	migrator := tarantool_migrator.NewMigrator(tt, migrations)
	err = migrator.RollbackLast(ctx)
	if err != nil {
		panic(err)
	}
}

Coverage

go test --coverprofile=coverage.out ./... ; go tool cover -func coverage.out ; go tool cover --html=coverage.out -o coverage.html

Documentation

Index

Constants

View Source
const MigrationFilePrefixExcluded = "--"
View Source
const MigrationFileSuffixDown = "down"
View Source
const MigrationFileSuffixUp = "up"

Variables

View Source
var DefaultLogger = NewLogger(log.New(os.Stdout, "", log.LstdFlags), LoggerConfig{
	LogLevel: LogLevelInfo,
	Prefix:   "Tarantool-Migrator:",
})
View Source
var DefaultOptions = &Options{
	MigrationsSpace: "migrations",
	ReadMode:        pool.ANY,
	WriteMode:       pool.RW,
}
View Source
var ErrMigrationIDDoesNotExist = errors.New("tried to migrate to an ID that doesn't exist")
View Source
var ErrMissingID = errors.New("missing ID in migration")

ErrMissingID is returned when the ID od migration is equal to ""

View Source
var ErrMissingMigrateFunc = errors.New("missing migrate function in migration")
View Source
var ErrMissingRollbackFunc = errors.New("missing rollback function in migration")
View Source
var ErrNoAppliedMigrations = errors.New("no applied migrations")
View Source
var ErrNoDefinedMigrations = errors.New("no defined migrations")

ErrNoDefinedMigrations is returned when no migrations are defined.

View Source
var ErrWrongMigrationCmdFormat = errors.New("wrong migration cmd format")
View Source
var ErrWrongMigrationFileFormat = errors.New("wrong migration file format")
View Source
var LuaFs embed.FS

Functions

func FormatDurationToMs

func FormatDurationToMs(d time.Duration) float64

func NewGenericMigrateFunction

func NewGenericMigrateFunction(req string) func(context.Context, pool.Pooler, *Options) error

func WithLogger

func WithLogger(lg Logger) func(migrator *Migrator)

func WithOptions

func WithOptions(op *Options) func(migrator *Migrator)

Types

type EmbedFsLoader

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

func NewEmbedFsLoader

func NewEmbedFsLoader(fs embed.FS) *EmbedFsLoader

func (*EmbedFsLoader) LoadMigrations

func (fl *EmbedFsLoader) LoadMigrations(path string) (MigrationsCollection, error)

type Executor

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

type LogLevel

type LogLevel int
const LogLevelDebug LogLevel = iota
const LogLevelInfo LogLevel = iota
const LogLevelSilent LogLevel = iota

type LogWriter

type LogWriter interface {
	Printf(string, ...interface{})
}

LogWriter log writer interface

type Logger

type Logger interface {
	Info(ctx context.Context, msg string, args ...interface{})
	Debug(ctx context.Context, msg string, args ...interface{})
	SetLogLevel(level LogLevel) Logger
}

func NewLogger

func NewLogger(writer LogWriter, config LoggerConfig) Logger

type LoggerConfig

type LoggerConfig struct {
	LogLevel LogLevel
	Prefix   string
}

type MigrateFunc

type MigrateFunc func(context.Context, pool.Pooler, *Options) error

MigrateFunc is the func signature for migrating.

type Migration

type Migration struct {
	// ID is the migration identifier. Usually a timestamp like "201601021504".
	ID string
	// Migrate is a function that will br executed while running this migration.
	Migrate MigrateFunc
	// Rollback will be executed on rollback. Can be nil.
	Rollback RollbackFunc
}

Migration represents a database migration (a modification to be made on the database).

type MigrationFile

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

func NewMigrationFile

func NewMigrationFile(path string, file fs.DirEntry) (*MigrationFile, error)

func (*MigrationFile) GetCmd

func (mf *MigrationFile) GetCmd() string

func (*MigrationFile) GetName

func (mf *MigrationFile) GetName() string

func (*MigrationFile) GetPath

func (mf *MigrationFile) GetPath() string

type MigrationsCollection

type MigrationsCollection []*Migration

func (*MigrationsCollection) Find

func (m *MigrationsCollection) Find(migrationID string) (*Migration, error)

func (*MigrationsCollection) IsEmpty

func (m *MigrationsCollection) IsEmpty() bool

type Migrator

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

func NewMigrator

func NewMigrator(tt pool.Pooler, migrations MigrationsCollection, options ...func(*Migrator)) *Migrator

func (*Migrator) Migrate

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

func (*Migrator) RollbackLast

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

type Options

type Options struct {
	// MigrationsSpace is the migrations space.
	MigrationsSpace string
	Instances       []string

	TransactionEnabled bool
	ReadMode           pool.Mode
	WriteMode          pool.Mode
}

Options define options for all migrations.

type RollbackFunc

type RollbackFunc func(context.Context, pool.Pooler, *Options) error

RollbackFunc is the func signature for rollback.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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