modulepackage
2.1.1
Repository: https://github.com/getkalido/hb_migrations.git
Documentation: pkg.go.dev
# README
hb migrations - A Better migration engine for go-pg/pg
Basic Commands
- init
- runs the specified intial migration as a batch on it's own.
- migrate
- runs all available migrations that have not been run inside a batch
- rollback
- reverts the last batch of migrations.
- create name
- creates a migration file using the name provided.
Usage
Make a main.go
in a migrations
folder
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/go-pg/pg/v10"
migrations "github.com/hbarnardt/hb_migrations"
)
const usageText = "Lorem Ipsum"
func main() {
flag.Usage = usage
flag.Parse()
dbCnf := ...
db := pg.Connect(&pg.Options{
Addr: dbCnf.GetHost(),
User: dbCnf.GetUsername(),
Database: dbCnf.GetDatabase(),
Password: dbCnf.GetPassword(),
})
migrations.SetMigrationTableName("public.migrations_home")
migrations.SetInitialMigration("000000000000_init")
migrations.SetMigrationNameConvention(migrations.SnakeCase)
err := migrations.Run(db, flag.Args()...)
if err != nil {
log.Print(err)
os.Exit(1)
}
}
func usage() {
fmt.Printf(usageText)
flag.PrintDefaults()
os.Exit(2)
}
Compile it:
$> go build -i -o ./migrations/migrations ./migrations/*.go
Run it:
$> ./migrations/migrations migrate
Notes on generated file names
$> ./migrations/migrations create new_index
Creates a file in the ./migrations
folder called 20181031230738_new_index.go
with the following contents:
package main
import (
"github.com/go-pg/pg/v10"
migrations "github.com/hbarnardt/hb_migrations"
)
func init() {
migrations.Register(
"20181031230738_new_index",
up20181031230738NewIndex,
down20181031230738NewIndex,
)
}
func up20181031230738NewIndex(tx *pg.Tx) error {
_, err := tx.Exec(``)
return err
}
func down20181031230738NewIndex(tx *pg.Tx) error {
_, err := tx.Exec(``)
return err
}
Forward migration sql commands go in up and Rollback migrations sql commands go in down
# Functions
ConvertCamelCaseToSnakeCase converts a potentially camel-case string to snake-case.
ConvertSnakeCaseToCamelCase converts a potentially snake-case string to camel-case.
DefaultMigrator returns a migrator with the default options.
GetCaser returns the appropriate caser for the given naming convention.
NewMigrator creates a Migrator with the specified options.
WithCapacity initialises a Migrator with enough capacity for a given number of migrations.
WithContext initialises a Migrator with a context object.
WithoutExplicitLock initialises a Migrator which will try to explicitly lock the migrations table for each transaction.
WithInitialName sets the name of the initial migration which will be run by a Migrator when running the init command.
WithLogger initialises a Migrator with a logger to use when logging output.
WithMigrationDir initialises a Migrator with a given migration directory.
WithMigrations loads migrations from an existing registry.
WithMigrationTableName sets the name of the table which will store completed migrations for a Migrator.
WithNameConvention sets the name naming convention which will be used by a Migrator when generating new migrations.
WithoutExplicitLock initialises a Migrator which will not try to explicitly lock the migrations table for each transaction.
WithPostgresFlavour initialises a Migrator with a given Postgres flavour.
WithQuiet initialises a Migrator with quiet level (default: 0).
WithTemplateDir initialises a Migrator with a given template directory.
WithVerbosity initialises a Migrator with verbosity level (default: 0).
# Constants
CamelCase represents a camelCase naming convention.
CockroachDB indicates that the DB is a CockroachDB instance.
DefaultInitialMigrationName is the name of the migration which will be run by Init, if not overridden in the Migrator.
DefaultMigrationNameConvention is the convention with which the names for migration files and functions will be generated, if not overridden in the Migrator.
DefaultMigrationTableName is the table in which migrations will be noted if not overridden in the Migrator.
DefaultMigrationTemplate is the template which will be used for Create, when using Create without a template.
Postgres indicates that the DB is an original Postgres instance or a DB which exactly matches the Postgres API.
SnakeCase represents a snake_case naming convention.
# Variables
ErrFileAlreadyExists indicates that an attempt was made to create a migration, without specifying a name.
ErrInitialMigrationNotKnown indicates that no migration was found with the name of the initial migration.
ErrInvalidMigrationFuncRegistered indicates that a migration is being registered with a function with invalid function signature.
ErrInvalidMigrationFuncRun indicates that a migration is being run with a function with invalid function signature.
ErrInvalidVerbosity indicates that verbosity has already been specified when attempting to specify quiet, or that quiet has already been specified when attempting to specify verbosity.
ErrMigrationAlreadyExists indicates that a migration is being registered with a name which has already been used.
ErrMigrationNotKnown indicates that a migration has been found in the DB, but with no corresponding known migration.
ErrNoMigrationName indicates that an attempt was made to create a migration, without specifying a name.
ErrNullMigrationFunc indicates that a migration is being registered with a null function for the up or down migration.
ErrUnknownNamingConvention indicates that an attempt was made to create a migration, without specifying a name.
# Structs
SnakeCaser will attempt to use camelCase for filenames.
Context contains some additional information which may be useful for migration functions.
Migrator can create or manage migrations as indicated by options during construction.
Registry holds a set of known migrations.
SnakeCaser will attempt to use snake_case for filenames.
# Interfaces
Caser is intended to convert a description to a particular naming convention.
# Type aliases
DBFactory returns a DB instance which will house both the migration table (to track completed migrations) and the tables which will be affected by the migrations.
MigrationNameConvention represents a naming convention in terms of casing and underscores for a Migrator.
MigratorOpt represents an option which can be applied to a migrator during creation.
PostgresFlavour indicates the type of Postgres-like API is being connected to.