Guide to running db migrations (entrypoint: cmd/migrate/).
Follow the instructions to install the migrate CLI here.
https://pkg.go.dev/github.com/golang-migrate/migrate/v4#section-readme
import (
"database/sql"
_ "github.com/lib/pq"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
func main() {
db, err := sql.Open("postgres", "postgres://localhost:5432/database?sslmode=enable")
driver, err := postgres.WithInstance(db, &postgres.Config{})
m, err := migrate.NewWithDatabaseInstance(
"file:///migrations",
"postgres", driver)
m.Up() // or m.Step(2) if you want to explicitly set the number of migrations to run
}make migration create-user-table(Scaffolds a .sql migration file)make migrate-upmake migrate-down
You can run migrations for development vs. production by specifying .env.dev vs .env.prod connection info. Specifying the APP_ENV in the .env file will tell the migrate command which of the two environments to run from.
Had to use the pg database driver for migrations specifically due to a legacy issue where migrate hasn't been able to fully support pgx driver yet. pg is no longer actively maintained and the creators have explicitly said to go use pgx instead, which is why I'm using pgx in the actual api implementation. pg will only be used temporarily here for migrates only.