Skip to content

jacobmoe/pgmig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgmig

Migration management library using go-pg

Provides a simple interface for creating, running and packaging Postgres schema migrations. Generated migrations are up and down SQL files.

Dependencies

go 1.16+

Example Usage

package main

import (
	"embed"
	"github.com/go-pg/pg/v10"
	"github.com/jacobmoe/pgmig"
)

//go:embed path/to/migrations/dir
var migrationsFS embed.FS

func main() {
	// create new up and down migration files:
	//  - path/to/migrations/dir/200405153854_create_users.up.sql
	//  - path/to/migrations/dir/200405153854_create_users.down.sql
	err = pgmig.Create("/full/path/to/migrations/dir", "create_users")
	check(err)

	db := pg.Connect(&pg.Options{
		Addr:     "localhost:5432",
		User:     "dbuser",
		Password: "dbpassword",
		Database: "dbname",
	})

	// initialize new migrator
	mig := pgmig.New(db, migrationsFS)

	// initialize the migrations table in your db
	// only need to be run this once for a database
	err := mig.Init()
	check(err)

	// run new migrations (after updating the migration files)
	err = mig.Migrate()
	check(err)

	// rollback most recent migration
	err = mig.Rollback()
	check(err)
}

func check(err error) {
	if err != nil {
		panic(err)
	}
}

About

Generate, run and package Postgres migrations using go-pg

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages