pqmigrate

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2022 License: MIT Imports: 19 Imported by: 0

README

pqmigrate

FOSSA Status

Migration utility for postgres databases

License

FOSSA Status

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
	FS              *embed.FS // Support for embedded migrations in application
	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 PQMigrate added in v1.0.0

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

PQMigrate utility for managing common postgresql operations

func New

func New(config Config) *PQMigrate

New creates a new PQMigrate instance.

func (*PQMigrate) CreateDB added in v1.0.0

func (ctx *PQMigrate) 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 (*PQMigrate) CreateMigration added in v1.0.0

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

CreateMigration creates a new migration file with specified name

func (*PQMigrate) DropDB added in v1.0.0

func (ctx *PQMigrate) 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 (*PQMigrate) DumpDBFull added in v1.0.0

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

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

func (*PQMigrate) DumpDBFullWithPath added in v1.4.0

func (ctx *PQMigrate) DumpDBFullWithPath(fname *string) (string, error)

func (*PQMigrate) DumpDBSchema added in v1.0.0

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

DumpDBSchema dumps the database schema without owner information.

func (*PQMigrate) DumpDBSchemaToFile added in v1.0.0

func (ctx *PQMigrate) 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 (*PQMigrate) DumpDBSchemaToFileWithName added in v1.0.0

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

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

func (*PQMigrate) Finish added in v1.0.0

func (ctx *PQMigrate) Finish() error

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

func (*PQMigrate) LoadDBSchema added in v1.0.0

func (ctx *PQMigrate) 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 (*PQMigrate) LoadFullDump added in v1.0.0

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

LoadFullDump loads database schema and data from specified file into the given database.

func (*PQMigrate) MigrateDown added in v1.0.0

func (ctx *PQMigrate) 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 (*PQMigrate) MigrateDownFile added in v1.2.0

func (ctx *PQMigrate) MigrateDownFile(fileName string) error

MigrateDownFile applies specified `down` migration and deletes both `up` and `down` from migrations table.

func (*PQMigrate) MigrateFromFile added in v1.0.0

func (ctx *PQMigrate) 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 (*PQMigrate) MigrateUp added in v1.0.0

func (ctx *PQMigrate) 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 (*PQMigrate) MigrateUpFile added in v1.2.0

func (ctx *PQMigrate) MigrateUpFile(fileName string) error

MigrateUpFile applies specified `up` migration and inserts both `up` and `down` into the migrations table.

func (*PQMigrate) Replace added in v1.1.0

func (ctx *PQMigrate) Replace(fileName string, cb ConfirmCB) error

Replace tries to replace a specified migration in the database without running the migration itself. Useful for if a broken `down` migration has found its way into the db and needs to be replaced.

func (*PQMigrate) Squash added in v1.3.0

func (ctx *PQMigrate) Squash(cb ConfirmCB) error

Squash takes all migrations present on fs and squashes them into one file. Usable to reduce clutter.

func (*PQMigrate) Sync added in v1.0.0

func (ctx *PQMigrate) Sync(cb ConfirmCB) error

Sync tries to synchronize db and fs state by first checking if any migrations have changed on fs, then finding migrations that exist in db but not in fs. The last step is to apply all migrations that only only exist in fs.

func (*PQMigrate) UnSquash added in v1.3.0

func (ctx *PQMigrate) UnSquash(cb ConfirmCB) error

UnSquash reverses the operations performed of `Squash`. Restores squashed files to their original state.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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