sqltractor

module
v0.0.0-...-2df506a Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2016 License: MIT

README

sqltractor

SQL schema migration tool for Go. Use it in your existing Go code or run commands via the CLI.

Usage

from Terminal

# install
go get github.com/netw00rk/sqltractor/sqltractor-cli

# create new migration file in path
sqltractor-cli -url driver://url -path ./migrations create migration_file_xyz

# apply all available migrations
sqltractor-cli -url driver://url -path ./migrations up

# roll back all migrations
sqltractor-cli -url driver://url -path ./migrations down

# show the current migration version
sqltractor-cli -url driver://url -path ./migrations version

# apply the next n migrations
sqltractor-cli -url driver://url -path ./migrations migrate +1
sqltractor-cli -url driver://url -path ./migrations migrate +2
sqltractor-cli -url driver://url -path ./migrations migrate +n

# roll back the previous n migrations
sqltractor-cli -url driver://url -path ./migrations migrate -1
sqltractor-cli -url driver://url -path ./migrations migrate -2
sqltractor-cli -url driver://url -path ./migrations migrate -n

# go to specific migration
sqltractor-cli -url driver://url -path ./migrations goto 1
sqltractor-cli -url driver://url -path ./migrations goto 10
sqltractor-cli -url driver://url -path ./migrations goto v

in Go code

See GoDoc here: http://godoc.org/github.com/netw00rk/sqltractor/tractor

import "github.com/netw00rk/sqltractor/tractor"

// Import required driver and reader
import "github.com/netw00rk/sqltractor/driver/postgres"
import "github.com/netw00rk/sqltractor/reader/file"


func main() {
    // create tractor struct
    t := &tractor.SqlTractor{
        Driver: postgres.New("driver://url")
        Reader: file.NewFileReader("./path/to/migration/files")
    }

    // UpAsync returning chan of Result structure
    // type Result struct {
    //    File  // applied file
    //    Error // error if something happened
    //}
    for r := range t.UpAsync() {
        if r.Error != nil {
            if r.File != nil {
                fmt.Printf("Error %s while applying file %s", r.Error, r.File.FileName)
            }
        }
        // do something with applied file
    }

    // usage of synchronous wrapper that
    // return slice of applied migration files and error
    files, err := tractor.Up(t)
    if err != nil {
      // do something with error
    }
}

Available Drivers

Need another driver? Just implement the Driver interface and open a PR.

Avaiable readers

Migration files

The format of migration files looks like this:

001_initial_plan_to_do_sth.up.sql     # up migration instructions
001_initial_plan_to_do_sth.down.sql   # down migration instructions
002_xxx.up.sql
002_xxx.down.sql
...

Why two files? This way you could still do sth like psql -f ./db/migrations/001_initial_plan_to_do_sth.up.sql and there is no need for any custom markup language to divide up and down migrations. Please note that the filename extension depends on the driver.

Acknowledgements

Many thanks goes to Matthias Kadenbach, https://github.com/mattes and all contributors to the https://github.com/mattes/migrate for the ideas and code

Directories

Path Synopsis
Package driver holds the driver interface.
Package driver holds the driver interface.
cassandra
Package cassandra implements the Driver interface.
Package cassandra implements the Driver interface.
mysql
Package mysql implements the Driver interface.
Package mysql implements the Driver interface.
postgres
Package postgres implements the Driver interface.
Package postgres implements the Driver interface.
sqlite3
Package sqlite3 implements the Driver interface.
Package sqlite3 implements the Driver interface.
Package main is the CLI.
Package main is the CLI.
migration/direction
Package direction just holds convenience constants for Up and Down migrations.
Package direction just holds convenience constants for Up and Down migrations.
migration/file
Package file contains functions for low-level migration files handling.
Package file contains functions for low-level migration files handling.

Jump to

Keyboard shortcuts

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