Skip to content

Commit

Permalink
Trigger SQL migration from cmd/migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
abeltay committed Dec 31, 2022
1 parent 5e80db4 commit 79372f9
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 20 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ jobs:
with:
go-version: 1.19

- name: Install sql-migrate
run: make install

- name: Migrate PostgreSQL
run: make migrate

Expand Down
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: up down pull update clean install migrate test generate-sqlboiler generate-mocks psql run
.PHONY: up down pull update clean migrate test generate-sqlboiler generate-mocks psql run

up:
docker compose up --detach postgres
Expand All @@ -15,11 +15,8 @@ update: pull up
clean:
docker compose down --remove-orphans --volumes

install:
go install github.com/rubenv/sql-migrate/[email protected]

migrate:
sql-migrate up
go run ./cmd/migrate

test:
go test -cover ./...
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

1. Install [Go](https://go.dev/)
1. Install [Docker](https://www.docker.com/)
1. `make install`
1. `make up`
1. `make migrate`

Expand Down
31 changes: 31 additions & 0 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"log"

"github.com/abeltay/go-template/env"
"github.com/abeltay/go-template/postgres"
migrate "github.com/rubenv/sql-migrate"
)

func main() {
options, err := env.LoadOSEnv()
if err != nil {
log.Fatalln("Error loading environment settings, exiting the program: ", err)
}
db, err := postgres.OpenDB(options)
if err != nil {
log.Fatalln("Database error: ", err)
}
defer db.Close()

migrations := &migrate.FileMigrationSource{
Dir: "db/migrations",
}
n, err := migrate.Exec(db, "postgres", migrations, migrate.Up)
if err != nil {
log.Fatalln("Migrate error: ", err)
}
fmt.Printf("Applied %d migrations!\n", n)
}
9 changes: 0 additions & 9 deletions dbconfig.yml

This file was deleted.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ require (
github.com/go-chi/chi/v5 v5.0.7
github.com/google/uuid v1.3.0
github.com/jackc/pgx/v4 v4.17.1
github.com/rubenv/sql-migrate v1.1.2
github.com/stretchr/testify v1.8.0
github.com/volatiletech/sqlboiler/v4 v4.12.0
go.uber.org/zap v1.23.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-gorp/gorp/v3 v3.0.2 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
Expand Down
59 changes: 57 additions & 2 deletions go.sum

Large diffs are not rendered by default.

0 comments on commit 79372f9

Please sign in to comment.