How to enable auto migration in Go?

This article is to enable auto migration in GoLang with integration with GORM in up-down approach.

Create migration CLI

To enable auto migration in Go with GORM install golang-migrate.

  • Run following commands:
$ go get -u -d github.com/golang-migrate/migrate/cmd/migrate
$ cd $GOPATH/src/github.com/golang-migrate/migrate/cmd/migrate
$ git checkout $TAG # e.g. v4.8.0
$ go build -tags 'postgres' -ldflags="-X main.Version=$(git describe --tags)
  • Navigate to your migrate project and find out migrate.exe file.
  • Copy migrate.exe and paste it into $GOPATH/bin folder.
  • Your migration CLI is ready to use. N.B: [find out the latest version of migrate cmd and replace it]
  • Read more about CLI.

Run your first migration.

i.e: migrate create -ext sql -dir db/migrations -digits 7 create_users_table

  • migrate command will create a db/migrations folder with to sql ( up/down) file.
  • Edit up file. for example-
CREATE TABLE IF NOT EXISTS users(
user_id serial PRIMARY KEY,
username VARCHAR (50) UNIQUE NOT NULL,
password VARCHAR (50) NOT NULL,
email VARCHAR (300) UNIQUE NOT NULL
);
  • Edit down file. for example-
DROP TABLE IF EXISTS users;
  • Now let’s install golang-migrate.
$ go get github.com/golang-migrate/migrate
  • Configure migration with GORM.

If you prefer a command-line tool over configured code then it’s for you!

  • Remove GORM configuration. (If any)
db, err := gorm.Open("postgres", config.ConnectionString)
// driver, err := postgres.WithInstance(db.DB(), &postgres.Config{})
// m, err := migrate.NewWithDatabaseInstance(
// "file://db/migrations",
// "postgres", driver)
// m.Steps(2)
  • Run migration script. i.e:
migrate -source file://{migration file path} -database postgres://{user name}:{password}@{db location}/{db name}?sslmode=disable up
  • For example:
migrate -source file://db/migrations -database postgres://postgres:648782@localhost:5432/zachai_auction?sslmode=disable up
  • Use down for dropping
  1. Happy Coding…😍😍😍

--

--

--

Every lie we tell incurs a debt to the truth. Sooner or later, that debt is paid.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

READ/DOWNLOAD$- PPI Practice Problems for the Mechanical Engineering PE Exam, 13th Edition…

Pry at a Glance

Innovation Week: How I got started at Geckoboard

Starting my python programming journey today with this awesome community…

Say Hi to Linux!

2018 in Review: Our Achievements, Stats, and Aspirations for the Year to Come

Remote Development with Docker WSL2 and Visual Studio Code

Django template and Mathjax configuration for dynamic content

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Faisal AL Mahmud

Faisal AL Mahmud

Every lie we tell incurs a debt to the truth. Sooner or later, that debt is paid.

More from Medium

Templates in Golang

Consuming an API with Golang

lancet (util function library of go) released v2.0.0, fully supports go generic features

Learning Go from documentation #5 (Last Create a module blog)