migrate

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2021 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Go database migrate

This is a simple database migration library for Go. This library is intended to be used with go-bindata.

We recommended using a go generate command to update the go-bindata file.

Installation

You can install this library with go get

go get git.fuyu.moe/Fuyu/migrate

Example

files/0001.sql:

CREATE TABLE tests (
	Name varchar(100) NOT NULL
);

INSERT INTO tests VALUES ('migration test');

Run go-bindata --prefix files files

Optionally you can add the flags -nomemcopy and -nometadata

main.go:

package main

import (
	"database/sql"
	"fmt"

	"git.fuyu.moe/Fuyu/migrate"
	_ "github.com/lib/pq"
)

func main() {
	db, err := sql.Open(`postgres`, `host=/run/postgresql dbname=testdb sslmode=disable`)
	if err != nil {
		fmt.Println(`Failed to connect to database. Message:`, err)
		return
	}
	err = migrate.Migrate(db, 1, migrate.Options{Schema: `testschema`}, Asset)
	if err != nil {
		fmt.Println(`The migration failed! Message:`, err)
		return
	}

	fmt.Println(`The database migration/update was successful`)
}

File names:

The names of your migration files should look like this:

0001.sql
0002.sql
...
0010.sql
...
0325.sql

Documentation

Overview

Package migrate allows you to update your database from your application

Index

Constants

View Source
const DefaultTableName = `version`

DefaultTableName is the name used when no TableName is specified in Options

Variables

View Source
var ErrUpdatesMissing = errors.New(`Missing migration files`)

ErrUpdatesMissing indicates an update is missing, making it impossible to execute the migration

Functions

func Migrate

func Migrate(db *sql.DB, o Options, assets fs.FS) error

Migrate executes all migrations Filenames need to have incrementing numbers Downgrading is not supported as it could result in data loss

Types

type AssetFunc

type AssetFunc func(string) ([]byte, error)

AssetFunc is a function that returns the data for the given name

type Options

type Options struct {
	TableName string // Name used for version info table; defaults to DefaultTableName if not set
	Schema    string // Schema used for version info table; In PostgreSQL the current schema is changed to the one specified here
}

Options contains all settings

Jump to

Keyboard shortcuts

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