将golang应用程序部署到Heroku时运行迁移

I've been trying to set up migrations for a golang application on Heroku. I'm using native Go modules (vgo) and the official Heroku build pack. For migrations I'm using the migrate package.

The thing is, I could run migrations when the main function runs, but it feels a bit hacky, this would mean that every time the app restarts it will run the migrations. I would prefer to just run the schema migration when deploying.

I'm not sure how this can be accomplished, when I log in to the instance the go binary is not installed, it's like the build pack only executes the main function.

I could live with doing it with a Heroku CLI command but I can't find how to do this in the docs nor via Google.

All of your dependencies should be defined such that Heroku can install them for you. For Go, Heroku supports godep and govendor. If you're not already using one, pick one and start.

Your dependency file should be committed to your repository. For godep that's Godeps/Godeps.json and for govendor it's vendor/vendor.json. Your dependencies themselves should not be committed.

After you've added a dependency on migrate it should be available on Heroku. You can run migrate up and other commands via heroku run bash. Once you're comfortable running migrations manually you might want to consider adding a release phase command to your Procfile so migrations get applied automatically when you deploy a new version.