GithubHelp home page GithubHelp logo

wjwangji / gomigrate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidhuie/gomigrate

0.0 1.0 0.0 55 KB

A SQL database migration toolkit in Golang

License: MIT License

Go 95.61% PLpgSQL 3.54% TSQL 0.84%

gomigrate's Introduction

gomigrate

Build Status

A SQL database migration toolkit in Golang.

Supported databases

  • PostgreSQL
  • MariaDB
  • MySQL
  • Sqlite3

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")

You may also specify a specific logger to use, such as logrus:

migrator, _ := gomigrate.NewMigratorWithLogger(db, gomigrate.Postgres{}, "./migrations", logrus.New())

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.

id should not be 0 as that value is used for internal validations.

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.

gomigrate's People

Contributors

davidhuie avatar c4milo avatar flemeur avatar veqryn avatar ralphpina avatar guoqchen1001 avatar rogerclermont avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.