pgmigration

package module
Version: v0.0.0-...-b721838 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2017 License: MIT Imports: 5 Imported by: 1

README

PostgreSQL Database Migrations

GoDoc

Package pgmigration provides support for PostgreSQL database migrations.

Install

This package provide support for migrations written in plain SQL scripts.

In your project, place your migrations in a separate folder, for example, db/migrations.

Migrations are sorted using their file name and then applied in the sorted order. Since sorting is important, name your migrations accordingly. For example, add a timestamp before migration name. Or use any other ordering scheme you'll like.

Note that migration file names are saved into a table, and the table is used later on to detect which migrations have already been applied. In other words, don't rename your migration files once they've been applied to your DB.

Install go-bindata and run this command:

go-bindata -pkg myapp -o bindata.go db/migrations/

The bindata.go file will contain your migrations. Regenerate your bindata.go file whenever you add migrations.

Use

In your app code, import pgmigration package:

import "github.com/baijum/pgmigration"

Then, run the migrations.

Make sure the migrations have an .sql ending. Any script with file name prefix as ignore will be ignored.

After app startup and after a sql.DB instance is initialized in your app, run the migrations. Assuming you have a variable called DB that points to sql.DB and the migrations are located in db/migrations, execute the following code:

// DB is the database connection wrapper
var DB *sql.DB

// SchemaMigrate migrate database schema
func SchemaMigrate() error {
	return pgmigration.Migrate(DB, AssetNames, Asset, nil)
}

The last argument is a string pointer which can refer to the last script, if there is any. This will be useful to write tests.

Credits

This project started as a fork of https://github.com/tanel/dbmigrate written by Tanel Lebedev

Documentation

Overview

Package pgmigration provides support for PostgreSQL database migrations.

This package provide support for migrations written in plain SQL scripts.

In your project, place your migrations in a separate folder, for example, "db/migrations".

Migrations are sorted using their file name and then applied in the sorted order. Since sorting is important, name your migrations accordingly. For example, add a timestamp before migration name. Or use any other ordering scheme you'll like.

Note that migration file names are saved into a table, and the table is used later on to detect which migrations have already been applied. In other words, don't rename your migration files once they've been applied to your DB.

Install go-bindata ( https://github.com/jteeuwen/go-bindata ) and run this command:

go-bindata -pkg myapp -o bindata.go db/migrations/

The "bindata.go" file will contain your migrations. Regenerate your "bindata.go" file whenever you add migrations.

In your app code, import pgmigration package:

import "github.com/baijum/pgmigration"

Then, run the migrations.

**Make sure the migrations have an .sql ending.** Any script with file name prefix as `ignore` will be ignored.

After app startup and after a "sql.DB" instance is initialized in your app, run the migrations. Assuming you have a variable called "db" that points to "sql.DB" and the migrations are located in "db/migrations", execute the following code:

// DB is the database connection wrapper
var DB *sql.DB

// SchemaMigrate migrate database schema
func SchemaMigrate() error {
	return pgmigration.Migrate(DB, AssetNames, Asset, nil)
}

The last argument is a string pointer which can refer to the last script, if there is any. This will be useful to write tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(db *sql.DB, assetNames func() []string, asset func(name string) ([]byte, error), lastScript *string) error

Migrate run the migrations written in SQL scripts

Types

This section is empty.

Jump to

Keyboard shortcuts

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