migrations

package module
v0.0.0-...-0bac4e1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2018 License: MIT Imports: 12 Imported by: 0

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"
	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"
	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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, up, down func(*pg.Tx) error)

func Run

func Run(db *pg.DB, cmd, name, template string) error

func SetInitialMigration

func SetInitialMigration(migrationName string)

func SetMigrationNameConvention

func SetMigrationNameConvention(convention MigrationNameConvention)

func SetMigrationTableName

func SetMigrationTableName(tableName string)

Types

type MigrationNameConvention

type MigrationNameConvention string
const (
	CamelCase MigrationNameConvention = "camelCase"
	SnakeCase MigrationNameConvention = "snakeCase"
)

Jump to

Keyboard shortcuts

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