migration

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2015 License: MIT Imports: 5 Imported by: 0

README

go-migration

Go library for database schema migrations.

Reads sql files from a directory and imports them to the database. Files that have been already imported are skipped.

This has currently been tested only with PostgreSQL but should work with other databases.

Import

import "gopkg.in/tkorri/go-migration.v2"

Usage

The migration is executed with Upgrade method.

import (
    "gopkg.in/tkorri/go-migration.v2"
)

database, err := sql.Open("postgres", "user=example password=example dbname=example sslmode=disable")
if err != nil {
    return err
}

err = migration.Upgrade(database, "example")
if err != nil {
    return err
}

Or if you want to tweak the configurations you can use UpgradeDir.


config := &Configuration{
    Project:   "example",
    TableName: "migration_tbl",
}

err = migration.UpgradeDir(database, config, "migrations/")
if err != nil {
    return err
}

Versions

go-migration uses gopkg.in for versioning. The supported versions are:

  • gopkg.in/tkorri/go-migration.v1 - The initial version
  • gopkg.in/tkorri/go-migration.v2 - Added support for providing upgrade files as an array of strings

Documentation

Documentation is available at godoc.org.

License

Copyright (c) 2015 Taneli Korri

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package migration handles database schema migrations.

Migration reads migration_tbl from the database (if one exists) and compares the rows to the files available in the migration directory. If file is found from the directory that doesn't exists in the database, the file contents is read and executed as sql.

The current version of migration has only been tested with PostgreSQL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLog

func SetLog(l standardlog.Logger)

SetLog sets the logger used for the logging ouput

func Upgrade

func Upgrade(db *sql.DB, project string) error

Upgrade database using the the given database connection and read the migration sql files from "migrations/" directory

func UpgradeDir

func UpgradeDir(db *sql.DB, config *Configuration, directory string) error

Upgrade database using the the given database connection and read the migration sql files from the given directory

func UpgradeItems

func UpgradeItems(db *sql.DB, config *Configuration, items []MigrationItem) error

UpgradeItems upgrades the database with the given migration items

Types

type Configuration

type Configuration struct {
	Project   string // Project id for this project
	TableName string // Migration table name
}

type MigrationItem

type MigrationItem struct {
	ID      string // Unique id for this database change
	Content string // The sql content
}

Jump to

Keyboard shortcuts

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