dbmigrate

package module
v0.0.0-...-a9ed8a6 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2015 License: MIT Imports: 8 Imported by: 0

README

Build Status

Supported databases

  • PostgreSQL
  • Cassandra

Install

In your project, place your migrations in a separate folder, for example, db/migrate. 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.

Use

In your app code, import dbmigrate package:

import (
  "log"
  "path/filepath"

  "github.com/tanel/dbmigrate"
)

Then, run the migrations, depending on your database type.

Use with PostgreSQL

Make sure the migrations have an .sql ending.

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/migrate, execute the following code:

if err := dbmigrate.Run(db, filepath.Join("db", "migrate")); err != nil {
  log.Fatal(err)
}

Use with Cassandra

Make sure the migrations have an .cql ending.

After app startup, open a session and run migrations:

session, err := cluster.CreateSession()
if err != nil {
	return err
}
defer session.Close()

cassandraMigrations := dbmigrate.NewCassandraDatabase(session, session)
if err := dbmigrate.ApplyMigrations(cassandraMigrations, filepath.Join("db", "migrate")); err != nil {
  return err
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMigrations

func ApplyMigrations(database Database, migrationsFolder string) error

Run applies migrations from migrationsFolder to database.

func Run

func Run(db *sql.DB, migrationsFolder string) error

By default, apply Postgresql migrations, as in older versions

Types

type CassandraDatabase

type CassandraDatabase struct {
	// contains filtered or unexported fields
}

func NewCassandraDatabase

func NewCassandraDatabase(readerSession *gocql.Session, writerSession *gocql.Session) *CassandraDatabase

func (*CassandraDatabase) CreateMigrationsTable

func (cassandra *CassandraDatabase) CreateMigrationsTable() error

func (*CassandraDatabase) HasMigrated

func (cassandra *CassandraDatabase) HasMigrated(filename string) (bool, error)

func (*CassandraDatabase) Migrate

func (cassandra *CassandraDatabase) Migrate(filename string, migration string) error

type Database

type Database interface {
	CreateMigrationsTable() error
	HasMigrated(filename string) (bool, error)
	Migrate(filename string, migration string) error
}

type PostgresDatabase

type PostgresDatabase struct {
	// contains filtered or unexported fields
}

func NewPostgresDatabase

func NewPostgresDatabase(db *sql.DB) *PostgresDatabase

func (*PostgresDatabase) CreateMigrationsTable

func (postgres *PostgresDatabase) CreateMigrationsTable() error

func (*PostgresDatabase) HasMigrated

func (postgres *PostgresDatabase) HasMigrated(filename string) (bool, error)

func (*PostgresDatabase) Migrate

func (postgres *PostgresDatabase) Migrate(filename string, migration string) error

Jump to

Keyboard shortcuts

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