Golang: Database migration to manage your database schema by creating incremental SQL


In this post I write down my experience regarding  database migration configuration for Golang using Goose database migration.

Goose installation

$ go get -u github.com/pressly/goose/cmd/goose

This will install the goose binary to your $GOPATH/bin directory.

1.Install
Goose binary installed in bin folder

How to use

$ goose

2.run_goose
Goose executable in action

Create a SQL migration using Goose

$ postgres=# CREATE DATABASE teahrm

3.create-users
Golang SQL migration using Goose
8.empty-migration-generated.png
Just generated empty Migration

Create target database to migrate with

$ goose create create_tables_users sql

4.create-database.png
List of existing databases

Connect to the PostgresSQL database thru psql

$ postgres=# \connect teahrm

Show tables

$ teahrm=# \dt

7.connect-database
Empty database without table yet

Create migration for table users (PostgreSQL datatype)

Migrate Up

goose postgres "user=postgres dbname=teahrm sslmode=disable" up

13.migrate-up.png

Migrate Down

goose postgres "user=postgres dbname=teahrm sslmode=disable" down

14.migrate-down

Common mistake while migrating up

10-common-mitsake
Missing connection information
12.wrongdbname

Wrong database name: goose: no migrations to run. current version: 1

Check Connection status to avoid connection error

goose postgres "user=postgres dbname=postgres sslmode=disable" status

11.connectionstatus

That’s all guys… have a nice weekend…

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s