Skip to content

Commit

Permalink
Merge pull request #11 from Sean-Miningah/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Sean-Miningah authored Jul 27, 2024
2 parents 92b3c5a + 018fc36 commit 231eb00
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
30 changes: 23 additions & 7 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ on:
pull_request:
branches: [main]

env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: Password123
POSTGRES_DB: crud_db

jobs:
build:
name: tests
Expand All @@ -14,9 +19,11 @@ jobs:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: Password123
POSTGRES_DB: crud_db
POSTGRES_USER: ${{ env.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
POSTGRES_DB: ${{ env.POSTGRES_DB }}
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
Expand All @@ -33,12 +40,21 @@ jobs:

- name: Install dbmate for golang migrations
run: |
curl -L -O https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
sudo mv dbmate-linux-amd64 /usr/bin/dbmate
sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
sudo chmod +x /usr/local/bin/dbmate
which dbmate
- name: MakeMigrations
run: make migrations
- name: Construct DB URL
id: construct_url
run: echo "DB_URL=postgres://${{ env.POSTGRES_USER }}:${{ env.POSTGRES_PASSWORD }}@localhost:5432/${{ env.POSTGRES_DB }}?sslmode=disable" >> $GITHUB_ENV

- run: env

- name: Make Migrations
run: make migrations URL=${{ env.DB_URL }}

- name: Seed test DB
run: go run db/seed.go

- name: Test
run: make test
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
URL ?=

build:
@go build -0 bin cmd/main.go

Expand All @@ -7,6 +9,14 @@ test:
dev-run:
@go run cmd/main.go


migrations:
@dbmate up
@if [ -z "$(URL)" ]; then \
echo "Running migrations without URL"; \
dbmate up; \
else \
echo "Running migrations with URL: $(URL)"; \
dbmate -u "$(URL)" up; \
fi

seed-db:
go run db/seed.go
8 changes: 0 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"flag"
"log"

"finance-crud-app/cmd/api"
Expand All @@ -25,8 +24,6 @@ func NewServer(db *sqlx.DB, mux *mux.Router) *Server {
}

func main() {
seedDB := flag.Bool("seed", false, "seed database with default data")
flag.Parse()

connStr := "postgres://postgres:Password123@localhost:5432/crud_db?sslmode=disable"

Expand All @@ -36,11 +33,6 @@ func main() {
}
defer dbconn.Close()

if *seedDB {
db.SeedTestDB(dbconn)
log.Printf("seed pass")
}

server := api.NewAPIServer(":8085", dbconn)
if err := server.Run(); err != nil {
log.Fatal(err)
Expand Down
20 changes: 20 additions & 0 deletions db/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"finance-crud-app/internal/db"
"log"

_ "github.com/lib/pq"
)

func main() {
connStr := "postgres://postgres:Password123@localhost:5432/crud_db?sslmode=disable"

dbconn, err := db.NewPGStorage(connStr)
if err != nil {
log.Fatal(err)
}
defer dbconn.Close()

db.SeedTestDB(dbconn)
}
2 changes: 1 addition & 1 deletion internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Data struct {
}

func SeedTestDB(db *sqlx.DB) {
seed_data, err := readJsonFile("../test_data/test_seed_data.json")
seed_data, err := readJsonFile("test_data/test_seed_data.json")
if err != nil {
log.Fatalf("error retrieving data from file %v", seed_data)
}
Expand Down

0 comments on commit 231eb00

Please sign in to comment.