DEV Community

Cover image for How to write & run database migration in Golang

How to write & run database migration in Golang

TECH SCHOOL on July 10, 2020

When working with database, schema migration is one important task that we often have to do throughout the application lifetime to adapt to new bus...
Collapse
 
lil5 profile image
Lucian I. Last

Please use go install github.com/golang-migrate/migrate/v4 and add go’s bin directory to your PATH. Using brew for a go package doesn’t sound like a good idea, considering version differences

Collapse
 
thewackyindian profile image
Raj Karan Singh

I am on windows and did this, like ran this command on the terminal, it ran and installed it and said package is not a main package
then i ran the migrate --version command
and its not able to find it like not installed or something, confused with gobin and gopath.

Collapse
 
lil5 profile image
Lucian I. Last • Edited

I think your first problem is that you’re running Windows but further you might want to add your ~/go/bin directory to your PATH env in bashrc

Collapse
 
athulmuralidhar profile image
Athul Muralidhar

cant stress this one enough

Collapse
 
sergeypodgornyy profile image
Sergey Podgornyy

You can also try using migrator package to write migrations using Go

GitHub logo larapulse / migrator

MySQL database migrator

MySQL database migrator

Build Status Software License codecov Go Report Card GoDoc Mentioned in Awesome Go Release TODOs

MySQL database migrator designed to run migrations to your features and manage database schema update with intuitive go code. It is compatible with the latest MySQL v8.

Installation

To install migrator package, you need to install Go and set your Go workspace first.

  1. The first need Go installed (version 1.13+ is required), then you can use the below Go command to install migrator.
$ go get -u github.com/larapulse/migrator
Enter fullscreen mode Exit fullscreen mode
  1. Import it in your code:
import "github.com/larapulse/migrator"
Enter fullscreen mode Exit fullscreen mode

Quick start

Initialize migrator with migration entries:

var migrations = []migrator.Migration{
    {
        Name: "19700101_0001_create_posts_table"
        Up: func() migrator.Schema {
            var s migrator.Schema
            posts := migrator.Table{Name: "posts"}

            posts.UniqueID("id")
            posts.Varchar("title", 64)
            posts.Text("content", false)
            posts.Timestamps()

            s.CreateTable(posts
Enter fullscreen mode Exit fullscreen mode
Collapse
 
techschoolguru profile image
TECH SCHOOL

Thanks for sharing, Sergey!

Collapse
 
acetorscode profile image
Apoorv Mishra

Getting this error while migratedown

"Dirty database version -1. Fix and force version."

can anyone help with a solution! it will be a great help if you can.

Collapse
 
mquanit profile image
Mohammad Quanit

Hi Apoorv, try this cmd first

migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" force 1

and then run makefile cmd

make migrateup

it should solve this issue. Here's the docs for it
github.com/golang-migrate/migrate/...

Collapse
 
covrom profile image
Roman Сovanyan
Collapse
 
ridhs193 profile image
Ridham Modh

when i type run make migratedown in terminal i am getting following error:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

I am not getting the issue!

Collapse
 
utamori profile image
森 優太 mori

How do I seed the DB in this example?

Collapse
 
techschoolguru profile image
TECH SCHOOL

Hi Mori, please check out lecture 4 and 5 of this series. You will see how to generate codes to insert data to the database, and also how to generate random data for seeding or testing.

Collapse
 
utamori profile image
森 優太 mori

Thank you

Collapse
 
abramq profile image
abramq • Edited

There is probably error on the orange picture "MIGRATE DOWN" - files shown there should be ...down.sql not ...up.sql
Right?