Golang Tutorial: How to Migrate Your Database by@amritsingh
378 reads

Golang Tutorial: How to Migrate Your Database

tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

Golang has been gaining popularity since its inception. It is a simple language with performance as good as a low-level language like C++. Acceptance of Golang in web development is booming at a high rate. It handles database migrations wonderfully by making them part of your code. GORM is the most popular ORM for Go. It has a lot of similarities with Active Record with the same name as Active Record. To create a table you create a new migration that looks like this: To remove a column you need to explicitly write code like this.
featured image - Golang Tutorial: How to Migrate Your Database
Amrit Singh HackerNoon profile picture

@amritsingh

Amrit Singh

Cloud Software Engineer | Product Development | I write about...

About @amritsingh
LEARN MORE ABOUT @AMRITSINGH'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

What are the ways and how I do it as a Rubyist

Golang has been gaining popularity since its inception. It is a simple language with performance as good as a low-level language like C++. Acceptance of Golang in web development is booming at a high rate.

I started my web-development journey with Ruby on Rails. I fell in love with Active Record. I was amazed to see an ORM that was so simple to use. It handles database migrations wonderfully by making them part of your code.

How DB migrations are handled in Rails (with Active Record)

Truly speaking, Rails and Active Record made me lazy. Active Record and its DB migrations were so easy to use that I stopped writing SQL Queries directly.

To create a table you create a new migration that looks like this:

Data migration to create a table

Data migration to create a table

It is easy enough to alter the table by writing a similar migration.

Apart from migrations, Active Record has the most simple models to interact with the database. You don’t have to specify each and every column in the model, it takes the column names from the table.

Golang ORMs

There are many ORM libraries for Go like — gorm, xorm, sqlboiler, reform, and many more. GORM is the most popular ORM for Go. It has a lot of similarities with Active Record.

How to handle DB migrations with GORM

Unlike Active Record, you specify a structure in the model that defines the table. e.g. blogs table can be specified like this —

image

Just specifying a struct does not make changes in the database. You need to explicitly call AutoMigrate method for each model to run DB migrations.

image

What happens when you change the struct?

  • Add a column
  • Altering a table is not straightforward to alter a table. If you add a column to a table, you just need to add the column in the struct. When the application runs it adds the column to the table.
  • Remove a column
  • When you remove a column from the struct, data migration does not remove the column from the table. To remove a column you need to explicitly write code like this —

image

Other Scenarios

There are ways to handle scenarios like —

  • Creating a table
  • Dropping a table
  • Renaming a table
  • Making changes to a column
  • Indices

For more details please check this documentation.

Final Words

There is no right or wrong way to do this. Developers have their preferences and skills that help them make such decisions.

What do I prefer?

Though data migration features of GORM are capable to handle any scenario, there is a learning curve. And if you are using auto migration you need to be aware of how it works.

Due to these reasons, I prefer to write SQL statements to manage DB schema. I know this is old school, but I find it easy.

In one of the projects, which is an offspring of a Rails project I use Active Record migrations to maintain the DB schema of the Golang project as well.

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa