gogolok / gomigrate

A SQL database migration toolkit in Golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gomigrate

Build Status

A SQL database migration toolkit in Golang.

Supported databases

  • PostgreSQL
  • MariaDB
  • MySQL

Usage

First import the package:

import "github.com/DavidHuie/gomigrate"

Given a database/sql database connection to a PostgreSQL database, db, and a directory to migration files, create a migrator:

migrator := gomigrate.NewMigrator(db, gomigrate.Postgres{}, "./migrations")

To migrate the database, run:

err := migrator.Migrate()

To rollback the last migration, run:

err := migrator.Rollback()

Migration files

Migration files need to follow a standard format and must be present in the same directory. Given "up" and "down" steps for a migration, create a file for each by following this template:

{{ id }}_{{ name }}_{{ "up" or "down" }}.sql

For a given migration, the id and name fields must be the same. The id field is an integer that corresponds to the order in which the migration should run relative to the other migrations.

Example

If I'm trying to add a "users" table to the database, I would create the following two files:

1_add_users_table_up.sql

CREATE TABLE users();

1_add_users_table_down.sql

DROP TABLE users;

Copyright

Copyright (c) 2014 David Huie. See LICENSE.txt for further details.

About

A SQL database migration toolkit in Golang

License:MIT License


Languages

Language:Go 100.0%