dbolve

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2019 License: MIT Imports: 7 Imported by: 0

README

CircleCI codecov GoDoc Go Report Card

dbolve

Very simple code only migration library for go

Features

  • Very simple and readable code (< 200 lines of code)
  • Easy to use interface
  • Transaction safety for each migration
  • Verifies that already applied transactions haven't changed

Usage

go get -u github.com/cinemast/dbolve
Quickstart
package main

import (
	_ "github.com/lib/pq"
	"database/sql"
	"log"
	"os"
	"fmt"
	"github.com/cinemast/dbolve"
)

func main() {
	db, err := sql.Open("postgres", "postgres://postgres:postgres@localhost/dbolve_test?sslmode=disable")
    if err != nil {
		panic(err)
	}

	migrations := []dbolve.Migration{
		dbolve.Migration{
			Name: "Add acccount table",
				Code: func(tx dbolve.Transaction) error {
					return tx.Exec(`CREATE TABLE account(user_id serial PRIMARY KEY, username VARCHAR (50) UNIQUE NOT NULL, password VARCHAR (50) NOT NULL);`)
				},
			},
		dbolve.Migration{
		Name: "Add acccount 2 table",
			Code: func(tx dbolve.Transaction) error {
				return tx.Exec(`CREATE TABLE account2(user_id serial PRIMARY KEY, username VARCHAR (50) UNIQUE NOT NULL, password VARCHAR (50) NOT NULL);`)
			},
		},
	}
	m,err := dbolve.NewMigrator(db, migrations)
	if err != nil {
		panic(err)
	}
	m.Log = log.New(os.Stdout, "", log.LstdFlags)
	if err := m.Migrate(); err != nil {
		panic(err)
	}
	fmt.Println("Finished migrations")
}

Motivation

Heavily inspired by lopezator/migrator.

I was missing two features:

  • Allow to list already applied and pending migrations
  • Verification that already applied migrations match the current migration code

TODO

  • CI Setup and coverage metrics
  • Properly versioned releases

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Migration

type Migration struct {
	Name      string
	Code      func(t Transaction) error
	Timestamp string
	// contains filtered or unexported fields
}

Migration struct

type Migrator

type Migrator struct {
	Migrations []Migration
	Log        *log.Logger
	// contains filtered or unexported fields
}

Migrator type

func NewMigrator

func NewMigrator(db *sql.DB, migrations []Migration) (*Migrator, error)

NewMigrator creates a new instance of Migrator

func (*Migrator) Applied

func (m *Migrator) Applied() []Migration

Applied returns a slice of already applied migrations

func (*Migrator) CountApplied

func (m *Migrator) CountApplied() int

CountApplied returns the number of already applied migrations

func (*Migrator) DryRun

func (m *Migrator) DryRun() error

DryRun tries to run the migrations but rollbacks each transaction

func (*Migrator) Migrate

func (m *Migrator) Migrate() error

Migrate run's all missing migrations

func (*Migrator) Pending

func (m *Migrator) Pending() []Migration

Pending returns a slice of not yet applied migrations

type Transaction

type Transaction interface {
	Exec(sql string) error
}

Transaction exposes allowed database operations for migrations

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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