Skip to content

Commit

Permalink
feat(main): Reworked database instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed May 17, 2024
1 parent 246a067 commit 635429d
Show file tree
Hide file tree
Showing 33 changed files with 753 additions and 984 deletions.
8 changes: 8 additions & 0 deletions http-demo/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.PHONY: up down fresh logs test lint migrate sqlc

up:
@if [ ! -f .env ]; then \
cp .env.example .env; \
Expand All @@ -23,3 +25,9 @@ test:

lint:
golangci-lint run -v

sqlc:
docker run --rm -u $$(id -u):$$(id -g) -v ./:/src -w /src sqlc/sqlc $(filter-out $@,$(MAKECMDGOALS))

%:
@:
3 changes: 0 additions & 3 deletions http-demo/configs/config.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ app:
modules:
log:
output: console
orm:
driver: mysql
dsn: ${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}?parseTime=true
trace:
processor:
type: otlp-grpc
Expand Down
9 changes: 8 additions & 1 deletion http-demo/configs/config.test.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
app:
debug: true
modules:
orm:
sql:
driver: sqlite
dsn: ":memory:"
migrations:
stdout: false
log:
output: test
trace:
Expand All @@ -15,3 +17,8 @@ config:
secret: ${AUTH_SECRET}
dashboard:
title: Test dashboard title
seed:
gophers:
alice: architect
bob: builder
carl: carpenter
18 changes: 14 additions & 4 deletions http-demo/configs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,24 @@ modules:
templates:
enabled: true
path: templates/*.html
orm:
sql:
driver: mysql
dsn: ${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}?parseTime=true
migrations:
path: db/migrations
log:
enabled: true
level: info
values: true
level: debug
arguments: true
exclude:
- "connection:ping"
- "connection:reset-session"
trace:
enabled: true
values: true
arguments: true
exclude:
- "connection:ping"
- "connection:reset-session"
log:
level: debug
config:
Expand Down
9 changes: 9 additions & 0 deletions http-demo/db/migrations/00001_create_gophers_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +goose Up
CREATE TABLE gophers (
id INTEGER NOT NULL PRIMARY KEY /*!40101 AUTO_INCREMENT */,
name VARCHAR(255) NOT NULL,
job VARCHAR(255)
);

-- +goose Down
DROP TABLE IF EXISTS gophers;
19 changes: 19 additions & 0 deletions http-demo/db/queries/gophers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- name: ListGophers :many
SELECT g.* FROM gophers AS g
ORDER BY g.name;

-- name: GetGopher :one
SELECT g.* FROM gophers AS g
WHERE g.id = ?
LIMIT 1;

-- name: CreateGopher :execresult
INSERT INTO gophers (
name, job
) VALUES (
?, ?
);

-- name: DeleteGopher :exec
DELETE FROM gophers
WHERE id = ?;
33 changes: 33 additions & 0 deletions http-demo/db/seeds/gophers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seeds

import (
"context"
"database/sql"

"github.com/ankorstore/yokai/config"
)

type GophersSeed struct {
config *config.Config
}

func NewGophersSeed(config *config.Config) *GophersSeed {
return &GophersSeed{
config: config,
}
}

func (s *GophersSeed) Name() string {
return "gophers"
}

func (s *GophersSeed) Run(ctx context.Context, db *sql.DB) error {
for name, job := range s.config.GetStringMapString("config.seed.gophers") {
_, err := db.ExecContext(ctx, "INSERT INTO gophers (name, job) VALUES (?, ?)", name, job)
if err != nil {
return err
}
}

return nil
}
118 changes: 118 additions & 0 deletions http-demo/db/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions http-demo/db/sqlc/gophers.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions http-demo/db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions http-demo/db/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions http-demo/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
build:
dockerfile: dev.Dockerfile
context: .
depends_on:
- http-demo-database
networks:
- http-demo
ports:
Expand All @@ -25,6 +27,10 @@ services:
restart: always
networks:
- http-demo
ports:
- "3306:3306"
expose:
- "3306"
volumes:
- http-demo-database-data:/var/lib/mysql
env_file:
Expand Down
Loading

0 comments on commit 635429d

Please sign in to comment.