initial pool creation
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 2m11s

This commit is contained in:
Dmitri 2026-04-19 18:32:24 +02:00
parent fb0f858f4b
commit 1ddf0eba4b
Signed by: kanopo
GPG Key ID: 759ADD40E3132AC7
4 changed files with 45 additions and 3 deletions

View File

@ -71,6 +71,19 @@
- Keep SQL in context directories (`query.sql`, `models.sql` style) - Keep SQL in context directories (`query.sql`, `models.sql` style)
- Service layer composes multiple repositories when needed - Service layer composes multiple repositories when needed
### Work in Progress Snapshot
- `sqlc.yaml` is now configured for PostgreSQL with schema from `migrations/*.sql`
- First bounded context added: `internal/db/users`
- Current users SQLC artifacts generated:
- `db.go`, `models.go`, `queries.sql.go`, `querier.go`
- Current SQLC generation options in use:
- `sql_package: pgx/v5`
- `emit_interface: true` (generated `Querier` interface)
- `emit_json_tags: false` (can be revisited if API structs are returned directly)
- Initial queries implemented for users: `GetUser`, `CreateUser`, `DeleteUser`
- DB pool wiring into `cmd/api` and service construction is planned next
--- ---
## Testing Approach (Beginner-Friendly) ## Testing Approach (Beginner-Friendly)
@ -80,6 +93,14 @@
- Unit tests for pure service logic (no DB) - Unit tests for pure service logic (no DB)
- Integration tests for SQLC repositories with real Postgres via Docker - Integration tests for SQLC repositories with real Postgres via Docker
### DB Interface Testing (via SQLC `Querier`)
- Treat generated SQLC interfaces (e.g. `usersdb.Querier`) as service dependencies
- For service unit tests, provide a fake/mock implementation of `Querier`
- Focus unit tests on business rules, branching, and error mapping (not SQL behavior)
- Keep SQL correctness in integration tests against real Postgres
- This split gives fast unit tests plus high-confidence DB integration coverage
### Phase 2 ### Phase 2
- HTTP handler tests with `httptest` - HTTP handler tests with `httptest`

View File

@ -1,12 +1,31 @@
package main package main
import ( import (
"context"
"log" "log"
"time"
"git.kanopo.dev/rhythm/rhythm-backend/internal/config" "git.kanopo.dev/rhythm/rhythm-backend/internal/config"
"github.com/jackc/pgx/v5/pgxpool"
) )
func main() { func main() {
cfg := config.Load() cfg := config.Load()
log.Println(cfg.DbUrl) ctx := context.Background()
pool, err := pgxpool.New(ctx, cfg.DbUrl)
if err != nil {
log.Fatalf("Error creatin the db pool:%v\n", err.Error())
}
defer pool.Close()
{
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
if err := pool.Ping(ctx); err != nil {
log.Fatalf("ping to db failed %v", err.Error())
}
log.Printf("successfully connected to database")
}
} }

2
go.mod
View File

@ -10,5 +10,7 @@ require (
require ( require (
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/text v0.29.0 // indirect golang.org/x/text v0.29.0 // indirect
) )

View File

@ -21,7 +21,7 @@ func Load() Config {
host := getEnv("DB_HOST") host := getEnv("DB_HOST")
// postgres://admin:admin@localhost:5432/admin_db // postgres://admin:admin@localhost:5432/admin_db
dbUrl = fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disabled", username, password, host, port, name) dbUrl = fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable", username, password, host, port, name)
} }
cfg := Config{ cfg := Config{
@ -35,7 +35,7 @@ func Load() Config {
func getEnv(key string) string { func getEnv(key string) string {
v := os.Getenv(key) v := os.Getenv(key)
if v == "" { if v == "" {
log.Fatalln("The env variable %v is not defined and the applciation can not operate without", key) log.Fatalf("The env variable %v is not defined and the applciation can not operate without\n", key)
} }
return v return v
} }