Golang — How to Handle Database Migrations

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

Amrit Pal Singh
Level Up Coding

--

Photo by benjamin lehman on Unsplash

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

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 GORM handles DB migrations

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.

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.

Final Words

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

My other Golang related stories —

Like to experience Medium yourself? Consider supporting me and other writers by signing up for a membership. Membership is only $5 per month, it supports us, writers, at no extra cost. If you do so, I receive a portion of the fee, and it won’t cost you more. Thank you!

--

--

Cloud Software Engineer | Product Development | I write about Tech and Travel | Profile https://bit.ly/3dNxaiK | Golang Web Dev Course - https://bit.ly/go-gin