Golang Tutorial: How to Migrate Your Database

Written by amritsingh | Published 2022/04/08
Tech Story Tags: programming | coding | golang | ruby | database | database-migration | gorm | web-development

TLDRGolang 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.via the TL;DR App

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:

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 —

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.

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 —

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.


Written by amritsingh | Cloud Software Engineer | Product Development | I write about Tech and Travel | Profile https://bit.ly/3dNxaiK
Published by HackerNoon on 2022/04/08