### Run Full Test Suite without Docker Source: https://github.com/sqlc-dev/sqlc/blob/main/CLAUDE.md Install and start the test setup environment, then run the full test suite remotely or in a CI environment. Requires the 'examples' tag. ```bash go run ./cmd/sqlc-test-setup install go run ./cmd/sqlc-test-setup start go test --tags=examples -timeout 20m ./... ``` -------------------------------- ### Install MySQL Source: https://github.com/sqlc-dev/sqlc/blob/main/internal/endtoend/CLAUDE.md Installs MySQL server, pre-configuring the root password. Starts the service and verifies the connection. ```bash # Pre-configure MySQL root password echo "mysql-server mysql-server/root_password password mysecretpassword" | sudo debconf-set-selections echo "mysql-server mysql-server/root_password_again password mysecretpassword" | sudo debconf-set-selections # Install MySQL sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server # Start the service sudo service mysql start # Verify connection mysql -uroot -pmysecretpassword -e "SELECT 1;" ``` -------------------------------- ### Run Full Test Suite with Docker Source: https://github.com/sqlc-dev/sqlc/blob/main/CLAUDE.md Start the Docker Compose environment and then run the full test suite, including end-to-end and example tests. Requires the 'examples' tag. ```bash docker compose up -d go test --tags=examples -timeout 20m ./... ``` -------------------------------- ### Run Tests with Examples Tag Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/development.md Run tests specifically in the examples folder by using the `examples` tag. This requires database instances to be running. ```bash go test --tags=examples ./... ``` -------------------------------- ### Install sqlc using Go Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/overview/install.md Install the latest version of sqlc using 'go install'. Requires Go 1.21+. ```bash go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest ``` -------------------------------- ### Install sqlc on Ubuntu using Snap Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/overview/install.md Use Snap to install sqlc on Ubuntu systems. ```bash sudo snap install sqlc ``` -------------------------------- ### dbmate Migration Example Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ddl.md Example of dbmate up and down migration statements for a 'foo' table. ```sql -- migrate:up CREATE TABLE foo (bar INT NOT NULL); -- migrate:down DROP TABLE foo; ``` -------------------------------- ### Golang-migrate Migration Example (Up) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ddl.md Example of an up migration SQL statement for the 'post' table using golang-migrate format. ```sql CREATE TABLE post ( id int NOT NULL, title text, body text, PRIMARY KEY(id) ); ``` -------------------------------- ### Atlas Migration Example Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ddl.md Example of a CREATE TABLE statement for the 'post' table, compatible with Atlas migrations. ```sql -- Create "post" table CREATE TABLE "public"."post" ("id" integer NOT NULL, "title" text NULL, "body" text NULL, PRIMARY KEY ("id")); ``` -------------------------------- ### Install Databases for sqlc Tests Source: https://github.com/sqlc-dev/sqlc/blob/main/CLAUDE.md Installs PostgreSQL and MySQL using the sqlc-test-setup tool. This command is idempotent and handles dependency resolution. ```bash go run ./cmd/sqlc-test-setup install ``` -------------------------------- ### Start Databases for sqlc Tests Source: https://github.com/sqlc-dev/sqlc/blob/main/CLAUDE.md Starts PostgreSQL and MySQL services, configuring them for testing. This command is idempotent and verifies existing configurations. ```bash go run ./cmd/sqlc-test-setup start ``` -------------------------------- ### Install MySQL Driver and Build Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-mysql.md Fetches the necessary MySQL driver for Go and builds the project. Ensure your database connection parameters in `sql.Open` are correctly configured. ```shell go get github.com/go-sql-driver/mysql go build ./... ``` -------------------------------- ### sql-migrate Migration Example (Up and Down) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ddl.md Example of sql-migrate up and down migration statements for the 'people' table. ```sql -- +migrate Up -- SQL in section 'Up' is executed when this migration is applied CREATE TABLE people (id int); -- +migrate Down -- SQL section 'Down' is executed when this migration is rolled back DROP TABLE people; ``` -------------------------------- ### Golang-migrate Migration Example (Down) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ddl.md Example of a down migration SQL statement for the 'post' table using golang-migrate format. ```sql DROP TABLE post; ``` -------------------------------- ### Install sqlc on macOS using Homebrew Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/overview/install.md Use Homebrew to install the latest version of sqlc on macOS. ```bash brew install sqlc ``` -------------------------------- ### Install PostgreSQL Source: https://github.com/sqlc-dev/sqlc/blob/main/internal/endtoend/CLAUDE.md Installs PostgreSQL, sets the 'postgres' user password, and configures pg_hba.conf for password authentication. Reloads the service after configuration. ```bash # Install PostgreSQL sudo DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql postgresql-contrib # Start the service sudo service postgresql start # Set password for postgres user sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';" # Configure pg_hba.conf for password authentication # Find the hba_file location: sudo -u postgres psql -t -c "SHOW hba_file;" # Add md5 authentication for localhost (add to the beginning of pg_hba.conf): # host all all 127.0.0.1/32 md5 # Reload PostgreSQL sudo service postgresql reload ``` -------------------------------- ### Example SQL queries for Go and pgx Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/using-go-and-pgx.md Define your SQL queries and schema in `query.sql` for code generation. ```sql CREATE TABLE authors ( id BIGSERIAL PRIMARY KEY, name text NOT NULL, bio text ); -- name: GetAuthor :one SELECT * FROM authors WHERE id = $1 LIMIT 1; -- name: ListAuthors :many SELECT * FROM authors ORDER BY name; -- name: CreateAuthor :one INSERT INTO authors ( name, bio ) VALUES ( $1, $2 ) RETURNING *; -- name: DeleteAuthor :exec DELETE FROM authors WHERE id = $1; ``` -------------------------------- ### Example SQLC Trace Log Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/environment-variables.md An example log showing execution times for different tasks and packages during a sqlc run. ```text 0.000043897 . 1 task sqlc (id 1, parent 0) created 0.000144923 . 101026 1 region generate started (duration: 47.619781ms) 0.001048975 . 904052 1 region package started (duration: 14.588456ms) 0.001054616 . 5641 1 name=authors dir=/Users/kyle/projects/sqlc/examples/python language=python 0.001071257 . 16641 1 region parse started (duration: 7.966549ms) 0.009043960 . 7972703 1 region codegen started (duration: 6.587086ms) 0.009171704 . 127744 1 new goroutine 35: text/template/parse.lex·dwrap·1 0.010361654 . 1189950 1 new goroutine 36: text/template/parse.lex·dwrap·1 0.015641815 . 5280161 1 region package started (duration: 10.904938ms) 0.015644943 . 3128 1 name=booktest dir=/Users/kyle/projects/sqlc/examples/python language=python 0.015647431 . 2488 1 region parse started (duration: 4.207749ms) 0.019860308 . 4212877 1 region codegen started (duration: 6.681624ms) 0.020028488 . 168180 1 new goroutine 37: text/template/parse.lex·dwrap·1 0.021020310 . 991822 1 new goroutine 8: text/template/parse.lex·dwrap·1 0.026551163 . 5530853 1 region package started (duration: 9.217294ms) 0.026554368 . 3205 1 name=jets dir=/Users/kyle/projects/sqlc/examples/python language=python 0.026556804 . 2436 1 region parse started (duration: 3.491005ms) 0.030051911 . 3495107 1 region codegen started (duration: 5.711931ms) 0.030213937 . 162026 1 new goroutine 20: text/template/parse.lex·dwrap·1 0.031099938 . 886001 1 new goroutine 38: text/template/parse.lex·dwrap·1 0.035772637 . 4672699 1 region package started (duration: 10.267039ms) 0.035775688 . 3051 1 name=ondeck dir=/Users/kyle/projects/sqlc/examples/python language=python 0.035778150 . 2462 1 region parse started (duration: 4.094518ms) 0.039877181 . 4099031 1 region codegen started (duration: 6.156341ms) 0.040010771 . 133590 1 new goroutine 39: text/template/parse.lex·dwrap·1 0.040894567 . 883796 1 new goroutine 40: text/template/parse.lex·dwrap·1 0.046042779 . 5148212 1 region writefiles started (duration: 1.718259ms) 0.047767781 . 1725002 1 task end ``` -------------------------------- ### Migrate Package Configuration (JSON - Before) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/migrating-to-sqlc-gen-python.md Example of a package configuration using the deprecated built-in Python support in sqlc.json. ```json "sql": [ { "schema": "schema.sql", "queries": "query.sql", "engine": "postgresql", "gen": { "python": { "out": "src", "package": "foo", "emit_sync_querier": true, "emit_async_querier": true, "query_parameter_limit": 5 } } } ] ``` -------------------------------- ### Tern Migration Example Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ddl.md Example of Tern migration statements, including create and drop for the 'comment' table. ```sql CREATE TABLE comment (id int NOT NULL, text text NOT NULL); ---- create above / drop below ---- DROP TABLE comment; ``` -------------------------------- ### Goose Migration Example (Up and Down) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ddl.md Example of Goose up and down migration statements for the 'post' table. ```sql -- +goose Up CREATE TABLE post ( id int NOT NULL, title text, body text, PRIMARY KEY(id) ); -- +goose Down DROP TABLE post; ``` -------------------------------- ### Migrate Package Configuration (YAML - Before) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/migrating-to-sqlc-gen-python.md Example of a package configuration using the deprecated built-in Python support in sqlc.yaml. ```yaml sql: - schema: "schema.sql" queries: "query.sql" engine: "postgresql" gen: python: out: "src" package: "foo" emit_sync_querier: true emit_async_querier: true query_parameter_limit: 5 ``` -------------------------------- ### Basic Configuration with Multiple Engines Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/config.md This example shows a basic configuration for version 2, defining settings for both PostgreSQL and MySQL databases. It includes schema and query paths, engine types, and Go code generation parameters. ```yaml version: "2" cloud: project: "" sql: - schema: "postgresql/schema.sql" queries: "postgresql/query.sql" engine: "postgresql" gen: go: package: "authors" out: "postgresql" database: managed: true rules: - sqlc/db-prepare - schema: "mysql/schema.sql" queries: "mysql/query.sql" engine: "mysql" gen: go: package: "authors" out: "mysql" ``` -------------------------------- ### Build sqlc for Local Development Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/development.md Install sqlc locally under an alias like `sqlc-dev` for development purposes. Ensure the output path is in your Go bin directory. ```bash go build -o ~/go/bin/sqlc-dev ./cmd/sqlc ``` -------------------------------- ### Install sqlc-gen-json Plugin Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/development.md Install the `sqlc-gen-json` plugin to prevent test failures. This is a necessary step for running the test suite. ```bash go build -o ~/go/bin/sqlc-gen-json ./cmd/sqlc-gen-json ``` -------------------------------- ### Start Docker Compose Services Source: https://github.com/sqlc-dev/sqlc/blob/main/CLAUDE.md Commands to check the status and start Docker Compose services, typically used for setting up database environments for testing. ```bash docker compose ps docker compose up -d ``` -------------------------------- ### Configure Process Plugin Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/plugins.md Example configuration for a process-based plugin, defining the command to execute and options for code generation. ```yaml version: '2' plugins: - name: jsonb process: cmd: sqlc-gen-json sql: - schema: schema.sql queries: query.sql engine: postgresql codegen: - out: gen plugin: jsonb options: indent: " " filename: codegen.json ``` -------------------------------- ### Run Full Test Suite in sqlc Source: https://github.com/sqlc-dev/sqlc/blob/main/CLAUDE.md Executes the complete test suite for sqlc, including examples. Requires databases to be running. ```bash go test --tags=examples -timeout 20m ./... ``` -------------------------------- ### Plugin Configuration Example Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/config.md Configure plugins for code generation, specifying WASM binaries or external commands. Environment variables can be passed to plugins, and the expected output format can be defined. ```yaml version: "2" plugins: - name: "py" wasm: url: "https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm" sha256: "428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2" - name: "js" env: - PATH process: cmd: "sqlc-gen-json" ``` -------------------------------- ### Default Go Struct Generation Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/rename.md Shows the default Go structs generated from the example SQL tables. ```go package db import ( "database/sql" ) type Author struct { ID int64 Name string Bio sql.NullString } type Publisher struct { ID int64 Name string } ``` -------------------------------- ### SQL Table Definitions Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/rename.md Example SQL schema definitions for tables that will be used to generate Go structs. ```sql CREATE TABLE authors ( id BIGSERIAL PRIMARY KEY, name text NOT NULL, bio text ); CREATE TABLE book_publishers ( id BIGSERIAL PRIMARY KEY, name text NOT NULL ); ``` -------------------------------- ### Database Connection URI with Environment Variables Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/config.md This example shows how to configure a database connection using a URI that includes environment variables. The `${PG_PASSWORD}` placeholder will be replaced by the value of the PG_PASSWORD environment variable at runtime. ```yaml version: '2' sql: - schema: schema.sql queries: query.sql engine: postgresql database: uri: postgresql://postgres:${PG_PASSWORD}@localhost:5432/authors gen: go: package: authors out: postgresql ``` -------------------------------- ### Start Docker Compose Services Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/migrating-off-hosted-managed-databases.md Run this command in the directory containing your docker-compose.yml file to start the database services in detached mode. ```sh docker compose up -d ``` -------------------------------- ### Migrate Package Configuration (JSON - Before) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/migrating-to-sqlc-gen-kotlin.md Example of a package configuration using the deprecated built-in Kotlin support in JSON format. This configuration will be updated to use the sqlc-gen-kotlin plugin. ```json "sql": [ { "schema": "schema.sql", "queries": "query.sql", "engine": "postgresql", "gen": { "kotlin": { "out": "src/main/kotlin/com/example/foo", "package": "com.example.foo" } } } ] ``` -------------------------------- ### MySQL LOAD DATA Configuration Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/insert.md Example sqlc configuration for using the :copyfrom command with MySQL, specifying database/sql and the MySQL driver. ```yaml version: "2" sql: - engine: "mysql" queries: "query.sql" schema: "query.sql" gen: go: package: "db" sql_package: "database/sql" sql_driver: "github.com/go-sql-driver/mysql" out: "db" ``` -------------------------------- ### Migrate Package Configuration (YAML - Before) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/migrating-to-sqlc-gen-kotlin.md Example of a package configuration using the deprecated built-in Kotlin support in YAML format. This configuration will be updated to use the sqlc-gen-kotlin plugin. ```yaml sql: - schema: "schema.sql" queries: "query.sql" engine: "postgresql" gen: kotlin: out: "src/main/kotlin/com/example/foo" package: "com.example.foo" ``` -------------------------------- ### Column to Field Name Conversion Examples Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/rename.md Illustrates the default algorithm for converting column names to struct field names. ```default account -> Account spotify_url -> SpotifyUrl app_id -> AppID ``` -------------------------------- ### Configure apt Proxy Source: https://github.com/sqlc-dev/sqlc/blob/main/internal/endtoend/CLAUDE.md Configures apt to use an HTTP proxy for package installations. Ensure the HTTP_PROXY environment variable is set before running. ```bash # Check if HTTP_PROXY is set echo $HTTP_PROXY # Configure apt to use the proxy sudo tee /etc/apt/apt.conf.d/99proxy << EOF Acquire::http::Proxy "$HTTP_PROXY"; Acquire::https::Proxy "$HTTPS_PROXY"; EOF # Update package lists sudo apt-get update -qq ``` -------------------------------- ### Use Generated Go Code with MySQL Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-mysql.md Demonstrates how to use the generated Go code to interact with a MySQL database. Ensure the MySQL driver is installed and database connection details are correct. ```go package main import ( "context" "database/sql" "log" "reflect" _ "github.com/go-sql-driver/mysql" "tutorial.sqlc.dev/app/tutorial" ) func run() error { ctx := context.Background() db, err := sql.Open("mysql", "user:password@/dbname?parseTime=true") if err != nil { return err } queries := tutorial.New(db) // list all authors authors, err := queries.ListAuthors(ctx) if err != nil { return err } log.Println(authors) // create an author result, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{ Name: "Brian Kernighan", Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true}, }) if err != nil { return err } insertedAuthorID, err := result.LastInsertId() if err != nil { return err } log.Println(insertedAuthorID) // get the author we just inserted fetchedAuthor, err := queries.GetAuthor(ctx, insertedAuthorID) if err != nil { return err } // prints true log.Println(reflect.DeepEqual(insertedAuthorID, fetchedAuthor.ID)) return nil } func main() { if err := run(); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Example sqlc configuration with lint rules Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/vet.md This YAML configuration demonstrates how to define and enable custom lint rules within `sqlc`. It specifies the SQL schema and query files, the database engine, and lists the rules to be applied. ```yaml version: 2 sql: - schema: "query.sql" queries: "query.sql" engine: "postgresql" gen: go: package: "authors" out: "db" rules: - no-pg - no-delete - only-one-param - no-exec rules: - name: no-pg message: "invalid engine: postgresql" rule: | config.engine == "postgresql" - name: no-delete message: "don't use delete statements" rule: | query.sql.contains("DELETE") - name: only-one-param message: "too many parameters" rule: | query.params.size() > 1 - name: no-exec message: "don't use exec" rule: | query.cmd == "exec" ``` -------------------------------- ### Find All Tables in a Statement Source: https://github.com/sqlc-dev/sqlc/blob/main/internal/sql/astutils/CLAUDE.md Example of using astutils.Walk to find all RangeVar nodes within a statement's FromClause. ```go var tv tableVisitor astutils.Walk(&tv, stmt.FromClause) // tv.list now contains all RangeVar nodes ``` -------------------------------- ### Configure WASM Plugin Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/plugins.md Example configuration for a WASM plugin, specifying its URL, SHA256 hash, and options for code generation. ```yaml version: '2' plugins: - name: greeter wasm: url: https://github.com/sqlc-dev/sqlc-gen-greeter/releases/download/v0.1.0/sqlc-gen-greeter.wasm sha256: afc486dac2068d741d7a4110146559d12a013fd0286f42a2fc7dcd802424ad07 sql: - schema: schema.sql queries: query.sql engine: postgresql codegen: - out: gen plugin: greeter options: lang: en-US ``` -------------------------------- ### VariableExpr AST Node Creation Source: https://github.com/sqlc-dev/sqlc/blob/main/internal/sql/ast/CLAUDE.md Example of creating a new AST node type for a variable expression, including its fields and a Format method. ```go package ast type VariableExpr struct { Name string Location int } func (n *VariableExpr) Pos() int { return n.Location } func (n *VariableExpr) Format(buf *TrackedBuffer) { if n == nil { return } buf.WriteString("@") buf.WriteString(n.Name) } ``` -------------------------------- ### Example sqlc Configuration with Renaming Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/config.md Demonstrates how to configure packages and apply renaming rules for struct fields in sqlc. The `rename` mapping allows customization of generated struct field names based on column names. ```yaml version: "1" packages: [...] # package configurations go here rename: spotify_url: "SpotifyURL" ``` -------------------------------- ### Configure sqlc for PostgreSQL Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-postgresql.md Configure your sqlc.yaml file to use PostgreSQL as the database engine and specify query and schema files. This setup is essential for generating Go code from your SQL. ```yaml version: "2" cloud: # Replace with your project ID from the sqlc Cloud dashboard project: "" sql: - engine: "postgresql" queries: "query.sql" schema: "schema.sql" gen: go: package: "tutorial" out: "tutorial" sql_package: "pgx/v5" ``` -------------------------------- ### Initialize Go Module Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-sqlite.md Initializes a new Go module for the project. This command is run in the project's root directory. ```shell go mod init tutorial.sqlc.dev/app ``` -------------------------------- ### Connect to PostgreSQL using pgx in Go Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/using-go-and-pgx.md Connect to your PostgreSQL database using `pgx.Connect` and initialize the generated Go database client. ```go package main import ( "context" "fmt" "os" "github.com/jackc/pgx/v5" "example.com/sqlc-tutorial/db" ) func main() { // urlExample := "postgres://username:password@localhost:5432/database_name" conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL")) if err != nil { fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err) os.Exit(1) } defer conn.Close(context.Background()) q := db.New(conn) author, err := q.GetAuthor(context.Background(), 1) if err != nil { fmt.Fprintf(os.Stderr, "GetAuthor failed: %v\n", err) os.Exit(1) } fmt.Println(author.Name) } ``` -------------------------------- ### PostgreSQL EXPLAIN Rule Example Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/vet.md Example of a PostgreSQL rule that checks if the query cost estimate is too high. This rule uses the `postgresql.explain` variable. ```yaml rules: - name: postgresql-query-too-costly message: "Query cost estimate is too high" rule: "postgresql.explain.plan.total_cost > 1.0" ``` -------------------------------- ### Run All Tests Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/development.md Execute all unit tests for the sqlc project using the standard Go test command. ```bash go test ./... ``` -------------------------------- ### Fetch SQLite Driver and Build Project Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-sqlite.md Fetches the necessary SQLite driver and builds the Go project. These commands are essential for compiling and running the Go program that uses the generated sqlc code. ```shell go get modernc.org/sqlite go build ./... ``` -------------------------------- ### Enable Dump Explain Output Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/environment-variables.md Set SQLCDEBUG=dumpexplain=1 to print the JSON-formatted result of running EXPLAIN on a query when a sqlc vet rule requires its output. ```bash SQLCDEBUG=dumpexplain=1 ``` -------------------------------- ### sqlc verify Output Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/verify.md Example output from `sqlc verify` indicating an ambiguous column reference error in the 'app query.sql' file. ```sh $ sqlc verify FAIL: app query.sql === Failed === FAIL: app query.sql GetUserActions ERROR: column reference "created_at" is ambiguous (SQLSTATE 42702) ``` -------------------------------- ### Set SQLC_AUTH_TOKEN Environment Variable Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/push.md Create an authentication token in sqlc Cloud and make it available via the `SQLC_AUTH_TOKEN` environment variable. ```shell export SQLC_AUTH_TOKEN=sqlc_xxxxxxxx ``` -------------------------------- ### Debugging EXPLAIN Output with a Dummy Rule Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/vet.md Configuration to enable debugging of EXPLAIN output by setting `SQLCDEBUG=dumpexplain=1` and using a dummy rule to trigger the explain plan generation for all queries. ```yaml version: 2 sql: - schema: "query.sql" queries: "query.sql" engine: "postgresql" database: uri: "postgresql://postgres:postgres@localhost:5432/postgres" gen: go: package: "db" out: "db" rules: - debug rules: - name: debug rule: "!has(postgresql.explain)" # A dummy rule to trigger explain ``` -------------------------------- ### Basic `sqlc.yaml` Configuration Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/generate.md This is a minimal configuration for `sqlc.yaml` specifying the SQL engine, query and schema file paths, and Go code generation settings. ```yaml version: "2" sql: - engine: "postgresql" queries: "query.sql" schema: "schema.sql" gen: go: package: "tutorial" out: "tutorial" sql_package: "pgx/v5" ``` -------------------------------- ### Go Code for Prepared Queries Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/prepared_query.md This Go code demonstrates how to generate and use prepared queries with sqlc. It includes the necessary interfaces, query preparation, and execution logic. Ensure `emit_prepared_queries` is set to `true` in your sqlc configuration. ```go package db import ( "context" "database/sql" "fmt" ) type Record struct { ID int32 } type DBTX interface { PrepareContext(context.Context, string) (*sql.Stmt, error) QueryRowContext(context.Context, string, ...interface{}) *sql.Row } func New(db DBTX) *Queries { return &Queries{db: db} } func Prepare(ctx context.Context, db DBTX) (*Queries, error) { q := Queries{db: db} var err error if q.getRecordStmt, err = db.PrepareContext(ctx, getRecord); err != nil { return nil, fmt.Errorf("error preparing query GetRecord: %w", err) } return &q, nil } func (q *Queries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) *sql.Row { switch { case stmt != nil && q.tx != nil: return q.tx.StmtContext(ctx, stmt).QueryRowContext(ctx, args...) case stmt != nil: return stmt.QueryRowContext(ctx, args...) default: return q.db.QueryRowContext(ctx, query, args...) } } type Queries struct { db DBTX tx *sql.Tx getRecordStmt *sql.Stmt } func (q *Queries) WithTx(tx *sql.Tx) *Queries { return &Queries{ db: tx, tx: tx, getRecordStmt: q.getRecordStmt, } } const getRecord = `-- name: GetRecord :one SELECT id FROM records WHERE id = $1 ` func (q *Queries) GetRecord(ctx context.Context, id int32) (int32, error) { row := q.queryRow(ctx, q.getRecordStmt, getRecord, id) err := row.Scan(&id) return id, err } ``` -------------------------------- ### Configure go-geom for PostGIS Geometry Types Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/datatypes.md Use this SQL schema definition for tables containing PostGIS geometry types. This example uses Multipolygon with EPSG:27700. ```sql -- Multipolygons in British National Grid (epsg:27700) create table shapes( id serial, name varchar, geom geometry(Multipolygon, 27700) ); -- name: GetShapes :many SELECT * FROM shapes; ``` -------------------------------- ### PostgreSQL EXPLAIN Rule for Sequential Scan Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/vet.md Example of a PostgreSQL rule that checks if the query plan results in a sequential scan. This rule uses the `postgresql.explain` variable. ```yaml - name: postgresql-no-seq-scan message: "Query plan results in a sequential scan" rule: "postgresql.explain.plan.node_type == 'Seq Scan'" ``` -------------------------------- ### SQL Schema Modifications Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ddl.md sqlc parses CREATE TABLE and ALTER TABLE statements to generate Go code. These examples show basic schema modifications. ```sql CREATE TABLE authors ( id SERIAL PRIMARY KEY, birth_year int NOT NULL ); ALTER TABLE authors ADD COLUMN bio text NOT NULL; ALTER TABLE authors DROP COLUMN birth_year; ALTER TABLE authors RENAME TO writers; ``` -------------------------------- ### Per-Package Configuration Structure (v1) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/overrides.md Illustrates the structure for per-package configurations within the `packages` list in version 1 of the `sqlc` configuration. Overrides are nested under the `overrides` key. ```yaml version: "1" packages: - name: "db" path: "internal/db" queries: "./sql/query/" schema: "./sql/schema/" engine: "postgresql" overrides: [...] ``` -------------------------------- ### Global Type Overrides (v1) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/overrides.md Configure global type overrides in version 1 of the `sqlc` configuration. This example maps the `uuid` database type to `github.com/gofrs/uuid.UUID` in Go. ```yaml version: "1" packages: [...] overrides: - db_type: "uuid" go_type: "github.com/gofrs/uuid.UUID" ``` -------------------------------- ### Build Development Versions of sqlc Source: https://github.com/sqlc-dev/sqlc/blob/main/CLAUDE.md Commands to build the main sqlc binary and the JSON plugin for development purposes. Ensure your Go environment is set up correctly. ```bash # 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-json ``` -------------------------------- ### Use Generated Go Code with PostgreSQL Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-postgresql.md Demonstrates how to use the Go code generated by sqlc to interact with a PostgreSQL database using the pgx/v5 driver. Includes connecting to the database, creating, listing, and fetching author records. ```go package main import ( "context" "log" "reflect" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" "tutorial.sqlc.dev/app/tutorial" ) func run() error { ctx := context.Background() conn, err := pgx.Connect(ctx, "user=pqgotest dbname=pqgotest sslmode=verify-full") if err != nil { return err } defer conn.Close(ctx) queries := tutorial.New(conn) // list all authors authors, err := queries.ListAuthors(ctx) if err != nil { return err } log.Println(authors) // create an author insertedAuthor, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{ Name: "Brian Kernighan", Bio: pgtype.Text{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true}, }) if err != nil { return err } log.Println(insertedAuthor) // get the author we just inserted fetchedAuthor, err := queries.GetAuthor(ctx, insertedAuthor.ID) if err != nil { return err } // prints true log.Println(reflect.DeepEqual(insertedAuthor, fetchedAuthor)) return nil } func main() { if err := run(); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Define PostgreSQL Queries Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-postgresql.md Defines a set of SQL queries for interacting with the 'authors' table in PostgreSQL. Includes operations for getting, listing, creating, updating, and deleting authors. ```sql -- name: GetAuthor :one SELECT * FROM authors WHERE id = $1 LIMIT 1; -- name: ListAuthors :many SELECT * FROM authors ORDER BY name; -- name: CreateAuthor :one INSERT INTO authors ( name, bio ) VALUES ($1, $2) RETURNING *; -- name: UpdateAuthor :exec UPDATE authors set name = $2, bio = $3 WHERE id = $1; -- name: DeleteAuthor :exec DELETE FROM authors WHERE id = $1; ``` -------------------------------- ### Use pgxpool for connection pooling in Go Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/using-go-and-pgx.md For production applications, use `pgxpool.New` to create a connection pool and initialize the generated Go database client. ```go import ( "github.com/jackc/pgx/v5/pgxpool" "example.com/sqlc-tutorial/db" ) func main() { pool, err := pgxpool.New(context.Background(), os.Getenv("DATABASE_URL")) if err != nil { fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\n", err) os.Exit(1) } defer pool.Close() q := db.New(pool) // Use q the same way as with single connections } ``` -------------------------------- ### Run Basic Unit Tests Source: https://github.com/sqlc-dev/sqlc/blob/main/CLAUDE.md Execute all unit tests in the project that do not require external dependencies like a database. ```bash go test ./... ``` -------------------------------- ### MySQL EXPLAIN Rule for Query Cost Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/vet.md Example of a MySQL rule that checks if the query cost estimate is too high. This rule uses the `mysql.explain` variable and requires checking for the existence of `cost_info`. ```yaml - name: mysql-query-too-costly message: "Query cost estimate is too high" rule: "has(mysql.explain.query_block.cost_info) && double(mysql.explain.query_block.cost_info.query_cost) > 2.0" ``` -------------------------------- ### Global Go Type Overrides (v2) Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/overrides.md Configure global overrides for Go types in version 2 of the `sqlc` configuration. This example maps `pg_catalog.timestamptz` to `null.Time` from `gopkg.in/guregu/null.v4` when the column is nullable. ```yaml version: "2" overrides: go: overrides: - db_type: "pg_catalog.timestamptz" nullable: true engine: "postgresql" go_type: import: "gopkg.in/guregu/null.v4" package: "null" type: "Time" sql: - schema: "service1/schema.sql" queries: "service1/query.sql" engine: "postgresql" gen: go: package: "service1" out: "service1" - schema: "service2/schema.sql" queries: "service2/query.sql" engine: "postgresql" gen: go: package: "service2" out: "service2" ``` -------------------------------- ### Enhanced Analysis with Your Own Database Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/generate.md Opt-in to database-backed analysis by providing a `uri` in your `sqlc.yaml` database configuration. This allows `sqlc` to use your existing database for analysis. The `uri` can reference environment variables for sensitive information like passwords. ```yaml version: "2" sql: - engine: "postgresql" queries: "query.sql" schema: "schema.sql" database: uri: "postgres://postgres:${PG_PASSWORD}@localhost:5432/postgres" gen: go: out: "db" sql_package: "pgx/v5" ``` -------------------------------- ### Improve Codegen with Managed Databases Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/managed-databases.md Configure sqlc to use managed databases for improved query analysis during code generation. This setup caches analysis locally for faster subsequent runs. ```yaml version: '2' servers: - engine: postgresql uri: "postgres://localhost:5432/postgres?sslmode=disable" sql: - schema: schema.sql queries: query.sql engine: postgresql database: managed: true gen: go: out: "db" ``` -------------------------------- ### Add Debug Logging for AST Node Visitation Source: https://github.com/sqlc-dev/sqlc/blob/main/internal/sql/rewrite/CLAUDE.md Provides an example of how to add debug logging within an AST traversal case to verify if a specific node type is being visited and inspect its fields. ```go case *ast.YourType: fmt.Printf("Visiting YourType with fields: %+v\n", n) a.apply(n, "ChildField", nil, n.ChildField) ``` -------------------------------- ### SQL Schema for Prepared Queries Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/prepared_query.md Define your database schema and queries. The `CREATE TABLE` statement sets up the necessary table, and the `-- name: GetRecord :one` comment defines a query that sqlc will generate a Go function for. ```sql CREATE TABLE records ( id SERIAL PRIMARY KEY ); -- name: GetRecord :one SELECT * FROM records WHERE id = $1; ``` -------------------------------- ### MySQL EXPLAIN Rule for Primary Key Usage Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/vet.md Example of a MySQL rule that checks if the query plan uses the primary key. This rule uses the `mysql.explain` variable and verifies the 'key' field. ```yaml - name: mysql-must-use-primary-key message: "Query plan doesn't use primary key" rule: "has(mysql.explain.query_block.table.key) && mysql.explain.query_block.table.key != 'PRIMARY'" ``` -------------------------------- ### Generate Go code with sqlc Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/guides/using-go-and-pgx.md Run the `sqlc generate` command to produce pgx-compatible Go database access methods. ```bash sqlc generate ``` -------------------------------- ### SQLC CLI Usage Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/cli.md Displays the available commands and global flags for the SQLC CLI. Use this to understand the overall structure and options. ```sh Usage: sqlc [command] Available Commands: compile Statically check SQL for syntax and type errors completion Generate the autocompletion script for the specified shell createdb Create an ephemeral database diff Compare the generated files to the existing files generate Generate source code from SQL help Help about any command init Create an empty sqlc.yaml settings file push Push the schema, queries, and configuration for this project verify Verify schema, queries, and configuration for this project version Print the sqlc version number vet Vet examines queries Flags: -f, --file string specify an alternate config file (default: sqlc.yaml) -h, --help help for sqlc --no-database disable database connections (default: false) Use "sqlc [command] --help" for more information about a command. ``` -------------------------------- ### Advanced Go Type Override Configuration Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/overrides.md For complex type mappings, the `go_type` field can be a map. This example demonstrates configuring a `db_type` override for `uuid` to use a specific type `MyType` from `a/b/v2` as a pointer. ```yaml version: "2" sql: - schema: "postgresql/schema.sql" queries: "postgresql/query.sql" engine: "postgresql" gen: go: package: "authors" out: "db" sql_package: "pgx/v5" overrides: - db_type: "uuid" go_type: import: "a/b/v2" package: "b" type: "MyType" pointer: true ``` -------------------------------- ### Configure sqlc.yaml for MySQL Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-mysql.md Add the `cloud` block to your `sqlc.yaml` file to enable sqlc Cloud verification. Replace `` with your actual project ID from the sqlc Cloud dashboard. ```yaml version: "2" cloud: # Replace with your project ID from the sqlc Cloud dashboard project: "" sql: - engine: "mysql" queries: "query.sql" schema: "schema.sql" gen: go: package: "tutorial" out: "tutorial" ``` -------------------------------- ### Generated Go Code for Selecting Specific Columns Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/select.md Go code generated by sqlc for retrieving specific columns from the authors table. Includes methods for getting a single bio or multiple info fields. ```go package db import ( "context" "database/sql" ) type DBTX interface { QueryRowContext(context.Context, string, ...interface{}) *sql.Row } func New(db DBTX) *Queries { return &Queries{db: db} } type Queries struct { db DBTX } const getBioForAuthor = `-- name: GetBioForAuthor :one SELECT bio FROM authors WHERE id = $1 ` func (q *Queries) GetBioForAuthor(ctx context.Context, id int) (string, error) { row := q.db.QueryRowContext(ctx, getBioForAuthor, id) var i string err := row.Scan(&i) return i, err } const getInfoForAuthor = `-- name: GetInfoForAuthor :one SELECT bio, birth_year FROM authors WHERE id = $1 ` type GetInfoForAuthorRow struct { Bio string BirthYear int } func (q *Queries) GetInfoForAuthor(ctx context.Context, id int) (GetInfoForAuthorRow, error) { row := q.db.QueryRowContext(ctx, getInfoForAuthor, id) var i GetInfoForAuthorRow err := row.Scan(&i.Bio, &i.BirthYear) return i, err } ``` -------------------------------- ### Verify database migrations with sqlc verify and push Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/ci-cd.md This comprehensive GitHub Actions workflow verifies database migrations using `sqlc verify` and pushes changes to sqlc Cloud. It runs on every push, sets up PostgreSQL, and requires both a connection URI and an auth token. ```yaml name: sqlc on: [push] jobs: verify: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: sqlc-dev/setup-sqlc@v3 with: sqlc-version: '1.31.1' - uses: sqlc-dev/action-setup-postgres@master with: postgres-version: "16" id: postgres - run: sqlc verify env: POSTGRESQL_SERVER_URI: ${{ steps.postgres.outputs.connection-uri }}?sslmode=disable SQLC_AUTH_TOKEN: ${{ secrets.SQLC_AUTH_TOKEN }} push: runs-on: ubuntu-latest if: ${{ github.ref == 'refs/heads/main' }} steps: - uses: sqlc-dev/setup-sqlc@v3 with: sqlc-version: '1.31.1' - run: sqlc push env: SQLC_AUTH_TOKEN: ${{ secrets.SQLC_AUTH_TOKEN }} ``` -------------------------------- ### Generate Structs with Singular Table Names Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/structs.md Struct names are generated using the singular form of table names when the table name is pluralized. This example shows a plural table 'authors' generating a singular struct 'Author'. ```sql CREATE TABLE authors ( id SERIAL PRIMARY KEY, name text NOT NULL ); ``` ```go package db // Struct names use the singular form of table names type Author struct { ID int Name string } ``` -------------------------------- ### Enable Dump Vet Environment Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/reference/environment-variables.md Use SQLCDEBUG=dumpvetenv=1 to print the variables available to a sqlc vet rule during evaluation. ```bash SQLCDEBUG=dumpvetenv=1 ``` -------------------------------- ### Configure Go Type Overrides in SQLC Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/overrides.md Use the `overrides` list in your `sqlc.yaml` to specify custom Go types for database columns or types. This example shows how to map PostgreSQL `uuid` to `github.com/google/uuid.UUID` and a specific column `users.birthday` to `time.Time`. ```yaml version: "2" sql: - schema: "postgresql/schema.sql" queries: "postgresql/query.sql" engine: "postgresql" gen: go: package: "authors" out: "db" sql_package: "pgx/v5" overrides: - db_type: "uuid" nullable: true go_type: import: "github.com/google/uuid" type: "UUID" - column: "users.birthday" go_type: "time.Time" ``` -------------------------------- ### Fetch PostgreSQL Driver Source: https://github.com/sqlc-dev/sqlc/blob/main/docs/tutorials/getting-started-postgresql.md Fetches the pgx/v5 PostgreSQL driver for Go. This is a necessary step before building and running the Go application that uses the generated sqlc code. ```shell go get github.com/jackc/pgx/v5 go build ./... ``` -------------------------------- ### Find and Replace Named Parameters in AST Source: https://github.com/sqlc-dev/sqlc/blob/main/internal/sql/named/CLAUDE.md Demonstrates how to traverse an Abstract Syntax Tree (AST) to find all named parameters using `IsParamFunc` and `IsParamSign`, and then replace them with positional parameters using `astutils.Apply` and `ast.ParamRef`. ```go // Find all named parameters params := astutils.Search(root, func(node ast.Node) bool { return named.IsParamFunc(node) || named.IsParamSign(node) }) // Replace with positional parameters astutils.Apply(root, func(cr *astutils.Cursor) bool { if named.IsParamFunc(cr.Node()) || named.IsParamSign(cr.Node()) { cr.Replace(&ast.ParamRef{Number: nextParam()}) } return true }, nil) ```