Open In App

How to Install Golang Migrate on Windows?

Last Updated : 31 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Golang (also called Go language) is an open-source procedural language. It is designed by Robert Griesemer, Rob Pike, and Ken Thompson at Google. It was developed in 2007 and publicly announced in 2009. Golang is widely used in Google and many other open-source projects. Have a look at this article for more information about Golang Programming Language.

Golang Migrate

Golang Migrate is a Golang package that is used for handling database migrations. Golang database migrations are imported as libraries or used as CLI. Golang migrates reads migrations from the sources like (Filesystem, io/fs, GitHub, Google cloud storage, etc…) and applies them to the databases like (PGX, Redshift, MongoDB, MySQL, etc…). Databases don’t have self-thinking capacities as humans. If it’s incomplete or incorrect, it fails.

File Name Format

A migration is represented in 2 separate migration files, one is migrate up file, and another is migrate down file from sources like (Filesystem, io/fs, GitHub, etc…). Migrate up file migrate “up” from the previous version to the specified version.

{version}_{filename}.up.{extension}

Migrate down the file to migrate back “down” to the previous version.

{version}_{filename}.down.{extension}

The title of each migration is only for readability. The library doesn’t check the extension of the migration files. Use the appropriate format for the database (ex: .mongodb for MongoDB). Golang migrate CLI is a simple wrapper around the library; it doesn’t have any config search paths, config files, or magic ENV var injections, and it also has one of the best features ctrl + c (SIGINT). It is handled very efficiently.

Example: 

$ migrate -source file://path/to/migrations -database postgres://localhost:8000/database up 5

If you want to run the first 5 migrations of your database and want to stop in the middle, send ctrl + c (SIGINT) the CLI will gracefully stop at a safe point. 
Send SIGKILL for an immediate halt.

Installing Golang Migrate

Let’s install Golang Migrate now, but before that, make sure that you have GO installed on your PC. Check if you had GO installed on your PC. Run the following command in cmd to check that:

$ go version

GO-version-1.19-installed.

 

If that shows an error or the go command is not recognized as internal or external, it means you don’t have GO installed on your PC. Also, if your version of GO is less than 1.15, follow this GFG article and install the latest version of GO: Install GO. Let’s start the process of installing GOLANG MIGRATE now,

Method 1: (Using Scoop)

Scoop is A command-line installer for Windows; this makes the installation of programs easy and fast. If you had installed it early, then jump to step 2. Else, don’t worry, it’s just 1 step process,

Step 1: Install Scoop: Open the Windows power shell on your PC, and run the following command to install SCOOP.

$ irm get.scoop.sh | iex

Scoop-installed

 

This shows that Scoop is installed successfully on your PC.

Step 2: Install Migrate: Open the Windows power shell on your PC, and run the following command to install MIGRATE.

$ scoop install migrate

Successfully-Installed-Migrate-4.15.2

 

Now you are all set to use MIGRATE for your database migrations.

Method 2: (Using Go Toolchain)

If you have git installed already on your PC, then proceed to Step 2. Else, follow the process in Step 1.

Step 1: Install GIT: If you don’t have git installed but Scoop installed on your PC, proceed to Step 1.2 or else follow Step 1.1.

  • Install Scoop first following Method 1, Step 1.
  • Open Windows PowerShell, using the following command to install git.

$ scoop install git

Installed-git-successfully.

 

Step 2: Open Windows Powershell and run the following commands,

$ go get -u -d github.com/golang-migrate/migrate/cmd/migrate

While running the below command, go through your PC, find the path where it is located and run the command. It may be in a different drive or directory, so carefully check all possible directories with names related to GO. You can see the example from my commands. Use the ls command wherever it is necessary.

$ cd src/github.com/golang-migrate/migrate/cmd/migrate

Running-command-in-windows-Powershell

 

 Step 3: After running the above commands, run the following commands,

$ git checkout v4.15.2

We are using v4.15.2 here because it is the latest version.

$ go install

Check by running,

$ migrate -version

It should give output as v4.15.2.

Migrate-installed

 

Now you are all set to use MIGRATE for your database migrations.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads