This document provides essential information for working with the sqlc codebase, including testing, development workflow, and code structure.
- Go 1.25.0+ - Required for building and testing
- Docker & Docker Compose - Required for integration tests with databases
- Git - For version control
# Simplest approach - runs all unit tests
go test ./...
# Using make
make test# Step 1: Start database containers
docker compose up -d
# Step 2: Run all tests including examples
go test --tags=examples -timeout 20m ./...
# Or use make for the full CI suite
make test-ci# Test a specific package
go test ./internal/config
go test ./internal/compiler
# Run with verbose output
go test -v ./internal/config
# Run a specific test function
go test -v ./internal/config -run TestConfig
# Run with race detector (recommended for concurrency changes)
go test -race ./internal/config- Location: Throughout the codebase as
*_test.gofiles - Run without: Database or external dependencies
- Examples:
/internal/config/config_test.go- Configuration parsing/internal/compiler/selector_test.go- Compiler logic/internal/metadata/metadata_test.go- Query metadata parsing
- Location:
/internal/endtoend/ - Requirements:
--tags=examplesflag and running databases - Tests:
TestExamples- Main end-to-end testsTestReplay- Replay testsTestFormat- Code formatting testsTestJsonSchema- JSON schema validationTestExamplesVet- Static analysis tests
- Location:
/examples/directory - Requirements: Tagged with "examples", requires live databases
- Databases: PostgreSQL, MySQL, SQLite examples
The docker-compose.yml provides test databases:
-
PostgreSQL 16 - Port 5432
- User:
postgres - Password:
mysecretpassword - Database:
postgres
- User:
-
MySQL 9 - Port 3306
- User:
root - Password:
mysecretpassword - Database:
dinotest
- User:
# Start databases
make start
# or
docker compose up -d
# Stop databases
docker compose down
# View logs
docker compose logs -fmake test # Basic unit tests only
make test-examples # Tests with examples tag
make build-endtoend # Build end-to-end test data
make test-ci # Full CI suite (examples + endtoend + vet)
make vet # Run go vet
make start # Start database containers- File:
.github/workflows/ci.yml - Go Version: 1.25.0
- Test Command:
gotestsum --junitfile junit.xml -- --tags=examples -timeout 20m ./... - Additional Checks:
govulncheckfor vulnerability scanning
# Install CI tools (optional)
go install gotest.tools/gotestsum@latest
# Run tests with same timeout as CI
go test --tags=examples -timeout 20m ./...
# Or use the CI make target
make test-ci# Build main sqlc binary for development
go build -o ~/go/bin/sqlc-dev ./cmd/sqlc
# Build JSON plugin (required for some tests)
go build -o ~/go/bin/sqlc-gen-json ./cmd/sqlc-gen-jsonYou can customize database connections:
PostgreSQL:
PG_HOST=127.0.0.1
PG_PORT=5432
PG_USER=postgres
PG_PASSWORD=mysecretpassword
PG_DATABASE=dinotestMySQL:
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_ROOT_PASSWORD=mysecretpassword
MYSQL_DATABASE=dinotestExample:
POSTGRESQL_SERVER_URI="postgres://postgres:mysecretpassword@localhost:5432/postgres" \
go test -v ./.../cmd/- Main binaries (sqlc, sqlc-gen-json)/internal/cmd/- Command implementations (vet, generate, etc.)/internal/engine/- Database engine implementations/postgresql/- PostgreSQL parser and converter/dolphin/- MySQL parser (uses TiDB parser)/sqlite/- SQLite parser
/internal/compiler/- Query compilation logic/internal/codegen/- Code generation for different languages/internal/config/- Configuration file parsing/internal/endtoend/- End-to-end tests/examples/- Example projects for testing
/Makefile- Build and test targets/docker-compose.yml- Database services for testing/.github/workflows/ci.yml- CI configuration/docs/guides/development.md- Developer documentation
If you see errors about storage.googleapis.com, the Go proxy may be unreachable. Tests may still pass for packages that don't require network dependencies.
End-to-end tests can take a while. Use longer timeouts:
go test -timeout 20m --tags=examples ./...Always run tests with the race detector when working on concurrent code:
go test -race ./...Ensure Docker containers are running:
docker compose ps
docker compose up -d- Run tests before committing:
make test-ci - Check for race conditions: Use
-raceflag when testing concurrent code - Use specific package tests: Faster iteration during development
- Start databases early:
docker compose up -dbefore running integration tests - Read existing tests: Good examples in
/internal/engine/postgresql/*_test.go
- Feature branches should start with
claude/for Claude Code work - Branch names should be descriptive and end with the session ID
# Stage changes
git add <files>
# Commit with descriptive message
git commit -m "Brief description
Detailed explanation of changes.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>"
# Push to remote
git push -u origin <branch-name># Update main
git checkout main
git pull origin main
# Rebase feature branch
git checkout <feature-branch>
git rebase main
# Force push rebased branch
git push --force-with-lease origin <feature-branch>- Main Documentation:
/docs/ - Development Guide:
/docs/guides/development.md - CI Configuration:
/.github/workflows/ci.yml - Docker Compose:
/docker-compose.yml
- Typo in create_function_stmt.go - Fixed "Undertand" → "Understand"
- Race condition in vet.go - Fixed Client initialization using
sync.Once - Nil pointer dereference in parse.go - Fixed unsafe type assertion in primary key parsing
These fixes demonstrate common patterns:
- Using
sync.Oncefor thread-safe lazy initialization - Using comma-ok idiom for safe type assertions:
if val, ok := x.(Type); ok { ... } - Adding proper nil checks and defensive programming
Last Updated: 2025-10-21 Maintainer: Claude Code