Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLc setup #561

Merged
merged 4 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
gen/** linguist-generated=true
gen/**/helpers.go linguist-generated=false
services/impulse_svc/gen/** linguist-generated=true
services/**/schema.sql linguist-generated=true
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ based on the migration files available in the service:
./migrate <service> desired
```

### `devenv shell models` - Generate models from SQL Queries

> [!IMPORTANT]
> Requires a running database, it's best to run `devenv up` before running this script.

We use [sqlc](https://docs.sqlc.dev/en/latest/), a compiler that generates go functions based on SQL queries and a schema.
This script
1. Makes sure the database is fully migrated up (see migration.sh)
2. Generates a `schema.sql` using pg_dump
> [!TIP]
> The `schema.sql` is also a great place to familiarize yourself with the data model(s)!
3. Invokes sqlc to generate go code based on a `sqlc.yaml`

Usage:
- `devenv shell models <some-svc>` generates models for a specific service
- `devenv shell models` generates models for all services

## Enviroment variables
Every service should contain a `.env.example` file which should be copied to a usable `.env` file

Expand Down
1 change: 1 addition & 0 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ in
git
go-migrate
nixpkgs-fmt
sqlc
] ++ [
# self build packages
protoc-go-inject-tag
Expand Down
25 changes: 25 additions & 0 deletions libs/hwdb/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module hwdb

go 1.20

replace hwutil => ../hwutil

require (
github.com/jackc/pgx/v5 v5.5.0
github.com/rs/zerolog v1.31.0
github.com/vgarvardt/pgx-google-uuid/v5 v5.0.0
hwutil v0.0.0
)

require (
github.com/google/uuid v1.4.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
45 changes: 45 additions & 0 deletions libs/hwdb/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw=
github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/vgarvardt/pgx-google-uuid/v5 v5.0.0 h1:kIIQmW04MYKyRE2ZwREPl1NY4/Uxf5x48ABTQ+yFdFo=
github.com/vgarvardt/pgx-google-uuid/v5 v5.0.0/go.mod h1:fskJeXpJTJCU9JvsZQRgR4OhKKpciztvx4rdXWil7E0=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
126 changes: 126 additions & 0 deletions libs/hwdb/pgx_zerolog/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// This is adapted from github.com/jackc/pgx-zerolog,
// and thus is licensed under the MIT LICENSE,
// available in jackc/pgx-zerolog's LICENSE file
package pgx_zerolog

import (
"context"
"github.com/jackc/pgx/v5/tracelog"
"github.com/rs/zerolog"
"math"
)

type Logger struct {
logger zerolog.Logger
withFunc func(context.Context, zerolog.Context) zerolog.Context
fromContext bool
skipModule bool
subDictionary bool
subDictionaryKey string
}

// option options for configuring the logger when creating a new logger.
type option func(logger *Logger)

// WithContextFunc adds possibility to get request scoped values from the
// ctx.Context before logging lines.
func WithContextFunc(withFunc func(context.Context, zerolog.Context) zerolog.Context) option {
return func(logger *Logger) {
logger.withFunc = withFunc
}
}

// WithoutPGXModule disables adding module:pgx to the default logger context.
func WithoutPGXModule() option {
return func(logger *Logger) {
logger.skipModule = true
}
}

// WithSubDictionary adds data to sub dictionary with key.
func WithSubDictionary(key string) option {
return func(logger *Logger) {
logger.subDictionary = true
logger.subDictionaryKey = key
}
}

// NewLogger accepts a zerolog.Logger as input and returns a new custom pgx
// logging facade as output.
func NewLogger(logger zerolog.Logger, options ...option) *Logger {
l := Logger{
logger: logger,
}
l.init(options)
return &l
}

// NewContextLogger creates logger that extracts the zerolog.Logger from the
// context.Context by using `zerolog.Ctx`. The zerolog.DefaultContextLogger will
// be used if no logger is associated with the context.
func NewContextLogger(options ...option) *Logger {
l := Logger{
fromContext: true,
}
l.init(options)
return &l
}

func (pl *Logger) init(options []option) {
for _, opt := range options {
opt(pl)
}
if !pl.skipModule {
pl.logger = pl.logger.With().Str("module", "pgx").Logger()
}
}

func (pl *Logger) Log(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]interface{}) {
var zlevel zerolog.Level
switch level {
case tracelog.LogLevelNone:
zlevel = zerolog.NoLevel
case tracelog.LogLevelError:
zlevel = zerolog.ErrorLevel
case tracelog.LogLevelWarn:
zlevel = zerolog.WarnLevel
case tracelog.LogLevelInfo:
zlevel = zerolog.InfoLevel
case tracelog.LogLevelDebug:
zlevel = zerolog.DebugLevel
case tracelog.LogLevelTrace:
zlevel = zerolog.TraceLevel
default:
zlevel = zerolog.DebugLevel
}

// pgx thinks it's more important than it is
zlevel = zerolog.Level(int(math.Max(float64(zlevel-2), -1)))

var zctx zerolog.Context
if pl.fromContext {
logger := zerolog.Ctx(ctx)
zctx = logger.With()
} else {
zctx = pl.logger.With()
}
if pl.withFunc != nil {
zctx = pl.withFunc(ctx, zctx)
}

pgxlog := zctx.Logger()
event := pgxlog.WithLevel(zlevel)
if event.Enabled() {
if pl.fromContext && !pl.skipModule {
event.Str("module", "pgx")
}

if pl.subDictionary {
event.Dict(pl.subDictionaryKey, zerolog.Dict().Fields(data))
} else {
event.Fields(data)
}

event.Msg(msg)
}
}
102 changes: 102 additions & 0 deletions libs/hwdb/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package hwdb

import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/tracelog"
"github.com/rs/zerolog/log"
"hwdb/pgx_zerolog"
"hwutil"

pgxUUID "github.com/vgarvardt/pgx-google-uuid/v5"
)

// connectionPool is set in OpenDatabase() and allows for concurrent access to the database
var connectionPool *pgxpool.Pool = nil

// SetupDatabaseFromEnv prefers the env POSTGRES_DSN and will be configured the database connection accordingly.
// When this env does not exist, a fallback to the following envs with proper default values will take place.
// [NAME_OF_THE_ENV] (DEFAULT)
// POSTGRES_HOST (localhost)
// POSTGRES_USER (postgres)
// POSTGRES_PASSWORD (postgres)
// POSTGRES_DB (postgres)
// POSTGRES_PORT (5432)
func SetupDatabaseFromEnv(context context.Context) {
log.Info().Msg("connecting to postgres ...")

dsn := hwutil.GetEnvOr("POSTGRES_DSN", "")
if dsn == "" {
dsn = buildDsnFromEnvs()
log.Debug().Msg("POSTGRES_DSN env not found, built dsn from POSTGRES_HOST, ... envs")
}

dbpool, err := openDatabasePool(context, dsn)
if err != nil {
log.Fatal().Err(err).Msg("could not connect to database")
}

// set connectionPool
connectionPool = dbpool

log.Info().Msg("connected to postgres")
}

func buildDsnFromEnvs() string {
host,
user,
password,
databaseName,
port :=
hwutil.GetEnvOr("POSTGRES_HOST", "localhost"),
hwutil.GetEnvOr("POSTGRES_USER", "postgres"),
hwutil.GetEnvOr("POSTGRES_PASSWORD", "postgres"),
hwutil.GetEnvOr("POSTGRES_DB", "postgres"),
hwutil.GetEnvOr("POSTGRES_PORT", "5432")

return fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%s",
host, user, password, databaseName, port,
)
}

// openDatabasePool simply opens a new database pool to the dsn provided
// it does not set up connectionPool!
func openDatabasePool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
pgxConfig, err := pgxpool.ParseConfig(dsn)
if err != nil {
log.Error().Err(err).Msg("Unable to parse database config")
return nil, err
}

// google's uuid <-> pgtype.uuid interop
pgxConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
pgxUUID.Register(conn.TypeMap())
return nil
}

// logging
pgxConfig.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: pgx_zerolog.NewContextLogger(),
// LogLevel is the level at which pgx will start calling zerolog
// Trace, being the lowest level, will cause it to always be called
LogLevel: tracelog.LogLevelTrace,
}

// open pool
dbpool, err := pgxpool.NewWithConfig(ctx, pgxConfig)
if err != nil {
log.Error().Err(err).Msg("Unable to create connection pool")
return nil, err
}
return dbpool, nil
}

func GetDB() *pgxpool.Pool {
if connectionPool == nil {
log.Error().Msg("GetDB called without set-up database, you will run into nil-pointers. Make sure to call SetupDatabaseFromEnv()!")
}
return connectionPool
}
23 changes: 23 additions & 0 deletions nix/devenv/scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ in
sleep 10
fi
'';

models.exec = ''
# No service? Build them all!
if [[ -z $1 ]]; then
for file in services/*-svc; do
devenv shell models $(basename "$file")
MaxSchaefer marked this conversation as resolved.
Show resolved Hide resolved
done
exit 0
fi

# First, make sure the db is migrated
devenv shell migratesh $1 up

if [ $? -ne 0 ]; then
exit 0
fi

# Generate schema.sql
# TODO: use parameters
pg_dump postgres://postgres:postgres@localhost:5432/$1 --schema-only > ./services/$1/schema.sql

cd services/$1 && sqlc generate
'';
};

processes = {
Expand Down
28 changes: 28 additions & 0 deletions services/task-svc/repos/bed_repo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- name: CreateBed :one
INSERT INTO beds (room_id, organization_id, name) VALUES (@room_id, @organization_id, @name) RETURNING *;

-- name: GetBedById :one
SELECT * FROM beds WHERE id = $1 LIMIT 1;

-- name: GetBedsByRoomForOrganization :many
SELECT * FROM beds
WHERE organization_id = $1 AND room_id = $2
ORDER BY name ASC;

-- name: UpdateBed :exec
UPDATE beds
SET
name = coalesce(sqlc.narg('name'), name),
room_id = coalesce(sqlc.narg('room_id'), room_id)
WHERE id = @id;

-- name: DeleteBed :exec
DELETE FROM beds WHERE id = $1;

-- name: ExistsBedInOrganization :one
SELECT EXISTS (
SELECT 1
FROM beds
WHERE id = $1
AND organization_id = $2
) bed_exists;
Loading
Loading