Skip to content

omniosorg/morph

This branch is 4 commits behind mattermost/morph:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

c59c176 · Nov 3, 2023
May 11, 2023
Nov 24, 2022
Feb 19, 2022
Nov 3, 2023
May 11, 2023
Nov 24, 2022
May 11, 2023
Nov 4, 2022
May 11, 2023
Aug 19, 2022
Mar 5, 2021
Mar 5, 2021
May 11, 2023
Nov 10, 2022
Feb 15, 2022
Nov 15, 2022
May 11, 2023
May 11, 2023
May 10, 2023
May 9, 2023
Nov 15, 2022

Repository files navigation

Morph_Logo

A database migration tool designed to make schema migrations easy.

Morph

GitHub Workflow Status (branch) GoDoc

As an application evolves, the data flowing inside inevitably evolves along with it. If you have an application that persists the data in a relational database, the way you store the data will probably change over time. Morph is a database migration tool that helps you to apply your migrations. It is written with Go so you can use it from your Go application as well. Read our blog post to learn more about the motivation behind this project.

Usage

Morph can be used as a library or a CLI tool.

Library

import (
    "context"

    "github.com/mattermost/morph"
    "github.com/mattermost/morph/drivers/mysql"
    "github.com/mattermost/morph/sources/embedded"
)

src, err := embedded.WithInstance(&embedded.AssetSource{
    Names: []string{}, // add migration file names
    AssetFunc: func(name string) ([]byte, error) {
        return []byte{}, nil // should return the file contents
    },
})
if err != nil {
    return err
}
defer src.Close()

driver, err := mysql.WithInstance(db)
if err != nil {
    return err
}

engine, err := morph.New(context.Background(), driver, src)
if err != nil {
    return err
}
defer engine.Close()

engine.ApplyAll()

CLI

To install morph you can use:

go install github.com/mattermost/morph/cmd/morph@latest

Then you can apply your migrations like below:

morph apply up --driver postgres --dsn "postgres://user:pass@localhost:5432/mydb?sslmode=disable" --path ./db/migrations/postgres --number 1

Migration Files

The migrations files should have an up and down versions. The program requires each migration to be reversible, and the naming of the migration should be in the following form:

0000000001_create_user.up.sql
0000000001_create_user.down.sql

The first part will be used to determine the order in which the migrations should be applied and the next part until the up|down.sql suffix will be the migration name.

The program requires this naming convention to be followed as it saves the order and names of the migrations. Also, it can rollback migrations with the down files.

LICENSE

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.6%
  • Makefile 0.4%