Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6043

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T02:53:08+00:00 2024-11-27T02:53:08+00:00

SQL Migrations with Cobra & Golang Migrate

  • 60k

Hello dear readers, lately I've been struggling to run database migrations in Golang with sqlite3 but I have found a solution and I want to share it with you guys.

Let's do this

Start by creating a simple project Golang project in your computer

  mkdir migration-cli cd migration-cli go mod init migration-cli   
Enter fullscreen mode Exit fullscreen mode

Then get the cobra package

  go get -u github.com/spf13/cobra   
Enter fullscreen mode Exit fullscreen mode

Open this in your favourite code editor and create and add a main.go file

Screenshot 2021-05-04 at 18.11.20

Adding Cobra

We are going to start by creating a package cmd with a root.go file.

There we are going to setup a root command for our cli using cobra of course. The name of this cli will be mcli for example purposes.

Screenshot 2021-05-05 at 09.15.14

SubCommands

Let's create a subcommand named migrate that will have a another up and down subcommand.

migrate.go will be a very simple file, it is like a subroot command that is added to the root command 🙂

Screenshot 2021-05-04 at 18.23.58

Now we can create migrateUp.go and migrateDown.go that are subcommands for migrate. The difference is that up and down will have an extra Run cobra property where we are going to write the actual logic to run our migrations.

Screenshot 2021-05-04 at 18.28.04

Screenshot 2021-05-04 at 18.41.03

To finish setting this up, in our main.go we need to call Execute() from our cmd/root.go file.

To test if the commands are called you can either build your program with a custom name and then call it

  go build -o mcli ./mcli migrate up   
Enter fullscreen mode Exit fullscreen mode

or for now just use go run

  go run main.go migrate up   
Enter fullscreen mode Exit fullscreen mode

Screenshot 2021-05-04 at 18.44.14

Setup Database

I'll be using sqlite3 for the database, I'm gonna start by creating a database package with a config file.

In that config file I'll write a Open method that will return an instance of my database.

For that I'll need the sqlite3 driver package

  go get -u github.com/mattn/go-sqlite3   
Enter fullscreen mode Exit fullscreen mode

Screenshot 2021-05-04 at 18.53.11

Now we can call database.Open() in our migrate subcommands to test the database connection.

Screenshot 2021-05-04 at 18.54.07

  go build -o mcli ./mcli migrate up   
Enter fullscreen mode Exit fullscreen mode

Screenshot 2021-05-04 at 18.55.45

It's working and as you can see it created a myDB file.
I will open this file in DB Browser for Sqlite that is a GUI for SQLITE.

Screenshot 2021-05-04 at 18.58.28

Okay we know how to open our database let's write our migrations.

Migrations

In a new directory migrations I am going to create a 1_install.up.sql file and a 1_install.down.sql, this syntax will allow golang migrate to know the version of our migration file as well as the action.

Screenshot 2021-05-04 at 19.34.24

Screenshot 2021-05-04 at 19.35.11Screenshot 2021-05-04 at 19.35.41

Now let's add golang migrate

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

Back to migrateUp.go we are going to write the logic to run our up migration using golang migrate package.

Let's build the program and run ./mcli migrate up again to test this 🙂

Screenshot 2021-05-04 at 19.24.14

Seems to be working, if we open the myDB file in DB Browser for Sqlite again we should see our tables.

Screenshot 2021-05-04 at 19.37.12

Perfect! Now let's do the same thing for migrate down the logic is basically the same but instead of call the golang migrate built in func Up() we call Down().

Screenshot 2021-05-04 at 19.43.46

Let's test migrate down 👍🏼

Screenshot 2021-05-04 at 19.44.45

Now if you open your myDB file again our tables should be gone.

Screenshot 2021-05-04 at 19.45.42

Conclusion

Okay, that's it! I hope this article will help 😀
The code source is here.
Youtube video here.

See you soon 👋🏼

cligosqlwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.