/mig

Simple Migration Golang Lib

Primary LanguageGoMIT LicenseMIT

Simple SQL Migration Tool

Only supports for PostgreSQL

Usage

func MigratePG(dir string, tx *sql.Tx) (err error)

Example

package postgres

import (
    "database/sql"
    "log"

    _ "github.com/lib/pq"
    "github.com/dilmurodov/mig"
)

func StartDBConn() {
    db, err := sql.Open("postgres", "user=postgres dbname=postgres sslmode=disable")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    tx, err := db.Begin()
    if err != nil {
        log.Fatal(err)
    }
    defer tx.Rollback()

    if err := mig.MigratePG("./migrations", tx); err != nil {
        log.Fatal(err)
    }

    if err := tx.Commit(); err != nil {
        log.Fatal(err)
    }
}

Migration File

-- 20160101_000000_create_table.sql

CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

License

MIT

Author

Dilmurodov Tolibbek