DEV Community

Cover image for Golang Database Migration With Golang Migrate and Sqlc
Timileyin Daso
Timileyin Daso

Posted on

Golang Database Migration With Golang Migrate and Sqlc

Database migrations are a crucial component of controlling a software application's evolution. They give programmers the ability to modify the database schema while maintaining the accuracy and consistency of the data. Golang-migrate, a strong library for the Go programming language, offers a practical method for managing database migrations. In this article, we'll look at how to do efficient database migrations in Go using golang-migrate.
Firstly, let's start by answering:

What Is SQLC

SQLC stands for SQL Compiler is a tool and framework used for type-safe SQL queries in Go programming. SQLC allows you to write SQL queries as part of your Go code, and it automatically generates Go code that represents those queries as native Go functions. like we defined it earlier it is just a framework to generate type safe queries in Golang.

Getting Started With Golang Migrate
To start with golang migrate you have to first install Golang on your machine. Here is the link to download Go for your respective device OS then get your project setup.
The next thing is to install golang migrate

go get -u -d github.com/golang-migrate/migrate/cmd/migrate
Enter fullscreen mode Exit fullscreen mode


or brew install golang-migrate for Mac OS

You can use golang-migrate to manage your database migrations once the installation is complete.

How To Create A Migration

Running a golang migrate create command will help you create a sequence up and down .sql file for your migration name. This SQL files are used by Golang-migrate to specify database migrations. Each migration file contains a series of SQL statements that must be run and reflects a particular version of the database schema.
And below is the command to generate a migration files

migrate create -ext sql -dir  <migrationDirectory> -seq {theName}
Enter fullscreen mode Exit fullscreen mode

This command will generate a new migration file in the specified directory with a name reflecting the migration version and a descriptive name.
For example:

migrate create -ext sql -dir  <db/migration> -seq init-schema
Enter fullscreen mode Exit fullscreen mode

This will generate this files for us in the db/migration folder
Image description

where the up.sql file is use to make changes to the database why down.sql file is to revert the changes.

How To use the migration file

Below is an instance of how to use the migration file in the generated init-schema.up.sql file.

Image description

In the image above is our SQL queries to create users table in the table and below is how to revert the changes.

Image description

The above steps has create for us migration file, the next step is to migrate the schema migration file to the Database.

Migrating the migration file to the database.

The code below shows how to migrate a migration file to a Postgres Database

Image description

where root is the username and secret is the password, localhost is the host, 5432 is the port and Sample is the name of the database to migrate into.

To remove such migration from the DB also, all we have to do is to run this command.

Image description

In the next article I will explain how to generate CRUD Golang code from the SQL files.

I hope you learn a thing or two from this, see you in the next one.

Top comments (0)