migration

package
v0.0.0-...-970f38b Latest Latest
Warning

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

Go to latest
Published: May 24, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TableName const
	TableName = "dbmigrator_migration"
	// StatusNotApplied const
	StatusNotApplied = 0
	// StatusApplied const
	StatusApplied = 1
	// StatusError const
	StatusError = 2
)
View Source
const DefaultDownQuantity = 1

DefaultDownQuantity const

View Source
const MigrationTypeGo = "go"

MigrationTypeGo - MigrationType gor go

View Source
const MigrationTypeSQL = "sql"

MigrationTypeSQL - MigrationType gor SQL

Variables

View Source
var MigrationTypes = []interface{}{MigrationTypeSQL, MigrationTypeGo}

MigrationTypes is slice of migration types

View Source
var SQLCreateTable string = `CREATE TABLE IF NOT EXISTS public."` + TableName + `" (
	id int4 NOT NULL,
	status int4 NOT NULL DEFAULT 0,
	name varchar(100) NOT NULL,
	"time" timestamptz NOT NULL DEFAULT Now(),
	CONSTRAINT migration_pkey PRIMARY KEY (id)
);`

SQLCreateTable is the SQL text for creation table

Functions

func GroupLogsByStatus

func GroupLogsByStatus(list []Log) (l map[uint]LogsList)

GroupLogsByStatus groups logs applied/not applied

Types

type CreateParams

type CreateParams struct {
	ID   uint
	Type string
	Name string
}

CreateParams is struct for params for creation of migration

func (CreateParams) Validate

func (p CreateParams) Validate() error

Validate method

type Func

type Func func(tx *sqlx.Tx) error

Func is func for migrations Up/Down

type IRepository

type IRepository interface {
	// SetLogger is setter for logger
	SetLogger(logger app.Logger)
	// Get returns an entity with the specified ID.
	//Get(ctx context.Context, id uint) (*Log, error)
	// Count returns the number of entities.
	//Count(ctx context.Context) (uint, error)
	// Query returns the list of entities with the given offset and limit.
	Query(ctx context.Context, offset, limit uint) ([]Log, error)
	// QueryTx returns the list of entities with the given offset and limit.
	QueryTx(ctx context.Context, t Transaction, query *QueryCondition, offset, limit uint) ([]Log, error)
	// Last retrieves a last record with the specified query condition and limit 1 from the database.
	Last(ctx context.Context, query *QueryCondition) (*Log, error)
	// LastTx retrieves a last record with the specified query condition and limit 1 from the database.
	LastTx(ctx context.Context, t Transaction, query *QueryCondition) (*Log, error)
	// Create saves a new entity in the storage.
	//Create(ctx context.Context, entity *Log) error
	// Update updates an entity with given ID in the storage.
	//Update(ctx context.Context, entity *Log) error
	// Delete removes an entity with given ID from the storage.
	//Delete(ctx context.Context, id uint) error
	// ExecSQL executes an user's plain sql
	ExecSQL(ctx context.Context, sql string) error
	// ExecSQLTx executes an user's plain sql
	ExecSQLTx(ctx context.Context, t Transaction, sql string) error
	// ExecFunc executes an user's func
	ExecFunc(ctx context.Context, f Func) (err error)
	// ExecFuncTx executes an user's func
	ExecFuncTx(ctx context.Context, t Transaction, f Func) (err error)
	// BeginTx begins a transaction
	BeginTx(ctx context.Context) (Transaction, error)
	// BatchCreateTx creates a batch of MigrationsLog with transaction
	BatchCreateTx(ctx context.Context, t Transaction, list LogsList) error
	// BatchUpdateTx updates a batch of MigrationsLog with transaction
	BatchUpdateTx(ctx context.Context, t Transaction, list LogsList) error
}

IRepository encapsulates the logic to access albums from the data source.

type IService

type IService interface {
	// NewEntity returns new empty entity
	NewEntity() *Log
	// Get returns an entity with given ID
	//Get(ctx context.Context, id uint) (*Log, error)
	//First(ctx context.Context, entity *Event) (*Event, error)
	// Query returns a list with pagination
	Query(ctx context.Context, offset, limit uint) ([]Log, error)
	// List entity
	List(ctx context.Context) ([]Log, error)
	//Count(ctx context.Context) (uint, error)
	// Create entity
	//Create(ctx context.Context, entity *Log) error
	// Update entity
	//Update(ctx context.Context, entity *Log) error
	// Delete entity
	//Delete(ctx context.Context, id uint) error
	// CreateTable creates table for migration
	CreateTable(ctx context.Context) error
	// Up a list of migrations
	Up(ctx context.Context, ms MigrationsList, quantity int) error
	// Down a list of migrations
	Down(ctx context.Context, ms MigrationsList, quantity int) error
	// Redo a last migration
	Redo(ctx context.Context, ms MigrationsList) error
	// Last returns a last Log
	Last(ctx context.Context) (*Log, error)
	// Create creates a file for migration
	Create(ctx context.Context, wr io.Writer, p CreateParams) (err error)
	// Create creates a main file for migrations execution
	CreateMainFile(ctx context.Context, wr io.Writer) (err error)
}

