mtnt

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2019 License: MIT Imports: 7 Imported by: 0

README

go-tenant-migration CircleCI codecov Go Report Card license GoDoc

go-tenant-migration, or mtnt for short, is designed to run migrations on tenants in parallel.

This lihbrary is an extention of the go-migration.

TL;DR

Account and AccountProducer interfaces should be implemented. Where Account should provide the account database reference and AccountProducer should implement listing the Account instaces that should be migrated.

The rest should look like this:

package main

import (
	"os"
	
	mtnt "github.com/lab259/go-migration-tenant"
	mtntMongo "github.com/lab259/go-migration-tenant/mongo"
	migration "github.com/lab259/go-migration"
	
	"yoursystem"
	_ "yoursystem/migrations"
)

func main() {
	executor := mtnt.NewMigrationExecutor(
		migration.NewDefaultReporter(), 
		mtnt.NewDefaultReporter(os.Stdout),
		mtntMongo.Connector,
		yoursystem.NewAccountProducer(), // YOU MUST implement the account producer
		migration.DefaultCodeSource(),
	)
	executor.Run(10, os.Args...) // <- number of workers that will execute migrations, in other
	                            // words how many accounts you want to migrate in parallel.
}

Usage

Listing accounts

In order to run the system, you need to implement 2 interfaces: Account and AccountProducer.

Account (interface)

Account represents each tenant of your system. It should hold the database reference and provides the execution context for the migration. The execution context will be passed to each handler as it is called.

Hence, it is up to you to produce an execution context that will provide the database connection for the handlers. The default implementation expects you to provide the database connection reference as execution context. But, it can be easily changed by implementing a new Connector.

Identification() string
ProvideMigrationContext(func(executionContext interface{}) error) error

AccountProducer (interface)

AccountProducer should list all the Accounts that need to be migrated.

Get() ([]Account, error)
HasNext() bool
Total() int
Running a migration

Once implemented Account and AccountProducer, the library is able to run all migrations.

package main

import (
	"os"
	
	mtnt "github.com/lab259/go-migration-tenant"
	mtntMongo "github.com/lab259/go-migration-tenant/mongo"
	migration "github.com/lab259/go-migration"
	
	"yoursystem"
	_ "yoursystem/migrations"
)

func main() {
	executor := mtnt.NewMigrationExecutor(
		migration.NewDefaultReporter(), 
		mtnt.NewDefaultReporter(os.Stdout),
		mtntMongo.Connector,
		yoursystem.NewAccountProducer(), // YOU MUST implement the account producer
		migration.DefaultCodeSource(),
	)
	executor.Run(10, os.Args...) // <- number of workers that will execute migrations, in other
	                // words how many accounts you want to migrate in parallel.
}

Now, after compiling the system:

./bin/migration migrate

To know more about the migration commands you can:

./bin/migration --help
What if I need to filter one specific account

Well, the AccountProducer implementation is up to you. So, you should filter accounts while implementing the producer.

Concurrency

The library uses gp-prdcsm to implement a Producer & Consumer pattern to achieve concurrent migrations. Migrations at the account level are not ran in parallel. But, two, or more, accounts should run in parallel.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDefaultReporter

func NewDefaultReporter(writer io.Writer) *defaultReporter

Types

type Account

type Account interface {
	Identification() string
	ProvideMigrationContext(func(executionContext interface{}) error) error
}

Account represents

type AccountProducer

type AccountProducer interface {
	Get() ([]Account, error)
	HasNext() bool
	Total() int
}

AccountProducer should list all the accounts that should be migrated.

type Connector

type Connector func(executionContext interface{}) (migration.Target, error)

Connector should create a new `migration.Target` from the provided execution context.

All default implementation expect the `executionContext` itself to be the database connection reference. So, if you need something more complex, you should create your own.

See also `MongoConnector`.

type MigrationExecutor

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

MigrationExecutor runs all migrations in all accounts listed by the `AccountProducer`.

func NewMigrationExecutor

func NewMigrationExecutor(migrationReporter migration.Reporter, mtntnReporter Reporter, connector Connector, producer AccountProducer, source migration.Source) *MigrationExecutor

NewMigrationExecutor returns a new instance of a `MigrationExecutor`.

func (*MigrationExecutor) Run

func (e *MigrationExecutor) Run(workers int, args ...string)

Run initializes the `go-prdcsm` starting the process.

func (*MigrationExecutor) Stop

func (e *MigrationExecutor) Stop()

Stop stops the running pool of workers.

type Reporter

type Reporter interface {
	BeforeAccount(account Account)
	AfterAccount(account Account)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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