gomiger

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: MIT Imports: 6 Imported by: 0

README

gomiger

A Golang framework for data migration.

instruction

install

go get github.com/ParteeLabs/gomiger

Add the gomiger.rc.yaml file to the root of your project.

path: './migrations' # Path to the migrations folder
pkg_name: 'mgr' # Package name
uri: '' # Database URI
schema_store: '' # Database schema store

usage

Initialize the source code.

go get github.com/ParteeLabs/gomiger/cmd/init

Add a migration.

go run your-migration-root-path add migration_name

Run migrations up.

go run your-migration-root-path up

Run migrations down.

go run your-migration-root-path down version

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseMigrator

type BaseMigrator struct {
	DB         DbPlugin
	Migrations []Migration
}

BaseMigrator is the base migrator for control flow.

func (*BaseMigrator) Down

func (b *BaseMigrator) Down(ctx context.Context, atVersion string) error

Down reverts the database to a specific version.

func (*BaseMigrator) Up

func (b *BaseMigrator) Up(ctx context.Context, toVersion string) error

Up updates the database to a specific version.

type DbPlugin

type DbPlugin interface {
	Connect(ctx context.Context) error
	GetSchema(ctx context.Context, version string) (Schema, error)
	ApplyMigration(ctx context.Context, mi Migration) error
	RevertMigration(ctx context.Context, mi Migration) error
}

DbPlugin is the interface for the plugin

type Gomiger

type Gomiger interface {
	Up(ctx context.Context, toVersion string) error
	Down(ctx context.Context, atVersion string) error
	Connect(ctx context.Context) error
}

Gomiger is the interface for the migrator

type GomigerConfig

type GomigerConfig struct {
	// Path to the migration root folder.
	Path string `yaml:"path"`
	// The package name of the migrator & migrations.
	// Default by the folder name of the path.
	PkgName string `yaml:"pkg_name"`
	// Database connection string.
	URI string `yaml:"uri"`
	// The path to the table / collection schema store.
	SchemaStore string `yaml:"schema_store"`
}

GomigerConfig is the global migration module configuration.

func GetGomigerRC

func GetGomigerRC(rcPath string) (*GomigerConfig, error)

GetGomigerRC returns the global migration module configuration

func (*GomigerConfig) ParseYAML

func (rc *GomigerConfig) ParseYAML(path string) error

ParseYAML parse the RC file in YAML format.

func (*GomigerConfig) PopulateAndValidate

func (rc *GomigerConfig) PopulateAndValidate() error

PopulateAndValidate populate data and validate it.

type Migration

type Migration struct {
	Version string
	Up      MutationFunc
	Down    MutationFunc
}

Migration contain a version name and a mutation function.

type MutationFunc

type MutationFunc = func(context context.Context) error

MutationFunc is a function that applies a migration.

type Schema

type Schema struct {
	Version   string       `json:"version" bson:"version" validate:"required"`
	Timestamp time.Time    `json:"timestamp" bson:"timestamp" validate:"required"`
	Status    SchemaStatus `json:"status" bson:"status" validate:"required"`
}

Schema is a log for migrations

type SchemaStatus

type SchemaStatus string

SchemaStatus is the status of the schema

var (
	// InProgress is for a running migration
	InProgress SchemaStatus = "in_progress"
	// Dirty is for a failed migration
	Dirty SchemaStatus = "dirty"
	// Applied is for a completed migration
	Applied SchemaStatus = "applied"
)

Directories

Path Synopsis
core module
mg command
mongomiger module

Jump to

Keyboard shortcuts

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