Skip to content

Commit

Permalink
feat: add db with migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
antosdaniel committed Mar 12, 2023
1 parent 666f607 commit ce0b360
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
23 changes: 23 additions & 0 deletions build/migrate/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM postgres:15.2-alpine as builder

ENV VERSION v4.15.2
WORKDIR /db

RUN apk --no-cache add curl && \
curl -L https://github.com/golang-migrate/migrate/releases/download/$VERSION/migrate.linux-amd64.tar.gz \
| tar xvz

COPY ./db/migrations ./migrations
COPY ./db/seeds ./seeds
COPY ./db/run-dev-migrations ./run-dev-migrations

CMD ["./run-dev-migrations"]

FROM alpine:3.17

WORKDIR /db

COPY --from=builder /db/migrate ./migrate
COPY ./db/migrations ./migrations

ENTRYPOINT ./migrate -path migrations -database "$DB_URL" up
7 changes: 5 additions & 2 deletions cmd/grpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ func main() {
defer cancel()

shutdown.SetupGraceful(ctx, cancel)
server := grpc.Setup()
defer server.Shutdown(ctx)

log.Println("starting server...")
err := grpc.StartServer()
err := server.ListenAndServe()
if err != nil {
log.Fatalf("unable to start gRPC server: %v", err) //nolint:gocritic
}

<-ctx.Done() // wait for shutdown signal
<-ctx.Done() // Wait for shutdown signal.
}
28 changes: 28 additions & 0 deletions db/migrations/20230312144700_create-payrolls-table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
begin;

create table payrolls
(
id uuid not null,
tenant_id uuid not null,
payday date not null
);
alter table payrolls
add constraint payrolls_pkey primary key (id);

create table payslips
(
id uuid not null,
tenant_id uuid not null,
payroll_id uuid not null,

gross_pay int not null default 0,
tax int not null default 0,
net_pay int not null default 0
);
alter table payslips
add constraint payslis_pkey primary key (id);
alter table payslips
add constraint payslips_payrolls_fkey
foreign key (payroll_id) references payrolls(id);

commit;
8 changes: 8 additions & 0 deletions db/run-dev-migrations
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -e

./migrate -path migrations -database "$DB_URL" up

# Seeds
psql "$DB_URL" -f seeds/payrolls.sql
Empty file added db/seeds/payrolls.sql
Empty file.
32 changes: 32 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,39 @@ services:
build:
dockerfile: ./build/grpc/Dockerfile
target: dev
depends_on:
db:
condition: service_healthy
ports:
- "8000:8000"
volumes:
- ./:/src/

db:
image: "postgres:15.2-alpine"
environment:
POSTGRES_DB: payroll
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret123
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
interval: 3s
timeout: 60s
retries: 10
start_period: 5s
ports:
- "5432:5432"

migrate:
build:
dockerfile: build/migrate/Dockerfile
target: builder
environment:
DB_URL: "postgres://postgres:secret123@db:5432/payroll?sslmode=disable"
depends_on:
db:
condition: service_healthy
volumes:
- ./db/migrations/:/db/migrations/
- ./db/seeds/:/db/seeds/
- ./db/run-dev-migrations:/db/run-dev-migrations
14 changes: 7 additions & 7 deletions internal/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
payrollv1 "github.com/antosdaniel/go-presentation-generate-code/internal/grpc/payroll/v1"
"github.com/antosdaniel/go-presentation-generate-code/internal/grpc/payroll/v1/payrollv1connect"
connect_go "github.com/bufbuild/connect-go"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

var port = getOptionalEnv("PORT", "8000")

const address = "0.0.0.0"

func StartServer() error {
func Setup() *http.Server {
mux := http.NewServeMux()
path, handler := payrollv1connect.NewPayrollServiceHandler(&payrollServiceServer{})
mux.Handle(path, handler)
return http.ListenAndServe( //nolint:gosec
fmt.Sprintf("%s:%s", address, port),
h2c.NewHandler(mux, &http2.Server{}),
)

addr := fmt.Sprintf("%s:%s", address, port)
return &http.Server{ //nolint:gosec
Addr: addr,
Handler: handler,
}
}

type payrollServiceServer struct{}
Expand Down

0 comments on commit ce0b360

Please sign in to comment.