Run migrations when deploying golang application to Heroku - go

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.

Related

Create Heroku pipelines with different build command

I have a repo with 2 different build/start commands: one for the app itself, and another to run Storybook. Yes, I know Storybook should be on its own repo but for now we have everything in a single repo.
How can I build 2 pipelines, one that starts with npm build and npm start, and another pipeline that uses npm run build-storybook and npm run storybook?
I can't use a Procfile since they are both on the main branch.
I was working on this exact issue. Unfortunately I was unable to find a solution because Heroku automatically runs the build command on deployment.
I pivoted to using Chromatic because it has built in Storybook deployments.

How to run Go migrations on heroku on release?

I'm writing a webapp in Go which uses Postgres for data storage and deploy in on Heroku. How can I run migrations automatically?
I use Go 1.13, for dependency management I want to use Go Modules.
As a migration tool I tried this https://github.com/golang-migrate/migrate.
Locally I just downloaded latest binary from github releases and run CLI utility ./migrate -database $DATABASE_URL -path migrations up.
Heroku Procfile content
release: migrate -database $DATABASE_URL -path migrations up
web: bin/myawesomegoapp
Of course, when I launch git push heroku master I get an error, that "migrate" no such file or directory, release command failed and push rejected.
So, how can I set up project to install migrate command to be able to run it on heroku on every release?
You have to tell Heroku to build it, and it's a bit of a pain. Here's how you make it happen using go modules.
First you have to tell go modules to include it even though it's not referenced in the source. This is discussed here. This is my tools package which lives in tools.go in the root of my project:
// +build tools
package tools
// See this https://github.com/go-modules-by-example/index/blob/master/010_tools/README.md
import (
_ "github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/cmd/migrate"
)
I then vendored my dependencies.
go mod tidy
go mod vendor
Finally, you have to tell Heroku to build the binary for migrate which is not documented very thoroughly. For this you not only need to specify the path where migrate lives, you need to include a "." which tells heroku you also want to look for main packages in the working directory. This is because (I'm guessing) using that directive overrides their default package search behavior. This is what my modules file looks like:
module ...
// +heroku goVersion 1.14
// +heroku install -tags 'postgres' ./vendor/github.com/golang-migrate/migrate/v4/cmd/migrate .
go 1.14
require (
...

Ext-Tidy on Heroku

I'm trying to get the PHP Tidy extension to run on my Heroku instance that I've set up. I've added ext-tidy to my composer.json file and run composer update to update the composer.lock file.
However when I push the changes to Heroku, the build fails as the Heroku buildpack doesn't include the Tidy extension.
Is there a way to get Composer to bundle the Tidy extension so that it gets pushed to Heroku (I'm using a Windows machine so I don't know if this will complicate matters if it is possible)? Or do I need to create a new buildpack with Tidy integrated? If so, I'd appreciate it if someone could point me in the right direction to be able to do this.
Thanks.

How do I build utilities with my Go web app on Heroku?

I've developed a web app using Go, which I've deployed to Heroku. I'm using mattes/migrate to manage migrations. It works great locally, but the migrate command-line binary isn't available when I deploy to Heroku.
The only binaries that are included are my own. Is there a way to have Godeps compile and install binaries provided by a dependency?
Just create a file where you include the executable path.
See this issue for Goose (mattes/migrate competitor)
I'd expect the equivalent for mattes/migrate would be:
package main
import _ "github.com/mattes/migrate"
Heroku's current recommended solution is to simply clone the command into your own repo (see github.com/tools/godep/issues/306).
I copied the mattes/migrate/main.go into the cmd/migrate directory in my own project. This builds the command just like my own server command.
It's not ideal, but it works.

Should I use Heroku git for source control in addition to production code?

I have created one application using NodeJS, Angular and Express which I want to run at Heroku. Now, Im using Grunt to build the code that are placed in the dist folder and is ready to be deployed and run on Heroku. This would be done by pushing the dist folder in the Heroku git repo.
Now, should i push my source code in Heroku git as well?
If so, how should I seperate it from dist-folder repository? For instance, I dont want Heroku to run npm install each time i push changes to remote repo. And dist folder should not be part of the source code folder in the repository since it is auto generated.
Using a git repository is the only way to push changes to heroku. So yes it is mandatory. Having said that here is what they have to say about it.
Heroku provides the git service primarily for deployment, and the ability to clone from it is offered as a convenience. We strongly recommend you store your code in another git repository such as GitHub and treat that as canonical.
Again there is no way to stop them from doing an npm install on each push. Here is a quote from their getting started guide
Heroku recognizes an app as Node.js by the existence of a package.json. Even if your app has no dependencies, you should still create a package.json that declares a name, version, and empty dependencies in order that it appear as a Node app.
But I suppose that you could download all the dependencies of your app locally, not specify in package.json, push it along with rest of your application and you might trick heroku into thinking that there are no dependencies. Have not tried it myself though.
If you don't want dist folder to be a part of push simply gitignore it.

Resources