This article is to enable auto migration in GoLang with integration with GORM in up-down approach.

Instructions

To enable auto migration in Go with GORM install golang-migrate.

  1. At first, create migration CLI

    $ go get -u -d github.com/golang-migrate/migrate/cmd/migrate
    $ cd $GOPATH/src/github.com/golang-migrate/migrate/cmd/migrate
    $ git checkout $TAG  # e.g. v4.8.0 
    $ go build -tags 'postgres' -ldflags="-X main.Version=$(git describe --tags)
  2. Navigate to your migrate project and find out migrate.exe file. 

  3. Copy migrate.exe.

  4. Paste it into $GOPATH/bin folder.

  5. Your migration CLI is ready to use. N.B: [find out the latest version of migrate cmd and replace it]

  6. Read more about CLI.

Run your first migration. 

i.e:  migrate create -ext sql -dir db/migrations -digits 7 create_users_table 

  1. migrate command will create a db/migrations folder with to sql (up/down) file.

  2. Edit up file. for example-

  3. Edit down file. for example-

  4. Now let install golang-migrate.

    1. $ go get github.com/golang-migrate/migrate

  5. Configure migration with GORM.

  6. Use command line to run migration

  7. Happy Coding…(smile)

 

Highlight important information in a panel like this one. To edit this panel's color or style, select one of the options in the menu below.

Related articles

The content by label feature displays related articles automatically, based on labels you choose. To edit options for this feature, select the placeholder below and tap the pencil icon.

Related issues