Skip to content

feat(db): wire pgx pool with sqlc and Atlas tooling #157

Draft
yiningsoong wants to merge 6 commits into
mainfrom
feat/135-decide-on-tooling-stack-pgx-sqlc-migration-tool
Draft

feat(db): wire pgx pool with sqlc and Atlas tooling #157
yiningsoong wants to merge 6 commits into
mainfrom
feat/135-decide-on-tooling-stack-pgx-sqlc-migration-tool

Conversation

@yiningsoong
Copy link
Copy Markdown
Contributor

🚀 Summary

Wire pgx/v5 into the server as the PostgreSQL driver, with sqlc and Atlas configured for type-safe queries and schema migrations. The pool is initialised in run() at startup,
pinged to fail fast on an unreachable database, and injected into the handler. No queries or schema tables are added yet — this is purely the wiring and tooling foundation.

✏️ Changes

  • Add github.com/jackc/pgx/v5 as a direct Go dependency
  • Add TW_DATABASE_URL config field with startup validation
  • Initialise pgxpool.Pool in run() with a ping check and graceful shutdown on close; inject pool into handler.New
  • Add server/sqlc.yaml — configures sqlc to generate pgx/v5-typed Go code from sql/queries/ into internal/db/
  • Add server/atlas.hcl — configures Atlas versioned migrations against migrations/ with a Docker dev database
  • Add docker-compose.yaml for a local Postgres 17 development instance
  • Add docs/database.md covering setup, migration workflow, and query generation

@yiningsoong yiningsoong force-pushed the feat/135-decide-on-tooling-stack-pgx-sqlc-migration-tool branch from 3d8a427 to c585715 Compare May 6, 2026 02:54
Comment thread docker-compose.yaml
@@ -0,0 +1,14 @@
services:
postgres:
image: postgres:17
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check if AWS supports Postgres version 18? IIRC, v18 has built in UUIDv7 function.

Comment thread server/cmd/tw/main.go

func run(ctx context.Context, cfg *config.Config) error {
h, err := handler.New(cfg)
pool, err := pgxpool.New(ctx, cfg.DatabaseURL)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Since we don't have any queries yet, can we defer the initialisation of pgx until we have our first query?

Comment thread docker-compose.yaml
services:
postgres:
image: postgres:17
environment:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we add container_name: tw-postgres above environment?

Comment thread docker-compose.yaml
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: teacher_workspace
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
POSTGRES_DB: teacher_workspace
POSTGRES_DB: tw OR tw-dev

Comment thread docs/database.md

```bash
# sqlc: generates Go code from SQL queries
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can explore go tool to setup sqlc and atlas. I think we can pin the tools to specific version and contributors no need to manually install tools.

@YimingIsCOLD YimingIsCOLD marked this pull request as draft May 12, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants