diff --git a/BACKEND_BLUEPRINT.md b/BACKEND_BLUEPRINT.md index 97a21d7..a924bb0 100644 --- a/BACKEND_BLUEPRINT.md +++ b/BACKEND_BLUEPRINT.md @@ -71,6 +71,19 @@ - Keep SQL in context directories (`query.sql`, `models.sql` style) - 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) @@ -80,6 +93,14 @@ - Unit tests for pure service logic (no DB) - 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 - HTTP handler tests with `httptest` diff --git a/cmd/api/main.go b/cmd/api/main.go index 9409b36..8d53afe 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -1,12 +1,31 @@ package main import ( + "context" "log" + "time" "git.kanopo.dev/rhythm/rhythm-backend/internal/config" + "github.com/jackc/pgx/v5/pgxpool" ) func main() { 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") + + } + } diff --git a/go.mod b/go.mod index 6e94ffb..8929227 100644 --- a/go.mod +++ b/go.mod @@ -10,5 +10,7 @@ require ( require ( github.com/jackc/pgpassfile v1.0.0 // 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 ) diff --git a/internal/config/config.go b/internal/config/config.go index e021f64..b87b245 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -21,7 +21,7 @@ func Load() Config { host := getEnv("DB_HOST") // 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{ @@ -35,7 +35,7 @@ func Load() Config { func getEnv(key string) string { v := os.Getenv(key) 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 }