migrator

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2022 License: MIT Imports: 4 Imported by: 5

README

About

Migrator is a simple and easy to use database migration library.

Supported database engines

All databases that has a go driver should work out of the box.

It has been successfully tested on Postgres and SQLite.

See the following link for a complete list of drivers: https://github.com/golang/go/wiki/SQLDrivers

Migration repositories

The migrations are stored within a Repository, it can either be file based or stored directly in memory. Check within the repositores directory for more information.

Usage example

package main

import (
	"database/sql"

	_ "github.com/mattn/go-sqlite3"
	"github.com/osm/migrator"
	"github.com/osm/migrator/repository"
)

func main() {
	// Initialize a new test db
	db, err := sql.Open("sqlite3", "./test.db")
	if err != nil {
		panic(err)
	}

	// Create a new mem repo
	repo := repository.FromMemory(map[int]string{
		1: "CREATE TABLE migration (version text NOT NULL PRIMARY KEY);\n",
		2: "CREATE TABLE foo (version text NOT NULL PRIMARY KEY);\n",
		3: "INSERT INTO foo VALUES(123);\n",
	})

	// Migrate the database to the latest version
	if err := migrator.ToLatest(db, repo); err != nil {
		panic(err)
	}
}

Documentation

Overview

Migrator is a simple and easy to use database migration library.

Technical details

Each migration is executed within its own transaction.

Execution will stop immediately and a rollback will be issued if there's any problem with the migration.

It will only rollback the failed migration and stop execution, all successfully migrated versions will be kept.

All exported functions expects a working database connection.

The migrator functions makes sure that the connection to the database is working before any migration scripts are executed.

It will load the migration scripts from the repository, any errors on the steps above will be returned immediately.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToLatest

func ToLatest(db *sql.DB, repo repository.Source) error

ToLatest migrates the database to the latest version.

func ToVersion

func ToVersion(db *sql.DB, repo repository.Source, toVersion int) error

ToVersion migrates the database to a specific version.

Setting the version to a version earlier than the database currently has is not supported.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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