pqmigrate

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2019 License: MIT Imports: 17 Imported by: 0

README

pg-migrate

Migration utility for postgres databases

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	AllInOneTx      bool   // Perform all database operations in the same transaction
	BaseDirectory   string // Directory where the sql files are stored
	DBUrl           string // Postgresql url `psql://<user>:<pwd>@<host>:<port>/<db_name>`
	Logger          Logger // If set the logger will be used instead of standard out
	MigrationsTable string // Name of migrations table in database
	Debug           bool   // Show debug info
	DryRun          bool   // Perform all database operations but don't commit
}

Config options for the library

type ConfirmCB

type ConfirmCB func(prompt string) bool

ConfirmCB simple confirm function for potentially dangerous operations. if return value is true the operation will be performed, else abort.

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
	Warn(args ...interface{})
	Print(args ...interface{})
	Error(args ...interface{})
	Inf(args ...interface{})
	DBG(args ...interface{})
	Ok(args ...interface{})
}

Logger interface for using other loggers than stdout.

type PGMigrate

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

PGMigrate utility for managing common postgresql operations

func New

func New(config Config) *PGMigrate

New creates a new PGMigrate instance.

func (*PGMigrate) CreateDB

func (ctx *PGMigrate) CreateDB(cb ConfirmCB) error

CreateDB ensures that the database specified in the postgres url exists. If not it creates it. This probably won't work if you don't have full access to the postgres server.

func (*PGMigrate) CreateMigration

func (ctx *PGMigrate) CreateMigration(name string) error

CreateMigration creates a new migration file with specified name

func (*PGMigrate) DropDB

func (ctx *PGMigrate) DropDB(cb ConfirmCB) error

DropDB drops the database specified in the postgres url. This probably won't work if you don't have full access to the postgres server.

func (*PGMigrate) DumpDBFull

func (ctx *PGMigrate) DumpDBFull(fname *string) error

DumpDBFull dumps database schema and content to a file named `dump_<timestamp-unix>.sql`

func (*PGMigrate) DumpDBSchema

func (ctx *PGMigrate) DumpDBSchema() ([]byte, error)

DumpDBSchema dumps the database schema without owner information.

func (*PGMigrate) DumpDBSchemaToFile

func (ctx *PGMigrate) DumpDBSchemaToFile(fname *string) error

DumpDBSchemaToFile dumps database schema and performed database migrations to files named `schema_<timestamp-unix>.sql` and `migrations_<timestamp-unix>.sql`.

func (*PGMigrate) DumpDBSchemaToFileWithName

func (ctx *PGMigrate) DumpDBSchemaToFileWithName(schemaName, migrationsName string) error

DumpDBSchemaToFileWithName calls `DumpDBSchema` and writes output to specified file.

func (*PGMigrate) Finish

func (ctx *PGMigrate) Finish() error

Finish commits lingering database transaction (if all in one transaction specified) and closes database handle.

func (*PGMigrate) LoadDBSchema

func (ctx *PGMigrate) LoadDBSchema(schemaName string, cb ConfirmCB) error

LoadDBSchema loads specified schema and inserts migrations from matching migrations file if found next to the schema sql.

func (*PGMigrate) LoadFullDump

func (ctx *PGMigrate) LoadFullDump(dumpName string) error

func (*PGMigrate) MigrateDown

func (ctx *PGMigrate) MigrateDown(steps int) error

MigrateDown applies `down` migrations from migration dir in order. `steps` are number of migrations to perform. If steps == -1 all `down` migrations will be applied.

func (*PGMigrate) MigrateFromFile

func (ctx *PGMigrate) MigrateFromFile(fileName string) error

MigrateFromFile loads the specified file and does a direct migration without modifying the migrations table. Useful for database schema and database seeds.

func (*PGMigrate) MigrateUp

func (ctx *PGMigrate) MigrateUp(steps int) error

MigrateUp applies `up` migrations from migration dir in order. `steps` are number of migrations to perform. If steps == -1 all `up` migrations will be applied.

func (*PGMigrate) Sync

func (ctx *PGMigrate) Sync() error

sync method in one transaction fetch migrations in db fetch migrations in fs find migrations that exist in db but not in fs if found:

show info to user
confirm that migrations will be rolled back
roll back db migrations from end

apply all newer migrations from fs in order

Directories

Path Synopsis
cmd
pqmigrate command

Jump to

Keyboard shortcuts

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