IService encapsulates usecase logic for event.

type Log

type Log struct {
	ID     uint
	Status uint
	Name   string
	Time   time.Time
}

Log struct

type LogsList

type LogsList map[uint]Log

LogsList is a map of Log entities

func MigrationsLogsFilterExceptByKeys

func MigrationsLogsFilterExceptByKeys(sourceList LogsList, exceptList LogsList) (l LogsList)

MigrationsLogsFilterExceptByKeys returns LogsList with all entities from sourceList other than those represented in exceptList

func MigrationsLogsFilterExistsByKeys

func MigrationsLogsFilterExistsByKeys(sourceList LogsList, existList LogsList) (l LogsList)

MigrationsLogsFilterExistsByKeys returns LogsList with all entities from sourceList that represented in existList

func (LogsList) Copy

func (l LogsList) Copy() (mls LogsList)

Copy one LogsList to another

func (LogsList) IDs

func (l LogsList) IDs() (ids []int)

IDs returns slice of id

func (LogsList) Slice

func (l LogsList) Slice() (mls []Log)

Slice converts LogsList to slice

type LogsSlice

type LogsSlice []Log

LogsSlice is a slice of Log entities

func (LogsSlice) Len

func (s LogsSlice) Len() int

Len returns length

func (LogsSlice) Less

func (s LogsSlice) Less(i, j int) bool

Less returns true if i elements less than j elements, otherwise - false

func (LogsSlice) Swap

func (s LogsSlice) Swap(i, j int)

Swap swaps elements

type Migration

type Migration struct {
	ID   uint
	Name string
	Up   interface{}
	Down interface{}
}

Migration struct Up and Down is a Func or a string (plain SQL text)

func (Migration) Log

func (m Migration) Log(status uint) *Log

Log returns corresponding Log

func (Migration) Validate

func (m Migration) Validate() error

Validate method

type MigrationsList

type MigrationsList map[uint]Migration

MigrationsList ia a map of Migration

func MigrationsListFilterExceptByKeys

func MigrationsListFilterExceptByKeys(sourceList MigrationsList, exceptList LogsList) (l MigrationsList)

MigrationsListFilterExceptByKeys returns MigrationsList with all entities from sourceList other than those represented in exceptList

func MigrationsListFilterExistsByKeys

func MigrationsListFilterExistsByKeys(sourceList MigrationsList, existList LogsList) (l MigrationsList)

MigrationsListFilterExistsByKeys returns MigrationsList with all entities from sourceList that represented in existList

func (MigrationsList) IDs

func (l MigrationsList) IDs() (ids []int)

IDs returns slice of IDs

type QueryCondition

type QueryCondition struct {
	Where *WhereCondition
}

QueryCondition struct for defining a query condition

type Service

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

Service stgruct

func NewService

func NewService(repo IRepository, logger app.Logger) *Service

NewService creates a new Service.

func (Service) Create

func (s Service) Create(ctx context.Context, wr io.Writer, p CreateParams) (err error)

Create creates a file for migration

func (Service) CreateMainFile

func (s Service) CreateMainFile(ctx context.Context, wr io.Writer) (err error)

CreateMainFile here is dummy. Redefined in ServiceTool.

func (Service) CreateTable

func (s Service) CreateTable(ctx context.Context) error

CreateTable creates table for migration

func (Service) Down

func (s Service) Down(ctx context.Context, ms MigrationsList, quantity int) error

Down a list of migrations

func (Service) Last

func (s Service) Last(ctx context.Context) (*Log, error)

Last returns a last Log

func (Service) List

func (s Service) List(ctx context.Context) ([]Log, error)

List returns the items list.

func (Service) NewEntity

func (s Service) NewEntity() *Log

NewEntity returns a new empty entity

func (Service) Query

func (s Service) Query(ctx context.Context, offset, limit uint) ([]Log, error)

Query returns the items with the specified offset and limit.

func (Service) Redo

func (s Service) Redo(ctx context.Context, ms MigrationsList) error

Redo a last migration

func (Service) Up

func (s Service) Up(ctx context.Context, ms MigrationsList, quantity int) error

Up a list of migrations

type ServiceTool

type ServiceTool struct {
	*Service
}

ServiceTool is the service for DBMigrator as a tool

func NewServiceTool

func NewServiceTool(is IService) (*ServiceTool, error)

NewServiceTool creates a new ServiceTool.

func (ServiceTool) Create

func (s ServiceTool) Create(ctx context.Context, wr io.Writer, p CreateParams) (err error)

Create creates a file for migration

func (ServiceTool) CreateMainFile

func (s ServiceTool) CreateMainFile(ctx context.Context, wr io.Writer) (err error)

CreateMainFile creates a main file for migrations execution

type Transaction

type Transaction interface {
	// Commit a transaction
	Commit() error
	// Rollback a transaction
	Rollback() error
}

Transaction for operations in domain level

type WhereCondition

type WhereCondition struct {
	Status uint
}

WhereCondition struct

Jump to

Keyboard shortcuts

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