migrant

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

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

Go to latest
Published: Jul 5, 2018 License: MIT Imports: 10 Imported by: 0

README

pgx_migrant

Golang library for migrations and sync interfaces versions of PostgreSQL databases. pgx driver only.

Status

It works, but API is unstable. And docs needed.

Usage Example


package db_sync_helper

import (
	"fmt"

	"github.com/i-erokhin/pgx_migrant"
	"github.com/jackc/pgx"

	"giterica.io/project/repo/config"
	"giterica.io/project/repo/constants"
)

func DBSyncHelper(c *config.Conf, db *pgx.ConnPool) error {
	tx, err := db.Begin()
	if err != nil {
		return err
	}
	defer tx.Rollback()

	if err = migrate(c, tx); err != nil {
		return err
	}

	return tx.Commit()
}

func migrate(c *config.Conf, tx *pgx.Tx) error {
	sm := migrant.SyncMap{
		Migration: migrant.MigrationMap{
			Path:   c.SQlArtifacts.MigrationsPath,
			Schema: constants.DBSchema,
		},
		Interfaces: migrant.InterfacesMap{
			Path:   c.SQlArtifacts.InterfacesPath,
			Schema: constants.DBInterfacesSchema,
		},
		TemplateData: c,
	}

	result, err := migrant.SyncDB(tx, sm)
	if err != nil {
		return err
	}
	if len(result.Migrations.Files) != 0 {
		fmt.Printf("DB state: %d, migrations applied:\n", result.Migrations.Number)
		for _, name := range result.Migrations.Files {
			fmt.Printf("  %s\n", name)
		}
	}
	if len(result.Interfaces.Files) != 0 {
		fmt.Printf("Actual DB interfaces schema: %s, SQL files applied:\n", constants.DBInterfacesSchema)
		for _, name := range result.Interfaces.Files {
			fmt.Printf("  %s\n", name)
		}
	}
	return nil
}

Output:

DB state: 2, migrations applied:
  01-tables.sql
  02-seeds.sql
Actual DB interfaces schema: tabel_interfaces_0, SQL files applied:
  01-active_employee.sql

Documentation

Index

Constants

View Source
const TableName = "_migration"

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	Tx         *pgx.Tx
	Schema     string
	LastNumber int
	// contains filtered or unexported fields
}

func NewDB

func NewDB(tx *pgx.Tx, schema string) (db *DB, err error)

func (*DB) Migrate

func (d *DB) Migrate(files []*File, templateData interface{}) (names []string, err error)

type DBInterfaces

type DBInterfaces struct {
	Tx     *pgx.Tx
	Schema string
	Found  bool
}

func NewDbInterfaces

func NewDbInterfaces(tx *pgx.Tx, schema string) (result *DBInterfaces, err error)

func (*DBInterfaces) Sync

func (d *DBInterfaces) Sync(files []*File, templateData interface{}) (names []string, err error)

type File

type File struct {
	Path     string
	Basename string
	Number   int
}

func NewFile

func NewFile(pa string) (file *File, err error)

path берется из файловой системы на этапе создания файл не читается с диска

func (*File) GetSQL

func (f *File) GetSQL(data interface{}) (sql string, err error)

type Filesystem

type Filesystem struct {
	Path      string
	Files     []*File
	MaxNumber int
}

func NewFilesystem

func NewFilesystem(pa string) (fs *Filesystem, err error)

func (*Filesystem) PrettyJSON

func (f *Filesystem) PrettyJSON() string

type InitDBResult

type InitDBResult struct {
	Migrations MigrationResult
	Interfaces InterfacesResult
}

func SyncDB

func SyncDB(tx *pgx.Tx, sm SyncMap) (result InitDBResult, err error)

type InterfacesMap

type InterfacesMap MigrationMap

type InterfacesResult

type InterfacesResult struct {
	Files []string
}

func CreateInterfaces

func CreateInterfaces(tx *pgx.Tx, im InterfacesMap, templateData interface{}) (result InterfacesResult, err error)

type MigrationError

type MigrationError struct {
	File *File
	Err  pgx.PgError
}

func NewError

func NewError(f *File, err pgx.PgError) *MigrationError

func (*MigrationError) Error

func (m *MigrationError) Error() string

type MigrationMap

type MigrationMap struct {
	Schema string
	Path   string
}

type MigrationResult

type MigrationResult struct {
	Files  []string
	Number int
}

func Migrate

func Migrate(tx *pgx.Tx, mm MigrationMap, templateData interface{}) (result MigrationResult, err error)

type SyncMap

type SyncMap struct {
	Migration    MigrationMap
	Interfaces   InterfacesMap
	TemplateData interface{}
}

Jump to

Keyboard shortcuts

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